Click here to Skip to main content
15,884,628 members
Articles / Desktop Programming / WPF
Tip/Trick

Kinect status and setup the Kinect for interaction

Rate me:
Please Sign up or sign in to vote.
4.87/5 (10 votes)
24 Dec 2013CPOL2 min read 45K   16   10
Kinect for windows status and setup the Kinect for interaction

Introduction 

The article will give you a getting started with kinect for windows sensor SDK 1.8 and how the kinect for interaction works. I am going to use the kinect sdk 1.8 and a WPF project using visual studio. 

Pre-Requisites   

  • Visual studio 2012
  • .NET 4.5
  • Kinect for windows sensor device.
  • Kinect for Windows SDK (http://go.microsoft.com/fwlink/?LinkID=323588)
  • Kinect for Windows Developer Toolkit (http://go.microsoft.com/fwlink/?LinkID=323589) 

Kinect for window sensor status

Create a WPF Application project WPFKinect18 using visual studio 2012.

Image 1

Add the following references:From the C:\Program Files\Microsoft SDKs\Kinect\v1.8\Assemblies\ folder, add Microsoft.Kinect.dll.

Image 2

From the C:\Program Files\Microsoft SDKs\Kinect\Developer Toolkit v1.8.0\Assemblies\ folder, add Kinect.Toolkit.dll, Kinect.Toolkit.Controls.dll and Kinect.Toolkit.Interaction.dll

Image 3

Add the KinectSensorChooserUI to the MainWindow to get the Status of the Kinect sensor. Plugged, unplugged, powered.

And a label control to display the Kinect status change. 

Image 4

Image 5

Kinect not connected

Image 6

Kinect is initializing (you can see the status by hovering on it)

Image 7

Kinect Connected

Image 8

Setup the Kinect for interaction

Copy these “KinectInteraction180_32.dll, KinectInteraction180_64.dll” dll’s on the output directory of your project. As shown below (you can find these dll’s in the source code of this article.)

Image 9

Now change the “SensorChooserOnKinectChanged” event by the following code.

Image 10

On the mainwindows.xaml

XML
<k:kinectuserviewer k:kinectregion.kinectregion="{Binding ElementName=kinectRegion}" height="150" primaryusercolor="Violet" usercoloringmode="HighlightPrimary" horizontalalignment="Right" verticalalignment="Bottom">
</k:kinectuserviewer>

Image 11

You can see the user depth image on the right bottom of the screen. User is detected in-front of Kinect but not active. The user is looking gray colored

Image 12

Now the user is active and moving hand and its depth image becomes colored. And you can also see the user’s hand, both hands are track able. You can switch hand cursor control from left hand to right hand easily.

Adding the Kinect tile button inside the Kinect region

XML
<k:kinecttilebutton click="ClickMe_Click" label="Click Me" x:name="ClickMe" />

In the MainWindow.cs page.

C#
private void ClickMe_Click(object sender, RoutedEventArgs e)
        {
            KinectStatus.Content = "Clicked me";
        }

Hover hand on the button on the button

Image 13

Taking your hand towards Kinect sensor, like pushing a button. Partial pushed button

Image 14

Pressed button looks like this.

Image 15

Take you hand back to fire up the click event of this button.

Image 16

For scrolling area on the Kinect: grip your figures and scroll from left to right and right to left.

Image 17

Using the code  

 MainWindow.xaml

XML
<Window x:Class="WPFKinectSDK18.MainWindow"
        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"
        Title="MainWindow" Height="450" Width="625" Loaded="Window_Loaded" WindowState="Maximized">
    <Grid>
        <k:KinectSensorChooserUI HorizontalAlignment="Center" VerticalAlignment="Top" Name="sensorChooserUi" />
        <Label x:Name="KinectStatus" Content="Kinect status change" 
               HorizontalAlignment="Left" VerticalAlignment="Bottom" FontSize="15"></Label>
        <k:KinectRegion x:Name="kinectRegion">
            <Grid>
                <k:KinectUserViewer VerticalAlignment="Bottom" HorizontalAlignment="Right" 
                UserColoringMode="HighlightPrimary" PrimaryUserColor="Violet" Height="150"
                k:KinectRegion.KinectRegion="{Binding ElementName=kinectRegion}" />
                <k:KinectTileButton x:Name="ClickMe" Label="Click Me" Click="ClickMe_Click" VerticalAlignment="Top" HorizontalAlignment="Left"></k:KinectTileButton>
                <k:KinectScrollViewer VerticalScrollBarVisibility="Disabled" HorizontalScrollBarVisibility="Auto" VerticalAlignment="Center">
                    <StackPanel Orientation="Horizontal" x:Name="StackPanelWithButton" />
                </k:KinectScrollViewer>
            </Grid>
        </k:KinectRegion>
    </Grid>
</Window> 
MainWindow.xaml.cs
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
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 Microsoft.Kinect.Toolkit;
using Microsoft.Kinect.Toolkit.Controls;

namespace WPFKinectSDK18
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private KinectSensorChooser sensorChooser;
        public MainWindow()
        {
            InitializeComponent();
        }
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            this.sensorChooser = new KinectSensorChooser();
            this.sensorChooser.KinectChanged += SensorChooserOnKinectChanged;
            this.sensorChooserUi.KinectSensorChooser = this.sensorChooser;
            this.sensorChooser.Start();
            GetTileButtonsForStackPanel();
        }
        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 inEX) { error = true; }
            }

            if (args.NewSensor != null)
            {
                switch (Convert.ToString(args.NewSensor.Status))
                {
                    case "Undefined": KinectStatus.Content = "Undefined"; break;
                    case "Disconnected": KinectStatus.Content = "Disconnected"; break;
                    case "Connected": KinectStatus.Content = "Connected"; break;
                    case "Initializing": KinectStatus.Content = "Initializing"; break;
                    case "Error": KinectStatus.Content = "Error"; break;
                    case "NotPowered": KinectStatus.Content = "NotPowered"; break;
                    case "NotReady": KinectStatus.Content = "NotReady"; break;
                    case "DeviceNotGenuine": KinectStatus.Content = "DeviceNotGenuine"; break;
                    case "DeviceNotSupported": KinectStatus.Content = "DeviceNotSupported"; break;
                    case "InsufficientBandwidth": KinectStatus.Content = "InsufficientBandwidth"; break;
                    default: KinectStatus.Content = "Undefined"; break;
                }
                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 inEX)
                    {
                        args.NewSensor.DepthStream.Range = DepthRange.Default;
                        args.NewSensor.SkeletonStream.EnableTrackingInNearRange = false;
                        error = true;
                    }
                }
                catch (InvalidOperationException inEX) { error = true; }
            }
            if (!error) { kinectRegion.KinectSensor = args.NewSensor; }
        }

        private void ClickMe_Click(object sender, RoutedEventArgs e)
        {
            KinectStatus.Content = "Clicked me";
        }

        private void GetTileButtonsForStackPanel()
        {
            for (int i = 1; i <= 35; i++)
            {
                KinectTileButton ObjTileButton = new KinectTileButton();                
                ObjTileButton.Height = 250;
                ObjTileButton.Label = i;
                ObjTileButton.Click += (o, Args) => KinectStatus.Content = "You clicked on tile button : " + i;
                StackPanelWithButton.Children.Add(ObjTileButton);
            }
        }
    }
}

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Technical Lead Ignatiuz Software Pvt. Ltd.
India India
Working in the projects by using technologies (.Net, C#, MSSQL, Web, Desktop, Kinect based Natural user inter-phase development, dynamic CRM.)

Comments and Discussions

 
GeneralDownload link not found Pin
DineshChaudhary4-Aug-20 21:20
DineshChaudhary4-Aug-20 21:20 
GeneralTrying to see if it works on xbox360 kinect with PC adapter Pin
Victor Ng15-Feb-15 22:11
Victor Ng15-Feb-15 22:11 
The Kinect sensor plugged into your computer is for use on the Xbox 360.
You may continue using your Xbox 360 Kinect sensor on your computer for development purposes.
Microsoft does not guarantee full compatibility for Kinect for Windows applications and the Xbox 360 Kinect Sensor.The thread 0x1404 has exited with code 259 (0x103).
A first chance exception of type 'System.InvalidOperationException' occurred in Microsoft.Kinect.dll
'WPFKinect18.vshost.exe' (CLR v4.0.30319: WPFKinect18.vshost.exe): Loaded 'C:\windows\Microsoft.Net\assembly\GAC_MSIL\UIAutomationProvider\v4.0_4.0.0.0__31bf3856ad364e35\UIAutomationProvider.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
The thread 0x12f4 has exited with code 259 (0x103).
The thread 0x17f8 has exited with code 259 (0x103).
The program '[3884] WPFKinect18.vshost.exe' has exited with code 0 (0x0).


Does this mean your code only works for kinect for windows only? I really do not have any access to a Kinect for Windows. I'm trying to use your code just to see if I can actually develop with this sensor.
-Victor-

GeneralRe: Trying to see if it works on xbox360 kinect with PC adapter Pin
Pankaj Deharia25-Sep-15 1:53
professionalPankaj Deharia25-Sep-15 1:53 
QuestionThanks for your tut. Pin
Member 105060958-Apr-14 23:23
Member 105060958-Apr-14 23:23 
AnswerRe: Thanks for your tut. Pin
Pankaj Deharia8-Apr-14 23:58
professionalPankaj Deharia8-Apr-14 23:58 
QuestionWhere to buy Kinect for Windows in India Pin
ramatbellcity10-Mar-14 20:42
ramatbellcity10-Mar-14 20:42 
AnswerRe: Where to buy Kinect for Windows in India Pin
Pankaj Deharia Ignatiuz28-Mar-14 3:56
professionalPankaj Deharia Ignatiuz28-Mar-14 3:56 
QuestionProblem making it run Pin
N. Henrik Lauridsen6-Mar-14 1:40
N. Henrik Lauridsen6-Mar-14 1:40 
AnswerRe: Problem making it run Pin
Pankaj Deharia Ignatiuz28-Mar-14 3:43
professionalPankaj Deharia Ignatiuz28-Mar-14 3:43 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.