public HandDetectorState DetectHand(Bitmap source) { ImageFrame = new Image <Bgr, byte>(source); var skinDetector = new HsvSkinDetector(); var skin = skinDetector.DetectSkin(ImageFrame, hsv_min, hsv_max); ExtractContourAndHull(ImageFrame, skin); var result = DrawAndComputeFingersNum(); startIndex = null; endIndex = null; depthIndex = null; defects = null; currentContour = null; var handDetectorState = new HandDetectorState() { Coordinates = result, IsActive = IsDrawing(), CountedCoordinates = MainWindow.CountMousePosition(ImageFrame, result) }; return(handDetectorState); }
protected virtual void OnActionChanged(HandDetectorState handDetectorState) { HandDetectorEvent?.Invoke(this, new HandDetectorEventArgs() { HandDetectorState = handDetectorState }); }
private void DetectorTimerTick(object sender, EventArgs e) { var currentFrame = _capture.QueryFrame(); var mirrorFrame = new Mat(); CvInvoke.Flip(currentFrame, mirrorFrame, FlipType.Horizontal); var handDetectorState = DetectHand(mirrorFrame.Bitmap); float sumx = 0; float sumy = 0; int isActiveCount = 0; foreach (var state in previousStates) { sumx += state.Coordinates.X; sumy += state.Coordinates.Y; if (state.IsActive) { isActiveCount++; } } float avgx = sumx / previousStates.Count(); float avgy = sumy / previousStates.Count(); if (Math.Abs(avgx - handDetectorState.Coordinates.X) <= PossibleDiffrence && Math.Abs(avgy - handDetectorState.Coordinates.Y) <= PossibleDiffrence) { CurrentState = handDetectorState; } if (previousStates.Count >= QueueSize) { previousStates.Dequeue(); } previousStates.Enqueue(handDetectorState); }
public void Run() { previousStates = new Queue <HandDetectorState>(QueueSize); _capture = new VideoCapture(); //_detectorTimer = new DispatcherTimer(); //_detectorTimer.Tick += DetectorTimerTick; //_detectorTimer.Interval = new TimeSpan(0, 0, 0, 0, 10); //_detectorTimer.Start(); while (true) { var currentFrame = _capture.QueryFrame(); var mirrorFrame = new Mat(); CvInvoke.Flip(currentFrame, mirrorFrame, FlipType.Horizontal); var handDetectorState = DetectHand(mirrorFrame.Bitmap); float sumx = 0; float sumy = 0; int isActiveCount = 0; foreach (var state in previousStates) { sumx += state.Coordinates.X; sumy += state.Coordinates.Y; if (state.IsActive) { isActiveCount++; } } float avgx = sumx / previousStates.Count(); float avgy = sumy / previousStates.Count(); if (Math.Abs(avgx - handDetectorState.Coordinates.X) <= PossibleDiffrence && Math.Abs(avgy - handDetectorState.Coordinates.Y) <= PossibleDiffrence) { if ((handDetectorState.IsActive && !CurrentState.IsActive) || (!handDetectorState.IsActive && CurrentState.IsActive)) { if (handDetectorState.IsActive && isActiveCount < QueueSize / 1.5) { handDetectorState.IsActive = false; } OnActionChanged(handDetectorState); } CurrentState = handDetectorState; } if (previousStates.Count >= QueueSize) { previousStates.Dequeue(); } previousStates.Enqueue(handDetectorState); Thread.Sleep(0); } }