Kinect.ReactiveV2 – Rx-ing the Kinect for Windows SDK

A few weeks ago I was finally able to get my hands on to the new Kinect for Windows V2 SDK. There are a few API changes compared to V1. So I started to port Kinect.Reactive to the new Kinect for Windows Dev Preview SDK and Kinect.ReactiveV2 was born.

Kinect.ReactiveV2 is, as it’s older brother, a project that contains a bunch of extension methods that should ease the development with the Kinect for Windows SDK. The project uses the ReactiveExtensions (an open source framework built by Microsoft) to transform the various Kinect reader events into IObservable<T> sequences. This transformation enables you to use Linq style query operators on those events.

Here is an example of how to use the BodyIndexFrame data as an observable sequence.

using System.Linq;
using System.Reactive;
using Microsoft.Kinect;
using Kinect.ReactiveV2;

var sensor = KinectSensor.Default;
sensor.Open();

var bodyIndexFrameDescription = sensor.BodyIndexFrameSource.FrameDescription;
var bodyIndexData = new byte[bodyIndexFrameDescription.LengthInPixels];

sensor.BodyIndexFrameArrivedObservable()
      .SelectBodyIndexData(bodyIndexData)
      .Subscribe(data => someBitmap.WritePixels(rect, data, stride, 0));

You’ll also get an extension method called SceneChanges() on every KinectSensor instance which notifies all it’s subscribers whenever a person entered or left a scene.

using System;
using System.Linq;
using System.Reactive;
using Microsoft.Kinect;
using Kinect.ReactiveV2;

var sensor = KinectSensor.Default;
sensor.Open();

sensor.SceneChanges()
      .Subscribe(_ =>
      {
            if (_.SceneChangedType is PersonEnteredScene)
            {
                  Console.WriteLine("Person {0} entered scene", _.SceneChangedType.TrackingId);
            }
            else if (_.SceneChangedType is PersonLeftScene)
            {
                  Console.WriteLine("Person {0} left scene", _.SceneChangedType.TrackingId);
            }
      });

Until now there are extension methods included for the BodyFrame, BodyIndexFrame, ColorFrame, DepthFrame, InfraredFrame and MultiSourceFrame.

The source code is available here.
Download the nuget package from here, or directly typing Install-Package Kinect.ReactiveV2 into the package manager console.

Please be aware that “This is preliminary software and/or hardware and APIs are preliminary and subject to change”.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s