Fluent Kinect

Since I have been playing around with the Kinect for Windows SDK I’ve created a lot of little new projects and samples to try things out. Starting point was always something like this:

var sensor = KinectSensor.KinectSensors
                         .FirstOrDefault(_ => _.Status == KinectStatus.Connected);
if (sensor == null) throw new InvalidOperationException("No kinect connected");

sensor.ColorStream.Enable(ColorImageFormat.RgbResolution640x480Fps30);
sensor.SkeletonStream.EnableTrackingInNearRange = true;
sensor.SkeletonStream.TrackingMode = SkeletonTrackingMode.Seated;
sensor.SkeletonStream.Enable();
sensor.DepthStream.Enable(DepthImageFormat.Resolution640x480Fps30);
sensor.Start();

A lot of code just to set up a Kinect sensor, isn't it?

Why not using a fluent style with less and cleaner code to set up a Kinect Sensor? So I came up with the idea of FluentKinect, a project with a few extension methods. Now I can set up my Kinect Sensor this way:

var sensor = KinectSensor.KinectSensors
                         .FirstOrDefault(_ => _.Status == KinectStatus.Connected);
if (sensor == null) throw new InvalidOperationException("No kinect connected");

sensor.EnableColorStream()
      .EnableSkeletonStream()
      .EnableDepthStream()
      .Seated()
      .NearMode()
      .Start();

Because I most often use the 640x480 option anyway, the format is an optional parameter when enabling the streams and it defaults to *640x480Fps30.
I've exracted the two little lines that gets the first connected Kinect Sensor to a class called KinectConnector. At the moment an exception is thrown when no Kinect unit is connected. This is not a very good way of handling this scenario and will be changed in the future.
Now the code is even cleaner:

var sensor = KinectConnector.GetKinect()
                            .EnableColorStream()
                            .EnableSkeletonStream()
                            .EnableDepthStream()
                            .Seated()
                            .NearMode()
                            .Start();

For an even shorter and quicker Setup I've implemented the method 'KickStart' which enables the three streams and calls Start() on the KinectSensor object.
For future 'try out samples' I'll just have to code this now:

var sensor = KinectConnector.GetKinect()
                            .KickStart();
About these ads

6 thoughts on “Fluent Kinect

  1. Pingback: [#KINECTSDK] Fluent Kinect (nunca subestimes lo que puede hacer un friki aburrido!) | El Bruno

  2. Pingback: [#KINECTSDK] Fluent Kinect (nunca subestimes lo que puede hacer un friki aburrido!) - El Bruno

  3. Your style is so unique compared to other people I have read
    stuff from. Thank you for posting when you’ve got the opportunity, Guess I’ll
    just bookmark this site.

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