在kinect v2中停止手部运动后运行事件

时间:2017-05-27 08:50:11

标签: c# wpf kinect

我开发了一款游戏,在fisrt我向用户展示了一张照片,用户应该将手移到顶部,右侧,左侧,底部以选择正确的图像。 下面的图像与我的游戏相同:

enter image description here

在显示图像后,用户应该移动手,之后我应该验证图像和移动。要做到这一点,我有这个代码:

第一名:

private void RunCamera()
    {
        _sensor = KinectSensor.GetDefault();

        if (_sensor != null)
        {
            _sensor.Open();

            _reader = _sensor.OpenMultiSourceFrameReader(FrameSourceTypes.Color | FrameSourceTypes.Depth | FrameSourceTypes.Infrared | FrameSourceTypes.Body);
            _reader.MultiSourceFrameArrived += Reader_MultiSourceFrameArrived;

        }

    }`

和:

   private void Reader_MultiSourceFrameArrived(object sender, MultiSourceFrameArrivedEventArgs e)
    {

        var reference = e.FrameReference.AcquireFrame();

        // Color
        using (var frame = reference.ColorFrameReference.AcquireFrame())
        {
            if (frame != null)
            {
                camera.Source = frame.ToBitmap();
            }
        }
        // Body
        using (var frame = reference.BodyFrameReference.AcquireFrame())
        {
            if (frame != null)
            {
                canvas.Children.Clear();

                _bodies = new Body[frame.BodyFrameSource.BodyCount];
                frame.GetAndRefreshBodyData(_bodies);
                distancedetect();
            }
            // End If
        }
        if(_bodies!=null)
        if (_bodies.Any())
            RunDetect();

    }

 private void RunDetect()
    {
        RightHandDetection();
        EblowrightHandDetection();

    }

rightHandDetectionEblowrightHandDetection我将postions设置为两个文本框,每个textBox都有text_change个事件,因为我用它来检查。

public void RightHandDetection()
        {

        foreach (var body in _bodies)
        {
            if (body != null)
            {
                if (body.IsTracked)
                {
                       handRight = body.Joints[JointType.HandRight];


                    CameraSpacePoint camerapointRight = handRight.Position;
                    double xr = Math.Round(camerapointRight.X, 2);
                    double yr = Math.Round(camerapointRight.Y, 2);

                    CameraSpacePoint right = body.Joints[JointType.HandRight].Position;


                    double zr = camerapointRight.Z;
                    colorPointR = _sensor.CoordinateMapper.MapCameraPointToColorSpace(camerapointRight);
                    try
                    {

                        Canvas.SetLeft(ellipseRight, (colorPointR.X + (170)));
                        Canvas.SetTop(ellipseRight, (colorPointR.Y - (37.5 * 11)));

                    }
                    catch (Exception ex)
                    {
                    }
                    ellipseRight.Stroke = Brushes.Black;
                    ellipseRight.Fill = Brushes.Green;
                    ellipseRight.HorizontalAlignment = System.Windows.HorizontalAlignment.Left;
                    ellipseRight.VerticalAlignment = System.Windows.VerticalAlignment.Center;
                    ellipseRight.Width = 30;
                    ellipseRight.Height = 30;

                    try
                    {

                        canvas.Children.Add(ellipseRight);
                    }
                    catch (Exception ex)
                    {
                    }
                }
            }
        }
        if (disableEvent) return;
        if (currentEye == CurrentEye.Right && _isRun)
        {
            txtdetailRightX.Text = Convert.ToString((Math.Round(colorPointR.X + (170)) / Denominator).ToString("#"));
            txtdetailRighty.Text = Convert.ToString((Math.Round(colorPointR.Y - (37.5 * 11)) / Denominator).ToString("#"));
        }
    }

    public void EblowrightHandDetection()
    {
        foreach (var body in _bodies)
        {
            if (body != null)
            {
                if (body.IsTracked)
                {

                    ElbowRightjoint = body.Joints[JointType.ElbowRight];
                    ColorSpacePointElbowRight = _sensor.CoordinateMapper.MapCameraPointToColorSpace(ElbowRightjoint.Position);
                    try
                    {

                        Canvas.SetLeft(ellipseElbowRight, (ColorSpacePointElbowRight.X + (160)));
                        Canvas.SetTop(ellipseElbowRight, (ColorSpacePointElbowRight.Y - (37.5 * 12)));

                    }
                    catch (Exception ex)
                    {
                    }
                    ellipseElbowRight.Stroke = Brushes.Green;
                    ellipseElbowRight.Fill = Brushes.Yellow;
                    ellipseElbowRight.HorizontalAlignment = System.Windows.HorizontalAlignment.Center;
                    ellipseElbowRight.VerticalAlignment = System.Windows.VerticalAlignment.Center;
                    ellipseElbowRight.Width = 30;
                    ellipseElbowRight.Height = 30;
                    try
                    {
                        canvas.Children.Add(ellipseElbowRight);
                    }
                    catch (Exception ex)
                    {
                    }
                }
            }
        }


        if (disableEvent) return;
        if (currentEye == CurrentEye.Right && _isRun)
        {
            txtElbowRightX.Text = Convert.ToString((Math.Round(ColorSpacePointElbowRight.X + (160)) / Denominator).ToString("#"));
            txtElbowRightY.Text = Convert.ToString((Math.Round(ColorSpacePointElbowRight.Y - (37.5 * 12)) / Denominator).ToString("#"));
        }

    }`

和textchnages是:

`private void txtdetailRightX_TextChanged(object sender, TextChangedEventArgs e)
        {
        if (disableEvent) return;

        if (currentEye == CurrentEye.Right && _isRun)
        {
            if (txtdetailRightX.Text.CompareTo(txtElbowRightX.Text) > 0) //right hand to >> right
            {

                HandelDirection(directionType.Right);
            }
            else if (txtdetailRightX.Text.CompareTo(txtElbowRightX.Text) < 0)//right hand to  left
            {

                HandelDirection(directionType.Left);
            }
            //Task.Delay(3000);
        }
    }


    private void txtdetailRighty_TextChanged(object sender, TextChangedEventArgs e)
    {
        if (disableEvent) return;
        if (currentEye == CurrentEye.Right && _isRun)
        {
            if (txtdetailRighty.Text.CompareTo(txtElbowRightY.Text) > 0)
            {

                HandelDirection(directionType.Down);

            }
            else if (txtdetailRighty.Text.CompareTo(txtElbowRightY.Text) < 0)
            {

                HandelDirection(directionType.Up);
            }

        }
    }`

我使用下面的代码来检查图像和用户手的移动:

   public void HandelDirection(directionType typeId)
        {
 if (currentImmage == typeId){


 //mark Score
    chnagePicture();
    }
    }

我的问题:

1 - 当Chnage Kinect Height我的计算不正确时,我无法检测到手的正确性。

2 - 当用户将手移到右边(或另一个)时,我调用了检测因为每次调用text_changed都被调用。

0 个答案:

没有答案