Search code examples
unity-game-enginekinect

I want to get user's height by using kinect V2 and unity3D,this code can't work well what should I do?


Attempting to get the user's height in Unity using the xbox kinect.
Below is my code and I cannot get the height.
This uses the KinectV2 interface.

// get User's height by KinectV2 and unity3D `enter code here`   
float  GetUserHeightByLeft(long userid)
{`enter code here`
    float uheight=0.0f;
    int[] joints = new int[9];
    int head = (int)KinectInterop.JointType.Head;
    joints[0] = head;
    joints[1] = (int)KinectInterop.JointType.Neck;
    int shoudlderCenter = (int)KinectInterop.JointType.SpineShoulder;
    joints[2] = shoudlderCenter;
    joints[3] = (int)KinectInterop.JointType.SpineMid;
    joints[4] = (int)KinectInterop.JointType.SpineBase;
    joints[5] = (int)KinectInterop.JointType.HipLeft;
    joints[6] = (int)KinectInterop.JointType.KneeLeft;
    joints[7] = (int)KinectInterop.JointType.AnkleLeft;
    joints[8] = (int)KinectInterop.JointType.FootLeft;
    int trackedcount = 0;
    for (int i = 0; i < joints.Length; ++i)
    {
        if (KinectManager.Instance.IsJointTracked(userid, joints[i]))
        {
            ++trackedcount;
        }
    }
    //if all joints that I need have been tracked ,compute user's height
    if (trackedcount == joints.Length)
    {
        for (int i = 0; i < joints.Length-1;++i)
        {
            if (KinectManager.Instance.IsJointTracked(userid, joints[i]))
            {
                Vector3 start= 100*KinectManager.Instance.GetJointKinectPosition(userid,joints[i]);
                Vector3 end = 100*KinectManager.Instance.GetJointKinectPosition(userid,joints[i+1]);
                uheight += Mathf.Abs(Vector3.Magnitude(end-start));
            }
        }
        //some height kinectV2 can't get  so I add it
        uheight += 8;
    }
        return uheight;
}

Solution

  • Given your code I do see a few issues

    1. The summation to get the total height requires all joints to be tracked at that time.
      • Why do you need all joints to be tracked for the height to be tracked? You should only need the head and the feet
    2. You double check if each joint is tracked
    3. using the left for every foot/knee/hip joint is going to give you math errors. They are offset in one direction (because our feet aren't in the center of our body)
      • I would track both right and left foot/knee/hip and then find the center of the two on the x axis.
    4. If you're using the magnitude of two vectors and one knee is far in front of your hip it's going to give it an inflated value.
      • I would only use the y positions of your kinect joints to calculate the height.