Wednesday
Mar142012

Getting Started with Kinect For Windows in Visual Studio .NET

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:

  • Create a new WPF application
  • Add a reference to the Kinect SDK

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

  • If using Speech, add a reference to the Microsoft Speech SDK

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();
                }
            }
 
        }
    }
 
 

PrintView Printer Friendly Version

EmailEmail Article to Friend

References (16)

References allow you to track sources for this article, as well as articles that were written in response to this article.
  • Response
    one of the best presents.if you really want to surprise somebody, you can always choose personalized presents. imagine a bottle of champagne with your own anniversary
  • Response
    Response: have a peek here
    Great Webpage, Continue the excellent job. Thank you!
  • Response
    Carl Franklin - Intellectual Hedonism - Blog - Getting Started with Kinect For Windows in Visual Studio .NET
  • Response
    Response: underarm sweat
    Carl Franklin - Intellectual Hedonism - Blog - Getting Started with Kinect For Windows in Visual Studio .NET
  • Response
    Carl Franklin - Blog - Getting Started with Kinect For Windows in Visual Studio .NET
  • Response
    End-user development tailors systems to meet users' specific needs. User software include spreadsheet templates and word processor templates. Even email filters are a kind of user software.
  • Response
    Response: oferta ereader
    Carl Franklin - Blog - Getting Started with Kinect For Windows in Visual Studio .NET
  • Response
    Carl Franklin - Blog - Getting Started with Kinect For Windows in Visual Studio .NET
  • Response
    Carl Franklin - Blog - Getting Started with Kinect For Windows in Visual Studio .NET
  • Response
    Response: click web page
    Carl Franklin - Blog - Getting Started with Kinect For Windows in Visual Studio .NET
  • Response
    Your blog numerous publishing solutions providing. Comfortable articles solutions essay or dissertation publishing solutions all the learners knowledge intent in your blog site. The many publishing solutions the knowledge upcoming applying. Many thanks a lot.
  • Response
    logomarcas fail que nao deram certa mais bonitas do mundo confira inspiração
  • Response
    Response: new year 2016
    tyj
  • Response
    Los iconos son un material indispensable en los proyectos de diseño, sobre todo cuando va a crear algo desde cero. Ayudan a crear mucho más entendimiento del sitio web que estás diseñando. Pero a veces se tarda mucho en encontrar los iconos que necesitas. En algunos casos es mejor dibujar el ...
  • Response
    Response: carrossel fest
  • Response
    Roland Garros
« Recovering Gracefully from Loss of Skeletal Tracking (Kinect For Windows) | Main | Acoustic Addicts Pilot - A Little More Depth »