blogs

blogs

08.222013
7 0
Detecting Kinect Sensor

Detecting Kinect Sensor

In my last blog post we discussed about the basics to get started with Kinect programming, today we will see how we can detect a connected Kinect device and Enable its stream and start that device. We will start by creating a sample application in WPF that uses the Kinect device.Open Visual studio and create a new WPF Application. Next we need to add reference to Microsoft.Kinect dll. This will enable us to use Kinect with our application.

In MainWindow.xaml.cs add the using directive for Microsoft.kinect, now we are ready to use kinect device, with our application.

Before we move further, it is important to know different status a kinect sensor can have. These statuses as defined in the KinectStatus enumeration, are:

Status

Description

Connected

The Kinect is fully connected and ready.

DeviceNotGenuine

The Kinect is not genuine.

DeviceNotSupported

The Kinect is not supported.

Disconnected

The Kinect is not connected to the USB connector.

Error

An error has occurred.

Initializing

The Kinect is initializing.

InsufficientBandwidth

The USB connector does not have sufficient bandwidth.

NotPowered

The Kinect is not powered up.

NotReady

Some part of the kinect is not yet ready.

Undefined

The status is not defined.

We will use some of these statuses in our code, which is as follows:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
using Microsoft.Kinect;
namespace KinectBiginner
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
private KinectSensor sensor = null;
public MainWindow()
{
InitializeComponent();
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
getKinect();// assigns the kinect sensor device to KinectSensor object.
//tracks the change in the kinect's status.
KinectSensor.KinectSensors.StatusChanged +=new EventHandler<StatusChangedEventArgs>(KinectSensors_StatusChanged);
if (sensor != null)
{
lblKinectStatus.Content = "Kinect Conected.";
}
else
{
lblKinectStatus.Content = "No device found.";
}
}
/// <summary>
/// Assigns the first kinect detected to the KinectSensor object.
/// Enables Skeletonstream and starts the device.
/// </summary>
private void getKinect()
{
sensor = KinectSensor.KinectSensors.FirstOrDefault(s => s.Status == KinectStatus.Connected);
if (sensor != null)
{
sensor.SkeletonStream.Enable();
sensor.Start();
}
}
/// <summary>
/// This is called each time, when there is a change in the kinect device status.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void KinectSensors_StatusChanged(Object sender, StatusChangedEventArgs e)
{
switch (e.Status)
{
case KinectStatus.Initializing:
if (sensor == null)
{
lblKinectStatus.Content = "Initializing Kinect...";
}
break;
case KinectStatus.Connected:
if (sensor == null)
{
getKinect();
if (sensor != null)
{
lblKinectStatus.Content = "Kinect Connected.";
}
}
break;
case KinectStatus.Disconnected:
if (sensor == e.Sensor)
{
lblKinectStatus.Content = "Kinect was disconnected.";
if (sensor != null)
{
sensor.Stop();
sensor = null;
}
}
break;
case KinectStatus.NotPowered:
if (sensor == e.Sensor)
{
lblKinectStatus.Content = "Kinect not powered.Please check the power supply.";
if (sensor != null)
{
sensor.Stop();
sensor = null;
}
}
break;
}
}
}
}

This application will show the current status of the Kinect device, as can be seen in below screen shots:

  1. Initially if the device is not connected
  2. <spanclass=”Kinect” >Kinect device is initializing
  3. When the status of the kinect is connected and is ready to be used
  4. When status of kinect is disconnected
  5. When the power adapter of kinect is not powered

For any commercial Kinect application it is important to track the status of Kinect device in real time.

In next blog we will see how we can track user skeleton using Kinect.

Leave a Reply

Your email address will not be published. Required fields are marked *