4
votes

I've been working on this for quite a while now, and I cannot find why (most times) the HandJoint isn't tracked. The code is adapted to avoid the situation when this happens (that's why all the nulls and nullables are there). I used C# with Kinect SDK v1.8.

I've never posted here before, so EVERY comment is apreciated.

 using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Text;
 using Microsoft.Kinect;
 using System.Windows;

 namespace Arcade_Kinect.Kinect
 {
    class KinectAddons
    {
    public KinectSensor kinect;
    public Point? punto;
    public JointType hand;

    public KinectAddons(bool leftHanded, KinectSensor sensor)
    {
        this.kinect = sensor;
        if (kinect != null)
        {
            kinect.SkeletonFrameReady += new EventHandler<SkeletonFrameReadyEventArgs>(kinect_SkeletonFrameReady);
            kinect.Start();
            try
            {
                kinect.DepthStream.Enable(DepthImageFormat.Resolution640x480Fps30);
                kinect.SkeletonStream.Enable();
                try
                {
                    kinect.DepthStream.Range = DepthRange.Near;
                    kinect.SkeletonStream.EnableTrackingInNearRange = true;
                }
                catch (InvalidOperationException)
                {

                    kinect.DepthStream.Range = DepthRange.Default;
                    kinect.SkeletonStream.EnableTrackingInNearRange = false;
                }
            }
            catch (InvalidOperationException) { }
        }
        if (leftHanded)
            this.hand = JointType.HandLeft;
        else
            this.hand = JointType.HandRight;

    }

    void kinect_SkeletonFrameReady(object sender, SkeletonFrameReadyEventArgs e)
    {
        Skeleton[] skeleton = new Skeleton[0];

        using (SkeletonFrame sf = e.OpenSkeletonFrame())
        {
            if (sf != null)
            {
                skeleton = new Skeleton[sf.SkeletonArrayLength];
                sf.CopySkeletonDataTo(skeleton);
                punto = returnPosFromHand(skeleton[0]);
            }
        }
    }

    //saves x and y of the hand
    public Point? returnPosFromHand(Skeleton sk)
    {
        DepthImagePoint depthPoint = new DepthImagePoint();
        bool notWorking = true;
        try
        {
            if (sk.Joints[this.hand].TrackingState == JointTrackingState.Tracked)
            {
                notWorking = false;
                depthPoint = this.kinect.CoordinateMapper.MapSkeletonPointToDepthPoint(sk.Joints[this.hand].Position, DepthImageFormat.Resolution640x480Fps30);
            }
        }
        catch (IndexOutOfRangeException)
        {
            System.Console.WriteLine("Hand capture was lost" + this.hand.ToString());
        }
        catch (Exception e)
        {
            MessageBox.Show(e.Message);
        }
        if (notWorking)
            return null;
        return new Point(depthPoint.X, depthPoint.Y);
    }

        //returns last know value
        public double? readNext()
        {
            if (this.punto.HasValue)
                return punto.Value.X;
            return null;
        }
    }
}
1

1 Answers

2
votes

Problem

Your problems is with getting your skeletons from the SkeletonFrame

SkeletonFrame.OpenSkeletonFrame() always return a array with X-objects. The person tracked isn't necessarily the first Skeleton in the array.

That why you get your hand tracking sometimes. The sometimes is when the tracked skeleton is the first in list (returnPosFromHand(skeleton[0]);)

Solution

Iterate over the skeleton array and check which skeleton is tracked and use that one. You can order by Z-index and maybe pick the closest one with the sensor. Then pass that skeleton to your function.