Exemple #1
0
        /// <summary>
        /// Read body frames
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void bodyFrameReader_FrameArrived(object sender, BodyFrameArrivedEventArgs e)
        {
            bool dataReceived = false;

            using (BodyFrame bodyFrame = e.FrameReference.AcquireFrame())
            {
                if (bodyFrame != null)
                {
                    if (this.bodies == null)
                    {
                        this.bodies = new Body[bodyFrame.BodyCount];
                    }

                    // The first time GetAndRefreshBodyData is called, Kinect will allocate each Body in the array.
                    // As long as those body objects are not disposed and not set to null in the array,
                    // those body objects will be re-used.
                    bodyFrame.GetAndRefreshBodyData(this.bodies);
                    dataReceived = true;
                }
            }

            if (!dataReceived)
            {
                alreadyTrackedPos = false;
                return;
            }

            if (this.bodies != null)
            {
                // loop through all bodies to see if any of the gesture detectors need to be updated
                int maxBodies = this.sensor.BodyFrameSource.BodyCount;
                for (int i = 0; i < maxBodies; ++i)
                {
                    Body  body       = this.bodies[i];
                    ulong trackingId = body.TrackingId;

                    // if the current body TrackingId changed, update the corresponding gesture detector with the new value
                    if (trackingId != this.gestureDetectorList[i].TrackingId)
                    {
                        this.gestureDetectorList[i].TrackingId = trackingId;

                        // if the current body is tracked, unpause its detector to get VisualGestureBuilderFrameArrived events
                        // if the current body is not tracked, pause its detector so we don't waste resources trying to get invalid gesture results
                        this.gestureDetectorList[i].IsPaused = trackingId == 0;
                    }
                }
            }
            foreach (Body body in this.bodies)
            {
                // get first tracked body only, notice there's a break below.
                if (body.IsTracked)
                {
                    // get various skeletal positions
                    CameraSpacePoint handLeft  = body.Joints[JointType.HandLeft].Position;
                    CameraSpacePoint handRight = body.Joints[JointType.HandRight].Position;
                    CameraSpacePoint spineBase = body.Joints[JointType.SpineBase].Position;
                    if (handRight.Z - spineBase.Z < -0.15f)
                    {
                        if ((handRight.Y - spineBase.Y >= 0.9) && (body.HandRightState == HandState.Open))
                        {
                            MouseControl.Vol_Up();
                            //    System.Threading.Thread.Sleep(2500);
                        }
                        else if ((handRight.Y - spineBase.Y <= 0.2f) && (body.HandRightState == HandState.Open))
                        {
                            MouseControl.Vol_Down();
                            //   System.Threading.Thread.Sleep(2500);
                        }
                        else if ((handRight.X - handLeft.X <= 0.25f) && (body.HandRightState == HandState.Open && body.HandLeftState == HandState.Open))
                        {
                            MouseControl.DoScroll();
                        }

                        /*else if ((handRight.X - handLeft.X <= 0.45f) && (handRight.X - handLeft.X >= 0.25f) && (body.HandRightState == HandState.Open && body.HandLeftState == HandState.Open))
                         * {
                         *
                         *  MouseControl.DoPause();
                         *
                         * }*/
                        /*else if (handRight.X - handLeft.X >= 0.2f)
                         * {
                         *  if (body.HandRightState == HandState.Open && body.HandLeftState == HandState.Open)
                         *  {
                         *      MouseControl.DoPause();
                         *  }
                         * }*/
                        /* if ((handRight.X - spineBase.X >= 0.5f) && (body.HandRightState == HandState.Open))
                         * {
                         *   System.Windows.Forms.SendKeys.SendWait("{Left}");
                         * }
                         *
                         * else if ((handLeft.X - spineBase.X <= 0.5f) && (body.HandLeftState == HandState.Open))
                         * {
                         *   System.Windows.Forms.SendKeys.SendWait("{Right}");
                         * }*/

                        if (handRight.Z - spineBase.Z < -0.15f) // if right hand lift up
                        {
                            /* hand x calculated by this. we don't use shoulder right as a reference cause the shoulder right
                             * is usually behind the lift right hand, and the position would be inferred and unstable.
                             * because the spine base is on the left of right hand, we plus 0.05f to make it closer to the right. */
                            float x = handRight.X - spineBase.X + 0.05f;

                            /* hand y calculated by this. ss spine base is way lower than right hand, we plus 0.51f to make it
                             * higer, the value 0.51f is worked out by testing for a several times, you can set it as another one you like. */
                            float y = spineBase.Y - handRight.Y + 0.51f;
                            // get current cursor position
                            Point curPos = MouseControl.GetCursorPosition();
                            // smoothing for using should be 0 - 0.95f. The way we smooth the cusor is: oldPos + (newPos - oldPos) * smoothValue
                            float smoothing = 1 - cursorSmoothing;
                            // set cursor position
                            MouseControl.SetCursorPos((int)(curPos.X + (x * mouseSensitivity * screenWidth - curPos.X) * smoothing), (int)(curPos.Y + ((y + 0.25f) * mouseSensitivity * screenHeight - curPos.Y) * smoothing));

                            alreadyTrackedPos = true;

                            // Grip gesture
                            if (doClick)
                            {
                                if (body.HandRightState == HandState.Closed)
                                {
                                    if (!wasRightGrip)
                                    {
                                        MouseControl.MouseLeftDown();
                                        wasRightGrip = true;
                                    }
                                }

                                else if (body.HandRightState == HandState.Open)
                                {
                                    if (wasRightGrip)
                                    {
                                        MouseControl.MouseLeftUp();
                                        wasRightGrip = false;
                                    }
                                }
                            }
                        }
                    }

                    /* else if (handLeft.Z - spineBase.Z < -0.15f) // if left hand lift forward
                     * {
                     *   float x = handLeft.X - spineBase.X + 0.3f;
                     *   float y = spineBase.Y - handLeft.Y + 0.51f;
                     *   Point curPos = MouseControl.GetCursorPosition();
                     *   float smoothing = 1 - cursorSmoothing;
                     *   MouseControl.SetCursorPos((int)(curPos.X + (x * mouseSensitivity * screenWidth - curPos.X) * smoothing), (int)(curPos.Y + ((y + 0.25f) * mouseSensitivity * screenHeight - curPos.Y) * smoothing));
                     *   alreadyTrackedPos = true;
                     *
                     *   if (doClick )
                     *   {
                     *       if (body.HandLeftState == HandState.Closed)
                     *       {
                     *           if (!wasLeftGrip)
                     *           {
                     *               MouseControl.MouseLeftDown();
                     *               wasLeftGrip = true;
                     *           }
                     *       }
                     *       else if (body.HandLeftState == HandState.Open)
                     *       {
                     *           if (wasLeftGrip)
                     *           {
                     *               MouseControl.MouseLeftUp();
                     *               wasLeftGrip = false;
                     *           }
                     *       }
                     *   }
                     * }*/
                    else
                    {
                        wasLeftGrip       = true;
                        wasRightGrip      = true;
                        alreadyTrackedPos = false;
                    }

                    // get first tracked body only
                    break;
                }
            }
        }
        /// <summary>
        /// Read body frames
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void bodyFrameReader_FrameArrived(object sender, BodyFrameArrivedEventArgs e)
        {
            bool dataReceived = false;

            using (BodyFrame bodyFrame = e.FrameReference.AcquireFrame())
            {
                if (bodyFrame != null)
                {
                    if (this.bodies == null)
                    {
                        this.bodies = new Body[bodyFrame.BodyCount];
                    }

                    // The first time GetAndRefreshBodyData is called, Kinect will allocate each Body in the array.
                    // As long as those body objects are not disposed and not set to null in the array,
                    // those body objects will be re-used.
                    bodyFrame.GetAndRefreshBodyData(this.bodies);
                    dataReceived = true;
                }
            }

            if (!dataReceived)
            {
                alreadyTrackedPos = false;
                return;
            }

            foreach (Body body in this.bodies)
            {
                // get first tracked body only, notice there's a break below.
                if (body.IsTracked)
                {
                    // get various skeletal positions
                    CameraSpacePoint handLeft  = body.Joints[JointType.HandLeft].Position;
                    CameraSpacePoint handRight = body.Joints[JointType.HandRight].Position;
                    CameraSpacePoint spineBase = body.Joints[JointType.SpineBase].Position;
                    if (handRight.Z - spineBase.Z < -0.15f)
                    {
                        if ((handRight.X - spineBase.X >= 0.5f) && (body.HandRightState == HandState.Open))
                        {
                            System.Windows.Forms.SendKeys.SendWait("{Left}");
                        }

                        else if ((handLeft.X - spineBase.X <= 0.5f) && (body.HandLeftState == HandState.Open))
                        {
                            System.Windows.Forms.SendKeys.SendWait("{Right}");
                        }


                        else if (handRight.Z - spineBase.Z < -0.15f) // if right hand lift up
                        {
                            /* hand x calculated by this. we don't use shoulder right as a reference cause the shoulder right
                             * is usually behind the lift right hand, and the position would be inferred and unstable.
                             * because the spine base is on the left of right hand, we plus 0.05f to make it closer to the right. */
                            float x = handRight.X - spineBase.X + 0.05f;

                            /* hand y calculated by this. ss spine base is way lower than right hand, we plus 0.51f to make it
                             * higer, the value 0.51f is worked out by testing for a several times, you can set it as another one you like. */
                            float y = spineBase.Y - handRight.Y + 0.51f;
                            // get current cursor position
                            Point curPos = MouseControl.GetCursorPosition();
                            // smoothing for using should be 0 - 0.95f. The way we smooth the cusor is: oldPos + (newPos - oldPos) * smoothValue
                            float smoothing = 1 - cursorSmoothing;
                            // set cursor position
                            MouseControl.SetCursorPos((int)(curPos.X + (x * mouseSensitivity * screenWidth - curPos.X) * smoothing), (int)(curPos.Y + ((y + 0.25f) * mouseSensitivity * screenHeight - curPos.Y) * smoothing));

                            alreadyTrackedPos = true;

                            // Grip gesture
                            if (doClick && useGripGesture)
                            {
                                if (body.HandRightState == HandState.Closed)
                                {
                                    if (!wasRightGrip)
                                    {
                                        MouseControl.MouseLeftDown();
                                        wasRightGrip = true;
                                    }
                                }

                                else if (body.HandRightState == HandState.Open)
                                {
                                    if (wasRightGrip)
                                    {
                                        MouseControl.MouseLeftUp();
                                        wasRightGrip = false;
                                    }
                                }
                            }
                        }
                    }
                    else if (handLeft.Z - spineBase.Z < -0.15f) // if left hand lift forward
                    {
                        float x         = handLeft.X - spineBase.X + 0.3f;
                        float y         = spineBase.Y - handLeft.Y + 0.51f;
                        Point curPos    = MouseControl.GetCursorPosition();
                        float smoothing = 1 - cursorSmoothing;
                        MouseControl.SetCursorPos((int)(curPos.X + (x * mouseSensitivity * screenWidth - curPos.X) * smoothing), (int)(curPos.Y + ((y + 0.25f) * mouseSensitivity * screenHeight - curPos.Y) * smoothing));
                        alreadyTrackedPos = true;

                        if (doClick && useGripGesture)
                        {
                            if (body.HandLeftState == HandState.Closed)
                            {
                                if (!wasLeftGrip)
                                {
                                    MouseControl.MouseLeftDown();
                                    wasLeftGrip = true;
                                }
                            }
                            else if (body.HandLeftState == HandState.Open)
                            {
                                if (wasLeftGrip)
                                {
                                    MouseControl.MouseLeftUp();
                                    wasLeftGrip = false;
                                }
                            }
                        }
                    }
                    else
                    {
                        wasLeftGrip       = true;
                        wasRightGrip      = true;
                        alreadyTrackedPos = false;
                    }

                    // get first tracked body only
                    break;
                }
            }
        }