none
Kinect not tracking Skelton RRS feed

  • Question

  • 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.Shapes; using Microsoft.Kinect; using Microsoft.Kinect.Toolkit; using Microsoft.Kinect.Toolkit.Controls; using Coding4Fun.Kinect.Wpf; namespace KinectStrokeRecovery { /// <summary> /// Interaction logic for SessionMenu.xaml /// </summary> public partial class SessionMenu : Window { private KinectSensorChooser sensorChooser; public SessionMenu() { InitializeComponent(); Loaded += OnLoaded; } private void OnLoaded(object sender, RoutedEventArgs routedEventArgs) { this.sensorChooser = new KinectSensorChooser(); this.sensorChooser.KinectChanged += SensorChooserOnKinectChanged; this.sensorChooserUi.KinectSensorChooser = this.sensorChooser; this.sensorChooser.Start(); //fill scroll content /* for (int i = 1; i < 20; i++) { var button = new KinectCircleButton { Content = i, Height = 200 }; int i1 = i; button.Click += (o, args) => MessageBox.Show("You clicked button #" + i1); scrollContent.Children.Add(button); }*/ } private void SensorChooserOnKinectChanged(object sender, KinectChangedEventArgs args) { bool error = false; if (args.OldSensor != null) { try { args.OldSensor.DepthStream.Range = DepthRange.Default; args.OldSensor.SkeletonStream.EnableTrackingInNearRange = false; args.OldSensor.DepthStream.Disable(); args.OldSensor.SkeletonStream.Disable(); } catch (InvalidOperationException) { // KinectSensor might enter an invalid state while enabling/disabling streams or stream features. // E.g.: sensor might be abruptly unplugged. error = true; } } if (args.NewSensor != null) { try { args.NewSensor.DepthStream.Enable(DepthImageFormat.Resolution640x480Fps30); args.NewSensor.SkeletonStream.Enable(); /* try { args.NewSensor.DepthStream.Range = DepthRange.Near; args.NewSensor.SkeletonStream.EnableTrackingInNearRange = true; args.NewSensor.SkeletonStream.TrackingMode = SkeletonTrackingMode.Seated; } catch (InvalidOperationException) { // Non Kinect for Windows devices do not support Near mode, so reset back to default mode. args.NewSensor.DepthStream.Range = DepthRange.Default; args.NewSensor.SkeletonStream.EnableTrackingInNearRange = false; error = true; }*/ } catch (InvalidOperationException) { error = true; // KinectSensor might enter an invalid state while enabling/disabling streams or stream features. // E.g.: sensor might be abruptly unplugged. } } if (!error) kinectRegion.KinectSensor = args.NewSensor; } private void TrackButtonOnClick(object sender, RoutedEventArgs e) { sensorChooser.Stop(); Exercise ex = new Exercise(); this.Close(); ex.Show(); } } }

    SessionMenu.Xaml.cs

    First i am running above code, when i clicked on TrackButtonClick() event fire new page open but its not working , i mean not tracking the skeleton.  
    Exercise.Xaml.cs

    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.Shapes;
    using Microsoft.Kinect;
    using Microsoft.Kinect.Toolkit;
    using Microsoft.Kinect.Toolkit.Controls;
    using Coding4Fun.Kinect.Wpf;
    
    namespace KinectStrokeRecovery
    {
        /// <summary>
        /// Interaction logic for Exercise.xaml
        /// </summary>
        public partial class Exercise : Window
        {
           KinectSensor sensor = KinectSensor.KinectSensors[0];
            
            Skeleton[] skeletons;
            public Exercise()
            {
                InitializeComponent();
                
                sensor.SkeletonStream.Enable();
                sensor.SkeletonFrameReady += TrackSkeleton;
                sensor.Start();
            }
            void  TrackSkeleton(object sender, SkeletonFrameReadyEventArgs e) 
            {
                bool receivedData = false;
    
                using (SkeletonFrame SFrame = e.OpenSkeletonFrame())
                {
                    if (SFrame == null)
                    {
                        
                    }
                    else
                    {
    
                        skeletons = new Skeleton[SFrame.SkeletonArrayLength];
                        SFrame.CopySkeletonDataTo(skeletons);
                        receivedData = true;
                    }
                }
    
                if (receivedData)
                {
                    // sub query expression .
                    Skeleton currentSkeleton = (from skl in skeletons
                                                where skl.TrackingState == SkeletonTrackingState.Tracked
                                                select skl).FirstOrDefault();
    
                    if (currentSkeleton != null)
                    {
                        SetEllipsePosition(head, currentSkeleton.Joints[JointType.Head]);
                        SetEllipsePosition(leftHand, currentSkeleton.Joints[JointType.HandLeft]);
                        SetEllipsePosition(rightHand, currentSkeleton.Joints[JointType.HandRight]);
                        SetEllipsePosition(ElboLeft, currentSkeleton.Joints[JointType.ElbowLeft]);
                        SetEllipsePosition(ElboRight, currentSkeleton.Joints[JointType.ElbowRight]);
                        SetEllipsePosition(WristRight, currentSkeleton.Joints[JointType.WristRight]);
                        SetEllipsePosition(WristLeft, currentSkeleton.Joints[JointType.WristLeft]);
                        SetEllipsePosition(ShoulderRight, currentSkeleton.Joints[JointType.ShoulderRight]);
                        SetEllipsePosition(ShoulderLeft,currentSkeleton.Joints[JointType.ShoulderLeft]);
                        SetEllipsePosition(ShoulderCenter, currentSkeleton.Joints[JointType.ShoulderCenter]);
    
    
                    
                    }
                }
            }
    
    
            //This method is used to position the ellipses on the canvas
            //according to correct movements of the tracked joints.
    
    
            private void SetEllipsePosition(Ellipse ellipse, Joint joint)
            {
                var scaledJoint = joint.ScaleTo(1920, 1080, .5f, .5f);
                //Microsoft.Kinect.SkeletonPoint vector = new Microsoft.Kinect.SkeletonPoint();
                //vector.X = ScaleVector(640, joint.Position.X);
                //vector.Y = ScaleVector(480, -joint.Position.Y);
                //vector.Z = joint.Position.Z;
    
                //Joint updatedJoint = new Joint();
                //updatedJoint = joint;
                //updatedJoint.TrackingState = JointTrackingState.Tracked;
                //updatedJoint.Position = vector;
    
                Canvas.SetLeft(ellipse, scaledJoint.Position.X);
                Canvas.SetTop(ellipse, scaledJoint.Position.Y);
            }
            // Match screen resolution with kinect resolution
           /* private float ScaleVector(int length, float position)
            {
                float value = (((((float)length) / 1f) / 2f) * position) + (length / 2); // Formula to set resolution
                if (value > length)
                {
                    return (float)length;
                }
                if (value < 0f)
                {
                    return 0f;
                }
                return value;
            }*/
    
            void MainWindow_Unloaded(object sender, RoutedEventArgs e)
            {
                sensor.Stop();
            }
    
            void MainWindow_Loaded(object sender, RoutedEventArgs e)
            {
                
                
                
            }
    
    
        }
    }
    

    Tuesday, October 21, 2014 5:36 PM

All replies

  • You have to pass the sensor object you created on MainWindow to your Exercise window. From there you can subscribe to the event, and you don't have to re-open. As well, you need to ensure you have unsubscribed from that even when you shutdown the window to prevent memory leaks.

    Carmine Sirignano - MSFT


    Wednesday, October 22, 2014 10:18 PM
  • But sensor became start when new window open. but its not tracking the skeleton, give SFrame ==Null
    Thursday, October 23, 2014 4:36 PM
  • And can you tell me how i can pass object from one window to other window in this context?

    Thursday, October 23, 2014 4:50 PM
  • Declare your sensor as a public variable in SessionMenu.Xaml.cs and then you can reference it in Exercise.xaml.cs.

    What exactly is your user doing when you try to get Skeleton Tracking (ST) to lock on?  The user must be facing the sensor.


    Ben Lower [MSFT]

    Thursday, October 23, 2014 7:40 PM
  • target is to track all the joints of upper limb of human body. but when  TrackButtonOnClick fire, sensor on next page will start and try to track the skeleton but its not tracking. sensor is enable but not tracking. SFrame give Null value. And i also try sensor as public variable but not working...
    Thursday, October 23, 2014 7:51 PM
  • In exercise you will need to do something like SessionMenu sm = new SessionMenu(); then access the sensor with sm.sensor;

    Did you try that?

    Are you able to get ST to work in one of the sample apps (try Skeleton Basics)?


    Ben Lower [MSFT]

    Thursday, October 23, 2014 8:12 PM
  • yup i tried a sample app and there it was working. now i am trying to upgrade my code, as you are watching in sessionMenu i am using kinect toolkit for click on the button. then after click event i am tracking skeleton in Exercise.xaml.cs. but here its not tracking.. :(
    Thursday, October 23, 2014 8:22 PM
  • And below code is Exercise.xaml, where the joints are tacked and marked
    <Window x:Class="KinectStrokeRecovery.Exercise"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:k="http://schemas.microsoft.com/kinect/2013"
            xmlns:my="clr-namespace:Coding4Fun.Kinect.Wpf.Controls;assembly=Coding4Fun.Kinect.Wpf"
            Title="Exercise" Height="600" Width="800">
        
            <Canvas>
            <Grid>
                <Ellipse Fill="Red" Height="50" Width="44" Name="leftHand" Stroke="White" DataContext="{Binding}" />
                <Ellipse Fill="Red" Height="20" Width="20" Name="rightHand" Stroke="White"  />
                <Ellipse Fill="Red" Height="20" Width="20" Name="head" Stroke="White"  />
                <Ellipse Fill="Red" Height="20" Width="20" Name="ElboRight" Stroke="White"  />
                <Ellipse Fill="Black" Height="20" Width="20" Name="ElboLeft" Stroke="White"/>
                <Ellipse Fill="Green" Height="20" Width="20" Name="ShoulderRight" Stroke="White" />
                <Ellipse Fill="Green" Height="20" Width="20" Name="ShoulderLeft" Stroke="White"  />
                <Ellipse Fill="Green" Height="20" Width="20" Name="WristRight" Stroke="White"  />
                <Ellipse Fill="Green" Height="20" Width="20" Name="WristLeft" Stroke="White"  />
                <Ellipse Fill="Gold" Height="20" Width="20" Name="ShoulderCenter" Stroke="White" />
    </Grid>
            </Canvas>
    
        
    </Window>
    

    Thursday, October 23, 2014 8:30 PM
  • Since you confirmed the samples are working, this has more to do with how your code is constructed and not the Kinect api's SDK. If you have questions with passing object between WPF windows, you will have to research this in the docs or ask on a WPF forum.

    http://stackoverflow.com/questions/12444816/how-to-pass-values-parameters-between-xaml-pages

    The Kinect does not support creating more than one sensor per process and cannot be opened across different processes. You have to manage this yourself or have a look at how this is done with the KinectSensorChooser object.

    For multi-window or user controls sample, you can have a look at the Kinect Explorer sample, but note it is pretty complex. If you do not have a good grasp of .Net/WPF it will take you some time to master how that all works, which is beyond the scope of what this forum can help you with.

    I would recommend you try to get the core functionality you want in a single window app first.


    Carmine Sirignano - MSFT

    Friday, October 24, 2014 8:04 PM
  • Okay Great!!! Can i get XY Value of each joints using code that is written in  SessionMenu.Xaml.cs window?

    like that

         Joint rightHand = skeleton.Joints[JointType.HandRight];
         double rightX = rightHand.Position.X;
         double rightY = rightHand.Position.Y;

    Saturday, October 25, 2014 6:19 AM
  • Every tracked skeleton provides that information, see the skeleton basics sample for information on how to acquire the data.

    Carmine Sirignano - MSFT

    Monday, October 27, 2014 11:33 PM
  • Sir can you write sample code how i can get joint information from below code. i tried my level best to find out the joint information but :( . Just write code for hand joint. waiting for your solution.

    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.Shapes;
    using Microsoft.Kinect;
    using Microsoft.Kinect.Toolkit;
    using Microsoft.Kinect.Toolkit.Controls;
    using Coding4Fun.Kinect.Wpf;
    
    namespace KinectStrokeRecovery
    {
        /// <summary>
        /// Interaction logic for SessionMenu.xaml
        /// </summary>
        public partial class SessionMenu : Window
        {
            private KinectSensorChooser sensorChooser;
            public SessionMenu()
            {
                InitializeComponent();
                Loaded += OnLoaded;
            }
            private void OnLoaded(object sender, RoutedEventArgs routedEventArgs)
            {
    
                this.sensorChooser = new KinectSensorChooser();
                this.sensorChooser.KinectChanged += SensorChooserOnKinectChanged;
                this.sensorChooserUi.KinectSensorChooser = this.sensorChooser;
                this.sensorChooser.Start();
    
                //fill scroll content
                /* for (int i = 1; i < 20; i++)
                 {
                     var button = new KinectCircleButton
                     {
                         Content = i,
                         Height = 200
                     };
    
                     int i1 = i;
                     button.Click +=
                         (o, args) => MessageBox.Show("You clicked button #" + i1);
    
                     scrollContent.Children.Add(button);
                 }*/
            }
    
            private void SensorChooserOnKinectChanged(object sender, KinectChangedEventArgs args)
            {
                bool error = false;
                if (args.OldSensor != null)
                {
                    try
                    {
                        args.OldSensor.DepthStream.Range = DepthRange.Default;
                        args.OldSensor.SkeletonStream.EnableTrackingInNearRange = false;
                        args.OldSensor.DepthStream.Disable();
                        args.OldSensor.SkeletonStream.Disable();
                    }
                    catch (InvalidOperationException)
                    {
                        // KinectSensor might enter an invalid state while enabling/disabling streams or stream features.
                        // E.g.: sensor might be abruptly unplugged.
                        error = true;
                    }
                }
    
                if (args.NewSensor != null)
                {
                    try
                    {
                        args.NewSensor.DepthStream.Enable(DepthImageFormat.Resolution640x480Fps30);
                        args.NewSensor.SkeletonStream.Enable();
    
                        /*  try
                          {
                              args.NewSensor.DepthStream.Range = DepthRange.Near;
                              args.NewSensor.SkeletonStream.EnableTrackingInNearRange = true;
                              args.NewSensor.SkeletonStream.TrackingMode = SkeletonTrackingMode.Seated;
                          }
                          catch (InvalidOperationException)
                          {
                              // Non Kinect for Windows devices do not support Near mode, so reset back to default mode.
                              args.NewSensor.DepthStream.Range = DepthRange.Default;
                              args.NewSensor.SkeletonStream.EnableTrackingInNearRange = false;
                              error = true;
                          }*/
                    }
                    catch (InvalidOperationException)
                    {
                        error = true;
                        // KinectSensor might enter an invalid state while enabling/disabling streams or stream features.
                        // E.g.: sensor might be abruptly unplugged.
                    }
                }
    
                if (!error)
                    kinectRegion.KinectSensor = args.NewSensor;
            }
    
            private void TrackButtonOnClick(object sender, RoutedEventArgs e)
            {
                sensorChooser.Stop();
                Exercise ex = new Exercise();
                this.Close();
                ex.Show();
            }
        }
    }

    Tuesday, October 28, 2014 6:50 PM
  • Have a look at the QuickStart series if you need programming help. http://channel9.msdn.com/Series/KinectQuickstart/Skeletal-Tracking-Fundamentals


    Carmine Sirignano - MSFT

    Wednesday, October 29, 2014 5:49 PM