none
Trouble Displaying Skeleton in Main Window RRS feed

  • Question

  • Okay I'm fully ready to laugh at here, but I can't seem to get this to work. I have been reading the book "Beginning Kinect Programming" by Jarrett Webb and James Ashley. I am up to Skeleton tracking and the code below is from the book and their website. However I can't seem to get it to work. It complies and runs, but no skeleton is displayed in the main window. I know I missing something, but I don't know what.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Data;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Imaging;
    using System.Windows.Navigation;
    using System.Windows.Shapes;
    
    using Microsoft.Kinect;
    using System.IO;
    
    
    
    
    namespace SkeletonTrackingProgram
    {
        /// <summary>
        /// Interaction logic for MainWindow.xaml
        /// </summary>
        public partial class MainWindow : Window
        {
            #region Member Variables
            private KinectSensor _KinectDevice;
            private readonly Brush[] _SkeletonBrushes;
            private Skeleton[] _FrameSkeletons;
            #endregion Member Variables
    
            #region Constructor
            public MainWindow()
            {
                InitializeComponent();
    
                _SkeletonBrushes = new Brush[] { Brushes.Black, Brushes.Crimson, Brushes.Indigo, Brushes.DodgerBlue, Brushes.Purple, Brushes.Pink };
    
                KinectSensor.KinectSensors.StatusChanged += KinectSensors_StatusChanged;
                this.KinectDevice = KinectSensor.KinectSensors.FirstOrDefault(x => x.Status == KinectStatus.Connected);
            }
            #endregion Constructor
    
            #region Methods
            private void KinectSensors_StatusChanged(object sender, StatusChangedEventArgs e)
            {
                switch (e.Status)
                {
                    case KinectStatus.Initializing:
                    case KinectStatus.Connected:
                    case KinectStatus.NotPowered:
                    case KinectStatus.NotReady:
                    case KinectStatus.DeviceNotGenuine:
                        this.KinectDevice = e.Sensor;
                        break;
                    case KinectStatus.Disconnected:
                        //TODO: Give the user feedback to plug-in a Kinect device.                    
                        this.KinectDevice = null;
                        break;  
                    default:
                        //TODO: Show an error state
                        Console.WriteLine("/n***ERROR IN STATUS CHANGE***");
                        break;
                }
            }
    
    
            // Listing 4-2
            private void KinectDevice_SkeletonFrameReady(object sender, SkeletonFrameReadyEventArgs e)
            {
                using (SkeletonFrame frame = e.OpenSkeletonFrame())
                {
                    if (frame != null)
                    {
                        Polyline figure;
                        Brush userBrush;
                        Skeleton skeleton;
    
    
                        LayoutRoot.Children.Clear();
                        frame.CopySkeletonDataTo(this._FrameSkeletons);
    
                        Skeleton[] dataSet2 = new Skeleton[this._FrameSkeletons.Length];
                        frame.CopySkeletonDataTo(dataSet2);
    
    
                        for (int i = 0; i < this._FrameSkeletons.Length; i++)
                        {
                            skeleton = this._FrameSkeletons[i];
    
                            if (skeleton.TrackingState == SkeletonTrackingState.Tracked)
                            {
                                userBrush = this._SkeletonBrushes[i % this._SkeletonBrushes.Length];
    
                                //Draw head and torso
                                figure = CreateFigure(skeleton, userBrush, new[] { JointType.Head, JointType.ShoulderCenter, JointType.ShoulderLeft, JointType.Spine,
                                                                                        JointType.ShoulderRight, JointType.ShoulderCenter, JointType.HipCenter
                                                                                        });
                                LayoutRoot.Children.Add(figure);
    
                                figure = CreateFigure(skeleton, userBrush, new[] { JointType.HipLeft, JointType.HipRight });
                                LayoutRoot.Children.Add(figure);
    
                                //Draw left leg
                                figure = CreateFigure(skeleton, userBrush, new[] { JointType.HipCenter, JointType.HipLeft, JointType.KneeLeft, JointType.AnkleLeft, JointType.FootLeft });
                                LayoutRoot.Children.Add(figure);
    
                                //Draw right leg
                                figure = CreateFigure(skeleton, userBrush, new[] { JointType.HipCenter, JointType.HipRight, JointType.KneeRight, JointType.AnkleRight, JointType.FootRight });
                                LayoutRoot.Children.Add(figure);
    
                                //Draw left arm
                                figure = CreateFigure(skeleton, userBrush, new[] { JointType.ShoulderLeft, JointType.ElbowLeft, JointType.WristLeft, JointType.HandLeft });
                                LayoutRoot.Children.Add(figure);
    
                                //Draw right arm
                                figure = CreateFigure(skeleton, userBrush, new[] { JointType.ShoulderRight, JointType.ElbowRight, JointType.WristRight, JointType.HandRight });
                                LayoutRoot.Children.Add(figure);
                            }
                        }
                    }
                }
            }
    
    
            // Listing 4-3
            private Polyline CreateFigure(Skeleton skeleton, Brush brush, JointType[] joints)
            {
                Polyline figure = new Polyline();
    
                figure.StrokeThickness = 8;
                figure.Stroke = brush;
    
                for (int i = 0; i < joints.Length; i++)
                {
                    figure.Points.Add(GetJointPoint(skeleton.Joints[joints[i]]));
                }
                //This is getting called.
                //Console.WriteLine("\n***CreateFigure Complete***");
                return figure;
            }
    
    
            private Point GetJointPoint(Joint joint)
            {
                DepthImagePoint point = this.KinectDevice.MapSkeletonPointToDepth(joint.Position, this.KinectDevice.DepthStream.Format);
                point.X *= (int)this.LayoutRoot.ActualWidth / KinectDevice.DepthStream.FrameWidth;
                point.Y *= (int)this.LayoutRoot.ActualHeight / KinectDevice.DepthStream.FrameHeight;
    
                return new Point(point.X, point.Y);
            }
            #endregion Methods
    
            #region Properties
            public KinectSensor KinectDevice
            {
                get { return this._KinectDevice; }
                set
                {
                    if (this._KinectDevice != value)
                    {
                        //Uninitialize
                        if (this._KinectDevice != null)
                        {
                            this._KinectDevice.Stop();
                            this._KinectDevice.SkeletonFrameReady -= KinectDevice_SkeletonFrameReady;
                            this._KinectDevice.SkeletonStream.Disable();
                            this._FrameSkeletons = null;
                        }
    
                        this._KinectDevice = value;
    
                        //Initialize
                        if (this._KinectDevice != null)
                        {
                            if (this._KinectDevice.Status == KinectStatus.Connected)
                            {
                                this._KinectDevice.SkeletonStream.Enable();
                                Console.WriteLine("\n***SkeletonStream Enabled.***");
                                this._FrameSkeletons = new Skeleton[this._KinectDevice.SkeletonStream.FrameSkeletonArrayLength];
                                this.KinectDevice.SkeletonFrameReady += KinectDevice_SkeletonFrameReady;
                                this._KinectDevice.Start();
                                Console.WriteLine("Kinect Divice Started.");
                            }
                        }
                    }
                }
            }
            #endregion Properties
    
    
        }
    }

    Here is the xmal window code:

    <Window x:Class="SkeletonTrackingProgram.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            Title="MainWindow" Height="350" Width="525">
        <Grid x:Name="LayoutRoot" Background="White">
            
        </Grid>
    </Window>

    Thanks for the help.


    • Edited by Achemil Friday, October 5, 2012 6:50 PM
    Friday, October 5, 2012 6:49 PM

All replies

  • I had a similar experience with this skeleton sample from the book.  I started to troubleshoot it, but because I was on a deadline, I decided abandon the code in the book and start from the Skeleton Basics sample code in the SDK instead, since that code always works for me.  Someday when I have lots of time, I might go back and try to figure out what's going wrong in the book's sample, but I'll probably just continue to use the SDK sample as known working code.
    • Edited by kengr Tuesday, October 9, 2012 9:15 PM
    Sunday, October 7, 2012 9:42 PM
  • Thanks, I have been having mixed results with this books code so far. Sometimes it works perfectly and other times not at all. 
    Tuesday, October 9, 2012 8:41 PM