Getting Started with Kinect For Windows in Visual Studio .NET
Wednesday, March 14, 2012 at 03:51PM
Carl Franklin in Kinect

If you haven't heard, Microsoft has released a new version of the Kinect that's just for Windows applications, along with a new Software Development Kit so .NET developers can write apps for Windows that use the Kinect.

Code along with me while I show you how to get started.

Downloads:

This is a 64-bit SDK with a commercial license.

If you're going to use voice recognition with the Kinect, download these:

Download the x86 versions of each of these. Kinect only works with the 32-bit version.

We're going to make an application that simply tracks your right hand, showing the X, Y, and Z values in real-time as you move it. 

Getting Started:

C:\Program Files\Microsoft SDKs\Kinect\v1.0\Assemblies\Microsoft.Kinect.dll

C:\Program Files (x86)\Microsoft SDKs\Speech\v11.0\Assembly\Microsoft.Speech.dll

XAML Source:

Change the default <Grid></Grid> to the following:

    <StackPanel>
        <Label FontSize="30" Content="X" Height="50"
                  HorizontalAlignment="Left" Margin="10,10,0,0" Name="LabelX" 
                  VerticalAlignment="Top" />
        <Label FontSize="30" Content="Y" Height="50"
                  HorizontalAlignment="Left" Margin="10,10,0,0" Name="LabelY"
                  VerticalAlignment="Top" />
        <Label FontSize="30" Content="Z" Height="50"
                  HorizontalAlignment="Left" Margin="10,10,0,0" Name="LabelZ"
                  VerticalAlignment="Top" />
    </StackPanel>

VB Source:

Class MainWindow 
    Private WithEvents Sensor As Microsoft.Kinect.KinectSensor
    Private Sub Window_Loaded(sender As System.Object, e As System.Windows.RoutedEventArgs) Handles MyBase.Loaded
        If Microsoft.Kinect.KinectSensor.KinectSensors.Count = 0 Then
            MessageBox.Show("No Kinect Devices Found")
            Application.Current.Shutdown()
        Else
            Sensor = Microsoft.Kinect.KinectSensor.KinectSensors(0)
            Sensor.SkeletonStream.Enable(New Microsoft.Kinect.TransformSmoothParameters)
            Sensor.Start()
        End If
    End Sub
 
    Private Sub Sensor_SkeletonFrameReady(sender As Object, e As Microsoft.Kinect.SkeletonFrameReadyEventArgs) Handles Sensor.SkeletonFrameReady
        Dim frame = e.OpenSkeletonFrame
 
        '-- Bail if there's no frame.
        If frame Is Nothing Then Return
 
        Using frame
            '-- Bail if no data is returned 
            If frame.SkeletonArrayLength = 0 Then Return
 
            '-- Copy the data to an array
            Dim data(frame.SkeletonArrayLength - 1) As Microsoft.Kinect.Skeleton
            frame.CopySkeletonDataTo(data)
 
            '-- Bail if the data wasn't copied
            If data.Length = 0 Then Return
 
            '-- Are we tracking?
            If data(0).TrackingState = Microsoft.Kinect.SkeletonTrackingState.Tracked Then
                '-- data(0) now contains live data
                Dim X = data(0).Joints(Microsoft.Kinect.JointType.HandRight).Position.X
                Dim Y = data(0).Joints(Microsoft.Kinect.JointType.HandRight).Position.Y
                Dim Z = data(0).Joints(Microsoft.Kinect.JointType.HandRight).Position.Z
                LabelX.Content = X.ToString
                LabelY.Content = Y.ToString
                LabelZ.Content = Z.ToString
                LabelX.UpdateLayout()
                LabelY.UpdateLayout()
                LabelZ.UpdateLayout()
            End If
        End Using
    End Sub
End Class

C# Source:
 
    public partial class MainWindow : Window
    {
        Microsoft.Kinect.KinectSensor Sensor;
 
        public MainWindow()
        {
            InitializeComponent();
            if (Microsoft.Kinect.KinectSensor.KinectSensors.Count == 0)
            {
                MessageBox.Show("No Kinect Devices Found");
                Application.Current.Shutdown();
            }
            else
            {
                Sensor = Microsoft.Kinect.KinectSensor.KinectSensors[0];
                Sensor.SkeletonFrameReady += Sensor_SkeletonFrameReady;
                Sensor.SkeletonStream.Enable(new Microsoft.Kinect.TransformSmoothParameters());
                Sensor.Start();
            }
        }
 
        private void Sensor_SkeletonFrameReady(object sender, Microsoft.Kinect.SkeletonFrameReadyEventArgs e)
        {
            Microsoft.Kinect.SkeletonFrame frame = e.OpenSkeletonFrame();
 
            //-- Bail if there's no frame.
            if (frame == null)
                return;
 
            using (frame)
            {
                //-- Bail if no data is returned 
                if (frame.SkeletonArrayLength == 0)
                    return;
 
                //-- Copy the data to an array
                Microsoft.Kinect.Skeleton[] data = new Microsoft.Kinect.Skeleton[frame.SkeletonArrayLength];
                frame.CopySkeletonDataTo(data);
 
                //-- Bail if the data wasn't copied
                if (data.Length == 0)
                    return;
 
                //-- Are we tracking?
                if (data[0].TrackingState == Microsoft.Kinect.SkeletonTrackingState.Tracked)
                {
                    //-- data(0) now contains live data
                    Single X = data[0].Joints[Microsoft.Kinect.JointType.HandRight].Position.X;
                    Single Y = data[0].Joints[Microsoft.Kinect.JointType.HandRight].Position.Y;
                    Single Z = data[0].Joints[Microsoft.Kinect.JointType.HandRight].Position.Z;
                    LabelX.Content = X.ToString();
                    LabelY.Content = Y.ToString();
                    LabelZ.Content = Z.ToString();
                    LabelX.UpdateLayout();
                    LabelY.UpdateLayout();
                    LabelZ.UpdateLayout();
                    this.UpdateLayout();
                }
            }
 
        }
    }
 
 
Article originally appeared on Carl Franklin (http://carlfranklin.net/).
See website for complete article licensing information.