0
votes

Here is my code so far. It is very basic as I'm just learning how to code with the Kinect.

    KinectSensor Sensor = null;

    private void SensorSkeletonFrameReady(object sender, SkeletonFrameReadyEventArgs e)
    {
        Skeleton[] skeletons = new Skeleton[0];

        using (SkeletonFrame skeletonFrame = e.OpenSkeletonFrame())
        {
            if (skeletonFrame != null)
            {
                skeletons = new Skeleton[skeletonFrame.SkeletonArrayLength];
                skeletonFrame.CopySkeletonDataTo(skeletons);
            }
        }

        // Draw the skeleton.
        if (skeletons.Length > 0)
        {
            drawSkeleton(skeletons[0]);
        }
    }

    private void drawSkeleton(Skeleton skeleton)
    {
        // Dispose of the current image if applicable.
        if (pctSkeleton.Image != null)
        {
            pctSkeleton.Image.Dispose();
        }

        Image image = Image.FromFile(CanvasPath);

        using (Graphics g = Graphics.FromImage(image))
        {
            // If any joints aren't tracked, return.
            foreach (Joint joint in skeleton.Joints)
            {
                if (joint.TrackingState == JointTrackingState.NotTracked)
                {
                    return;
                }
            }

            // Sort the 20 joints.
            Joint head = skeleton.Joints[JointType.Head];
            Joint hipCenter = skeleton.Joints[JointType.HipCenter];
            Joint spine = skeleton.Joints[JointType.Spine];
            Joint shoulderCenter = skeleton.Joints[JointType.ShoulderCenter];
            Joint shoulderLeft = skeleton.Joints[JointType.ShoulderLeft];
            Joint elbowLeft = skeleton.Joints[JointType.ElbowLeft];
            Joint wristLeft = skeleton.Joints[JointType.WristLeft];
            Joint handLeft = skeleton.Joints[JointType.HandLeft];
            Joint shoulderRight = skeleton.Joints[JointType.ShoulderRight];
            Joint elbowRight = skeleton.Joints[JointType.ElbowRight];
            Joint wristRight = skeleton.Joints[JointType.WristRight];
            Joint handRight = skeleton.Joints[JointType.HandRight];
            Joint hipLeft = skeleton.Joints[JointType.HipLeft];
            Joint kneeLeft = skeleton.Joints[JointType.KneeLeft];
            Joint ankleLeft = skeleton.Joints[JointType.AnkleLeft];
            Joint footLeft = skeleton.Joints[JointType.FootLeft];
            Joint hipRight = skeleton.Joints[JointType.HipRight];
            Joint kneeRight = skeleton.Joints[JointType.KneeRight];
            Joint ankleRight = skeleton.Joints[JointType.AnkleRight];
            Joint footRight = skeleton.Joints[JointType.FootRight];

            Pen inBoundPen = new Pen(Brushes.Green, 3);

            ///////////////////DRAW BONES////////////////////////
            // head => shoulder center.
            g.DrawLine(inBoundPen, convertX(head.Position.X), convertY(head.Position.Y), convertX(shoulderCenter.Position.X), convertY(shoulderCenter.Position.Y));

            // shoulders => shoulder center.
            g.DrawLine(inBoundPen, convertX(shoulderRight.Position.X), convertY(shoulderRight.Position.Y), convertX(shoulderCenter.Position.X), convertY(shoulderCenter.Position.Y));
            g.DrawLine(inBoundPen, convertX(shoulderLeft.Position.X), convertY(shoulderLeft.Position.Y), convertX(shoulderCenter.Position.X), convertY(shoulderCenter.Position.Y));

            // shoulder center => spine.
            g.DrawLine(inBoundPen, convertX(shoulderCenter.Position.X), convertY(shoulderCenter.Position.Y), convertX(spine.Position.X), convertY(spine.Position.Y));

            // shoulder right => elbow right
            g.DrawLine(inBoundPen, convertX(shoulderRight.Position.X), convertY(shoulderRight.Position.Y), convertX(elbowRight.Position.X), convertY(elbowRight.Position.Y));

            // elbow right => wrist right
            g.DrawLine(inBoundPen, convertX(elbowRight.Position.X), convertY(elbowRight.Position.Y), convertX(wristRight.Position.X), convertY(wristRight.Position.Y));

            // wrist right => hand right
            g.DrawLine(inBoundPen, convertX(wristRight.Position.X), convertY(wristRight.Position.Y), convertX(handRight.Position.X), convertY(handRight.Position.Y));

            // shoulder left => elbow left
            g.DrawLine(inBoundPen, convertX(shoulderLeft.Position.X), convertY(shoulderLeft.Position.Y), convertX(elbowLeft.Position.X), convertY(elbowLeft.Position.Y));

            // elbow left => wrist left
            g.DrawLine(inBoundPen, convertX(elbowLeft.Position.X), convertY(elbowLeft.Position.Y), convertX(wristLeft.Position.X), convertY(wristLeft.Position.Y));

            // wrist left => hand left
            g.DrawLine(inBoundPen, convertX(wristLeft.Position.X), convertY(wristLeft.Position.Y), convertX(handLeft.Position.X), convertY(handLeft.Position.Y));

            // spine => hip center.
            g.DrawLine(inBoundPen, convertX(spine.Position.X), convertY(spine.Position.Y), convertX(hipCenter.Position.X), convertY(hipCenter.Position.Y));

            // hips => hip center.
            g.DrawLine(inBoundPen, convertX(hipCenter.Position.X), convertY(hipCenter.Position.Y), convertX(hipLeft.Position.X), convertY(hipLeft.Position.Y));
            g.DrawLine(inBoundPen, convertX(hipCenter.Position.X), convertY(hipCenter.Position.Y), convertX(hipRight.Position.X), convertY(hipRight.Position.Y));

            // hip left => knee left.
            g.DrawLine(inBoundPen, convertX(hipLeft.Position.X), convertY(hipLeft.Position.Y), convertX(kneeLeft.Position.X), convertY(kneeLeft.Position.Y));

            // knee left => ankle left.
            g.DrawLine(inBoundPen, convertX(kneeLeft.Position.X), convertY(kneeLeft.Position.Y), convertX(ankleLeft.Position.X), convertY(ankleLeft.Position.Y));

            // ankle left => foot left.
            g.DrawLine(inBoundPen, convertX(ankleLeft.Position.X), convertY(ankleLeft.Position.Y), convertX(footLeft.Position.X), convertY(footLeft.Position.Y));

            // hip right => knee right.
            g.DrawLine(inBoundPen, convertX(hipRight.Position.X), convertY(hipRight.Position.Y), convertX(kneeRight.Position.X), convertY(kneeRight.Position.Y));

            // knee right => ankle right.
            g.DrawLine(inBoundPen, convertX(kneeRight.Position.X), convertY(kneeRight.Position.Y), convertX(ankleRight.Position.X), convertY(ankleRight.Position.Y));

            // ankle right => foot right.
            g.DrawLine(inBoundPen, convertX(ankleRight.Position.X), convertY(ankleRight.Position.Y), convertX(footRight.Position.X), convertY(footRight.Position.Y));
            ////////////////////////////////////////////////

            pctSkeleton.Image = image;
        }
    }

    private float convertX(float x)
    {
        return (pctSkeleton.Width / 2) + (x * 100);
    }

    private float convertY(float y)
    {
        return (pctSkeleton.Height / 2) - (y * 100);
    }

    private void button2_Click(object sender, EventArgs e)
    {
        // Get the first sensor
        Sensor = KinectSensor.KinectSensors[0];

        // Enable the skeleton and video streams.
        Sensor.SkeletonStream.Enable();
        Sensor.ColorStream.Enable(ColorImageFormat.RgbResolution640x480Fps30);

        // Set the event handlers
        Sensor.SkeletonFrameReady += SensorSkeletonFrameReady;
        Sensor.ColorFrameReady += SensorColorFrameReady;

        // Start the kinect.
        Sensor.Start();
    }

For some reason, the sensor isn't tracking the skeleton most of the time. It works about 20% of the time however. When setting breakpoints, it shows the joints as not tracked. I am new at this, so any information as to what I'm doing wrong would be greatly appreciated.

1

1 Answers

1
votes

Your application draws a skeleton only, when all 20 joints are tracked. If only a single joint is not tracked (for example the left ankle), your application does nothing. It is quite rare, that the sensor can detect every joint.

In the kinect developer toolkit you can find a sample named "SkeletonBasics-WPF" that is more forgiving when a joint is not tracked.