none
I cant display my tracked skeleton RRS feed

  • Question

  • Below is the code. Basically when i run the application it does track the skeleton that is infront of the kinect and the code works, but when i add in "this.DrawBonesAndJoints(skeleton, dc);" the program breaks and error indicated at:
     Joint joint0 = skeleton.Joints[jointType0];
     Joint joint1 = skeleton.Joints[jointType1];

    private int CurrentTrackingId = 0;
    private Skeleton[] skeletons = new Skeleton[0];
    
    Skeleton FindSkeleton(SkeletonFrameReadyEventArgs e)
    {
        Skeleton skeleton = null;
    
        using (SkeletonFrame skeletonFrame = e.OpenSkeletonFrame())
        {
            if (skeletonFrame != null)
            {
                skeletons = new Skeleton[skeletonFrame.SkeletonArrayLength];
                skeletonFrame.CopySkeletonDataTo(skeletons);
            }
    
            using (DrawingContext dc = this.drawingGroup.Open())
            {
                // Draw a transparent background to set the render size
                dc.DrawRectangle(Brushes.Black, null, new Rect(160, 0.0, RenderWidth, RenderHeight));
    
                if (CurrentTrackingId != 0)
                {
                    // Tracked someone
                    skeleton =
                        (from s in skeletons
                         where s.TrackingState == SkeletonTrackingState.Tracked &&
                               s.Joints[JointType.Head].TrackingState == JointTrackingState.Tracked &&
                               s.TrackingId == CurrentTrackingId
                         select s).FirstOrDefault();
                    
                        this.DrawBonesAndJoints(skeleton, dc);
    
                        PatientTextblock.Text = "Patient on Bed";
                        StatusTextblock.Background = new SolidColorBrush(Colors.White);
                        StatusTextblock.Foreground = new SolidColorBrush(Colors.Black);
    //
                        if (skeleton == null)
                    {
                        // Patient is gone!
                        PatientTextblock.Text = "PATIENT LEAVING BED!";
                        StatusTextblock.Background = new SolidColorBrush(Colors.White);
                        StatusTextblock.Foreground = new SolidColorBrush(Colors.Black);
                        Process.Start("wmplayer.exe", "C:\\Users\\User\\Downloads\\PatientLeavingBed.wma");
                        CurrentTrackingId = 0;
                        sensor.SkeletonStream.AppChoosesSkeletons = false;
                    }
                }
                else
                {
                    // Try to find someone new
    
                    skeleton =
                        (from s in skeletons
                         where s.TrackingState == SkeletonTrackingState.Tracked &&
                               s.Joints[JointType.Head].TrackingState == JointTrackingState.Tracked
                         select s).FirstOrDefault();
    
                    if (skeleton != null)
                    {
                        // Found one; start tracking them
    
                        CurrentTrackingId = skeleton.TrackingId;
                        sensor.SkeletonStream.AppChoosesSkeletons = true;
                        sensor.SkeletonStream.ChooseSkeletons(CurrentTrackingId);
                    }
                }
                // prevent drawing outside of our render area
                this.drawingGroup.ClipGeometry = new RectangleGeometry(new Rect(160, 0.0, RenderWidth, RenderHeight));
            }
        }
    
        return skeleton;
    }

    Is there any problem with my code? I just need to display the first tracked skeleton the kinect tracked. I do not want to track the rest of the skeleton that comes after the first tracked skeleton.

    Thanks!

    Zong Li

    Wednesday, April 16, 2014 10:32 AM

