Enumerating and Starting a Kinect for Windows Sensor
Kinect for Windows 1.5, 1.6, 1.7, 1.8
Overview
Kinect for Windows drivers support the use of multiple sensors on a single computer. The API includes functions that enumerate the sensors so that you can determine how many Kinects are connected to the computer, get the name of a particular sensor, and set streaming characteristics for each sensor. After finding a connected sensor, you need to enable any or all of the data streams (color, depth, and skeleton) and then start the sensor to begin streaming data out.
Code It
Enumerate Kinect sensors
Enumerate the Kinects that are connected to your computer using the KinectSensor class. When you declare the private sensor member, the static KinectSensors property is filled with the collection of Kinects that are on your system. Because the property is static, the collection of Kinects is available as long as your application is running.
private KinectSensor sensor; foreach (var potentialSensor in KinectSensor.KinectSensors) { if (potentialSensor.Status == KinectStatus.Connected) { this.sensor = potentialSensor; break; } }
Use a foreach statement to loop through the collection and test each Kinect to see if it is connected using the KinectStatus enumeration. Once you find a sensor whose status is Connected, store the Kinect in the sensor member so that you can use the sensor in the code below.
Enable Data Streaming
After enumerating the available sensors and finding one that is connected and ready, the next step is to enable the Kinect to stream out data. There are several types of data that can be streamed out: color, depth, skeleton, and infrared. This example demonstrates how to enable each of the data streams.
if (this.sensor != null) { this.sensor.ColorStream.Enable(ColorImageFormat.RgbResolution640x480Fps30); this.sensor.DepthStream.Enable(DepthImageFormat.Resolution640x480Fps30); this.sensor.SkeletonStream.Enable(); this.sensor.ColorStream.Enable(ColorImageFormat.InfraredResolution640x480Fps30); }
To stream color data - the KinectSensor class contains a ColorStream property that accesses a ColorImageStream class. Call the Enable method to enable the Kinect to generate color data by initializing the image data format, the framerate, and the resolution of the pixel data. Valid values for each of these are contained in the ColorImageFormat enumeration; the default value is RgbResolution640x480Fps30, which means the Kinect will stream out RGB data, at a 640x480 resolution, at a frame rate of 30 frames per second. To create a raw bayer color data stream, specify a raw bayer format such as ColorImageFormat.RawBayerResolution640x480Fps30 when calling the Enable method.
To stream depth data: the KinectSensor class contains a DepthStream property that accesses a DepthImageStream class. Call the Enable method to initialize the image data format, the frame rate, and the resolution. Valid values are in the DepthImageFormat enumeration. The default value is RgbResolution640x480Fps30, which means the Kinect will stream out RGB data, at a 640x480 resolution, at a frame rate of 30 frames per second.
To stream skeleton data: the KinectSensor class contains a SkeletonStream property which accesses a SkeletonStream class. Call the Enable method to enable the Kinect to generate skeleton data.
The KinectSensor class contains a ColorStream property which accesses a ColorImageStream class. Use the Enable method to enable the Kinect to generate infrared data by initializing the image data format, the framerate, and the resolution of the pixel data. Valid values for each of these are contained in the ColorImageFormat enumeration using the value InfraredResolution640x480Fps30 which means the Kinect will stream out infrared data, at a 640x480 resolution, at a frame rate of 30 frames per second.
Start the Kinect
Once you have chosen a connected sensor, and initialized the data streams that you need in your application, call the Start method to start streaming data out.
if (this.sensor != null) { this.sensor.Start(); }