All replies

  • Li,

    What is the error message?

    James


    James Ashley - Presentation Layer Architect at Razorfish Emerging Experiences
    jamesashley@imaginativeuniversal.com
    www.imaginativeuniversal.com www.emergingexperiences.com

    Wednesday, April 16, 2014 3:32 PM
  • Hi James thanks for taking your time to help.

    Sorry i do not have my kinect with me now but i can describe the problem.

    Basically when i added the "this.DrawBonesAndJoints(skeleton, dc);" into the codes above the kinect does track the skeleton and its displayed on the application screen i created.

    But when the skeleton leaves the frame or any joints not detected(skeleton not tracked), the application breaks and it will indicate at here,

     Joint joint0 = skeleton.Joints[jointType0];
     Joint joint1 = skeleton.Joints[jointType1];

    Below are the  codes i used from the Kinect SDK skeleton basics.

    Thanks again!

    Zong Li

      private void DrawBonesAndJoints(Skeleton skeleton, DrawingContext drawingContext)
            {
               
                //Render Torso
                this.DrawBone(skeleton, drawingContext, JointType.Head, JointType.ShoulderCenter);
                this.DrawBone(skeleton, drawingContext, JointType.ShoulderCenter, JointType.ShoulderLeft);
                this.DrawBone(skeleton, drawingContext, JointType.ShoulderCenter, JointType.ShoulderRight);
                this.DrawBone(skeleton, drawingContext, JointType.ShoulderCenter, JointType.Spine);
                this.DrawBone(skeleton, drawingContext, JointType.Spine, JointType.HipCenter);
                this.DrawBone(skeleton, drawingContext, JointType.HipCenter, JointType.HipLeft);
                this.DrawBone(skeleton, drawingContext, JointType.HipCenter, JointType.HipRight);
    
                // Left Arm
                this.DrawBone(skeleton, drawingContext, JointType.ShoulderLeft, JointType.ElbowLeft);
                this.DrawBone(skeleton, drawingContext, JointType.ElbowLeft, JointType.WristLeft);
                this.DrawBone(skeleton, drawingContext, JointType.WristLeft, JointType.HandLeft);
    
                // Right Arm
                this.DrawBone(skeleton, drawingContext, JointType.ShoulderRight, JointType.ElbowRight);
                this.DrawBone(skeleton, drawingContext, JointType.ElbowRight, JointType.WristRight);
                this.DrawBone(skeleton, drawingContext, JointType.WristRight, JointType.HandRight);
    
                // Left Leg
                this.DrawBone(skeleton, drawingContext, JointType.HipLeft, JointType.KneeLeft);
                this.DrawBone(skeleton, drawingContext, JointType.KneeLeft, JointType.AnkleLeft);
                this.DrawBone(skeleton, drawingContext, JointType.AnkleLeft, JointType.FootLeft);
    
                // Right Leg
                this.DrawBone(skeleton, drawingContext, JointType.HipRight, JointType.KneeRight);
                this.DrawBone(skeleton, drawingContext, JointType.KneeRight, JointType.AnkleRight);
                this.DrawBone(skeleton, drawingContext, JointType.AnkleRight, JointType.FootRight);
     
                // Render Joints
                foreach (Joint joint in skeleton.Joints)
                {
                    Brush drawBrush = null;
    
                    if (joint.TrackingState == JointTrackingState.Tracked)
                    {
                        drawBrush = this.trackedJointBrush;                    
                    }
                    else if (joint.TrackingState == JointTrackingState.Inferred)
                    {
                        drawBrush = this.inferredJointBrush;                    
                    }
    
                    if (drawBrush != null)
                    {
                        drawingContext.DrawEllipse(drawBrush, null, this.SkeletonPointToScreen(joint.Position), JointThickness, JointThickness);
                    }
                }
            }
    
            /// <summary>
            /// Maps a SkeletonPoint to lie within our render space and converts to Point
            /// </summary>
            /// <param name="skelpoint">point to map</param>
            /// <returns>mapped point</returns>
            private Point SkeletonPointToScreen(SkeletonPoint skelpoint)
            {
                //Bitmap bitmap = new Bitmap(100, 100);
                //bitmap.SetResolution(96.0F, 96.0F);
                // Convert point to depth space.  
                // We are not using depth directly, but we do want the points in our 640x480 output resolution.                      Resolution640x480Fps30
                DepthImagePoint depthPoint = this.sensor.CoordinateMapper.MapSkeletonPointToDepthPoint(skelpoint, DepthImageFormat.Resolution640x480Fps30);
                return new Point(depthPoint.X, depthPoint.Y);
            }
    
    
            /// <summary>
            /// Draws a bone line between two joints
            /// </summary>
            /// <param name="skeleton">skeleton to draw bones from</param>
            /// <param name="drawingContext">drawing context to draw to</param>
            /// <param name="jointType0">joint to start drawing from</param>
            /// <param name="jointType1">joint to end drawing at</param>
            ///  private void DrawBone(Skeleton skeleton, DrawingContext drawingContext, JointType jointType0, JointType jointType1)
            private void DrawBone(Skeleton skeleton, DrawingContext drawingContext, JointType jointType0, JointType jointType1)
            {
    
                Joint joint0 = skeleton.Joints[jointType0];
                Joint joint1 = skeleton.Joints[jointType1];
    
                // If we can't find either of these joints, exit
                if (joint0.TrackingState == JointTrackingState.NotTracked ||
                    joint1.TrackingState == JointTrackingState.NotTracked)
                {
                    return;
                }
    
                // Don't draw if both points are inferred
                if (joint0.TrackingState == JointTrackingState.Inferred &&
                    joint1.TrackingState == JointTrackingState.Inferred)
                {
                    return;
                }
    
                // We assume all drawn bones are inferred unless BOTH joints are tracked
                Pen drawPen = this.inferredBonePen;
                if (joint0.TrackingState == JointTrackingState.Tracked && joint1.TrackingState == JointTrackingState.Tracked)
                {
                    drawPen = this.trackedBonePen;
                }
    
                drawingContext.DrawLine(drawPen, this.SkeletonPointToScreen(joint0.Position), this.SkeletonPointToScreen(joint1.Position));
            }

    Saturday, April 19, 2014 3:41 AM
  • If you are not tracking a skeleton why are you trying to draw its skeleton? Is the skeleton object null?

    Carmine Sirignano - MSFT

    Wednesday, April 23, 2014 12:00 AM
  • Basically the skeleton is tracked by the kinect but im not able to draw the skeleton as the code will break once the tracked skeleton leaves the resolution of the kinect (640x480).

    The code works but after adding in this.DrawBoneAndJoints(skeleton, dc); the skeleton is drawn but the application breaks once the skeleton leaves the scene. How should i do it so that when the tracked skeleton becomes null(leaves the scene), my application will not break.

    Zong li

        if (CurrentTrackingId != 0)
                {
                    // Tracked someone
                    skeleton =
                        (from s in skeletons
                         where s.TrackingState == SkeletonTrackingState.Tracked &&
                               s.Joints[JointType.Head].TrackingState == JointTrackingState.Tracked &&
                               s.TrackingId == CurrentTrackingId
                         select s).FirstOrDefault();
                    
                        this.DrawBonesAndJoints(skeleton, dc);
    
                        PatientTextblock.Text = "Patient on Bed";
                        StatusTextblock.Background = new SolidColorBrush(Colors.White);
                        StatusTextblock.Foreground = new SolidColorBrush(Colors.Black);
    //
                        if (skeleton == null)
                    {
                        // Patient is gone!
                        PatientTextblock.Text = "PATIENT LEAVING BED!";
                        StatusTextblock.Background = new SolidColorBrush(Colors.White);
                        StatusTextblock.Foreground = new SolidColorBrush(Colors.Black);
                        Process.Start("wmplayer.exe", "C:\\Users\\User\\Downloads\\PatientLeavingBed.wma");
                        CurrentTrackingId = 0;
                        sensor.SkeletonStream.AppChoosesSkeletons = false;
                    }
                }


    Wednesday, April 23, 2014 4:00 AM
  • If the skeleton leaves the camera field of view it is no longer tracked. So how is your code checking to ensure the Skeleton object you are passing is not null?

    Carmine Sirignano - MSFT

    Wednesday, April 23, 2014 6:04 PM