await GetKinect()

In the first blog post about FluentKinect I’ve mentioned that I’m not very happy with the actual process of getting a KinectSensor instance from the KinectSensors collection.

FluentKinect has now been updated and the KinectConnector’s static method GetKinect is now awaitable.

If you now call KinectConnector.GetKinect() and no KinectSensor is connected to your PC a new Task is started that listens for StatusChanged events of the KinectSensor collection. If you later plugin a Kinect controller the Task returns the connected KinectSensor instance.

KinectConnector’s GetKinect method:

public static Task<KinectSensor> GetKinect()
{
	return Task.Factory.StartNew<KinectSensor>(() =>
	{
		if (kinectSensor != null) return kinectSensor;

		var kinect = KinectSensor.KinectSensors
							.FirstOrDefault(_ => _.Status == KinectStatus.Connected);
		if (kinect != null)
		{
			kinectSensor = kinect;
			return kinectSensor;
		}

		using (var signal = new ManualResetEventSlim())
		{
			KinectSensor.KinectSensors.StatusChanged += (s, e) =>
			{
				if (e.Status == KinectStatus.Connected)
				{
					kinectSensor = e.Sensor;
					coordinateMapper = new CoordinateMapper(kinectSensor);
					signal.Set();
				}
			};

			signal.Wait();
		}

		return kinectSensor;
	});
}

How to use it:

var kinect = await KinectConnector.GetKinect();
kinect.Start();

It’s not thread safe at the moment but there are a few improvements now in my opinion.

    • You can start and debug your actual program without a Kinect controller connected because no more exception is thrown. This helps with Kinect programming on a plane for example. 😉
    • Your programm starts faster because GetKinect returns immediately
    • Since GetKinect returns a Task you get the ability to await the result

The code was pushed to GitHub and the nuget package FluentKinect has been updated as well.

Looking forward to new improvements!

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 640×480 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();

Set up LinqPad for OracleConnections

If you want to query an Oracle database with LinqPad you have to install the additional driver ‘IQDriver’.
You can download it through LinqPad if you click on ‘Add connection’ –> ‘View more drivers’.

linqpad

I had the problem that my Windows account didn’t have administrative privileges and I was not able to download the driver (Windows XP).
When I clicked on ‘Download and enable driver’ I received a message box ‘Invalid driver file’.
Because I didn’t know the proxy settings either, I downloaded the .zip file containing the driver
through the offical LinqPad website by following the message ‘Click here to download these drivers from a webbrowser’.

I saved the .zip file and unzipped it under
‘C:\Documents and Settings\All Users\Applicaiton Data\LINQPad\drivers\DataContext\4.0\IQDriver (5b59726538a49684)’.

After starting LinqPad and adding a new connection I was able to select the driver ‘IQDriver’ to establish
a connection to the oracle database.

Ready to work in the usual way!

Thanks Joseph Albahari for this awesome tool!!!

Framework soup

Over the last months I’ve spent quite some time checking out the Kinect for Windows SDK and it’s really fun to work with this awesome little piece of hard and software.

While playing around with the Kinect for Windows SDK it soon became obvious that I’ll need some sort of framework that handles all the tough event handling stuff for me. Thats were the ReactiveExtensions framework came into play. Since I’ve first read about it I wanted to use it somewhere but didn’t get the chance so far. Now that the Kinect came around the corner with its event driven API Rx fitted perfectly to my requirements.

Another framework that impressed me a lot is SignalR, an abstraction layer for persistent connections over http.

So how do they all come together? In an application I called ‘KickerNotifier‘!
We have a ‘Kicker’ (thats german, in english foosball/tabletop football/tabletop soccer) to play with in the cellar of our office.

Problem is that the table stands in the cellar and you never know whether it is busy.

So the idea is the following: Set up a Kinect that is able to track the number of people in the room and push the amount to a website so that all the colleagues are able to see how many people are playing.

The technology stack:

The SignalR hub class
public class KickerNotifyHub : Hub
{
    private Int32 playerCount = -1;
    public void SetPlayerCount(Int32 count)
    {
        if (this.playerCount != count)
        {
            this.playerCount = count;
            Clients.setCurrentPlayerCount(this.playerCount);
        }
    }
}
Webpage hub connection
<script type="text/javascript" src="Scripts/jquery-1.6.4.js" />
<script type="text/javascript" src="Scripts/jquery.signalR-0.5.3.js" />
<script src="signalr/hubs" type="text/javascript" />
$(function () {
    var hub = $.connection.kickerNotifyHub;

    hub.setCurrentPlayerCount = function (count) {
        $('#currentPlayerCount').text(count); // updates <div id="currentPlayerCount" />
    };

    $.connection.hub.start();
});
Client console application
using System.Reactive.Disposables;
using System.Reactive.Linq;
using SignalR.Client;
using SignalR.Client.Hubs;

// some method

var personNotification = new PersonNotification();
var connection = new HubConnection("http://???????");
var hub = connection.CreateProxy("KickerNotifyHub");
var setPlayerCountSubscription = Disposable.Empty;
connection.Start().ContinueWith(task =>
{
   if (task.IsFaulted)
   {
      Console.WriteLine("Failed to start: {0}", task.Exception.GetBaseException());
   }
   else
   {
      Console.WriteLine("Success! Connected with client connection id {0}", connection.ConnectionId);

      setPlayerCountSubscription = Observable.Interval(TimeSpan.FromSeconds(1))
                                             .Subscribe(l => hub.Invoke("SetPlayerCount", personNotification.PersonCount));
   }
});

Console.ReadLine();
setPlayerCountSubscription.Dispose();
personNotification.Dispose();
PersonNotification.cs
public class PersonNotification
{
   private readonly KinectSensor kinect;
   private readonly IDisposable newSkeletonDataEvent;

   public PersonNotification() : IDisposable
   {
      this.kinect = KinectSensor.KinectSensors
                                .FirstOrDefault(s => s.Status == KinectStatus.Connected);
      if (this.kinect == null) throw new InvalidOperationException("No Kinect connected.");

      this.newSkeletonDataEvent = Observable.FromEventPattern(this.kinect, "SkeletonFrameReady")
                                            .Select(e => e.EventArgs)
                                            .Subscribe(NewSkeletonData);

      this.kinect.SkeletonStream.TrackingMode = SkeletonTrackingMode.Seated;
      this.kinect.SkeletonStream.Enable();
      this.kinect.Start();
   }

   private void NewSkeletonData(SkeletonFrameReadyEventArgs skeletonDataFrame)
   {
      using (var frame = skeletonDataFrame.OpenSkeletonFrame())
      {
         if (frame == null) return;

         var skeletons = new Skeleton[frame.SkeletonArrayLength];
         frame.CopySkeletonDataTo(skeletons);

         var personCount = skeletons.Count(s => s.TrackingState == SkeletonTrackingState.PositionOnly ||
                                                s.TrackingState == SkeletonTrackingState.Tracked);
         if (this.PersonCount != personCount)
         {
            this.PersonCount = personCount;
         }
      }
   }

   public Int32 PersonCount { get; private set; }

   public void Dispose()
   {
      this.newSkeletonDataEvent.Dispose();
   }
}

Kinect.Reactive

Events in the .NET programming model really don’t have anything in common with what the Object Oriented paradigm has taught us. Personally I dislike events because they are not first class objects but some kind of background compiler voodoo.

When it comes to writing against an event driven API like the Kinect for Windows SDK you don’t have much of a choice but programming against those events. But wait, there is this wonderful ReactiveExtensions library that comes to the rescue. These classes and extension methods give you the possibility to easily wrap an event in an object and furthermore the library provides you with tons of handy methods that you don’t have to code yourself.

So I decided to write my own IObservable extension methods to extend the Kinect API with the ReactiveExtensions programming model.
Here are two methods to give you an idea of what I’m talking about:

public static IObservable<AllFramesReadyEventArgs> GetAllFramesReadyObservable(this KinectSensor kinectSensor)
{
   if(kinectSensor == null) throw new ArgumentNullException("kinectSensor");

   return Observable.FromEventPattern<AllFramesReadyEventArgs>(h => kinectSensor.AllFramesReady += h,
                                                               h => kinectSensor.AllFramesReady -= h)
                    .Select(e => e.EventArgs);
}

public static IObservable<ColorImageFrameReadyEventArgs> GetColorFrameReadyObservable(this KinectSensor kinectSensor)
{
   if (kinectSensor == null) throw new ArgumentNullException("kinectSensor");

   return Observable.FromEventPattern<ColorImageFrameReadyEventArgs>(
                                                           h => kinectSensor.ColorFrameReady += h,
                                                           h => kinectSensor.ColorFrameReady -= h)
                    .Select(e => e.EventArgs);
}
[...]

And so on…

Linq ExpressionTrees

I was faced with the following situation: I’ve entered a .NET/WPF project that was already running two years. My tasks were to maintain and improve the business and data access layer. The application had to deal with files and due to the fact that the Sql Server 2008 FileTable feature should NOT be used, we saved file references with the entities that were associated to files in the database. One entity could reference up to 14 different files.
At some point of time a new feature request arrived: Delete all files that are not referenced by an entity in the database from disk.

My solution:
I’ve been too lazy to code each database query to check whether the content of any property1 to property14 of table (entity) x contained a reference to an existing file. And what if another file reference property will be added in the future?
Instead I’ve implemented a FilePath attribute and decorated all the properties of the entity that represented FileReferences. With the help of Reflection and ExpreesionTrees I was able to construct database queries at runtime to query for columns that contained a file reference from disk.
This code example shows the basic idea:

public static class ExpressionExample
{
 public static void Do()
 {
   var someClassSuccess = new SomeClass { A = 2, B = "Hello" };
   var someClassNull = new SomeClass { A = 2 };
   var someClassFailure = new SomeClass { A = 2, B = "Foo" };

   typeof(SomeClass).GetProperties()
                    .Where(p => p.IsDefined(typeof(FilePathAttribute), false))
                    .ExecuteForEach(p =>
                    {
                      var expressionParameter = Expression.Parameter(typeof(SomeClass), "lambda");
                      var parameter = new [] { expressionParameter };
                      var propertyAccess = Expression.Property(expressionParameter, p);
                      var nullCheck = Expression.NotEqual(propertyAccess, Expression.Constant(null, p.PropertyType));
                      var nullCheckLambda = Expression.Lambda<Func<SomeClass, Boolean>>(nullCheck, parameter);

                      var containsMethodInfo = typeof(String).GetMethod("Contains", new[] { typeof(String) });
                      var contains = Expression.Call(propertyAccess, containsMethodInfo, Expression.Constant("ell"));
                      var containsLambda = Expression.Lambda<Func<SomeClass, Boolean>>(contains, new[] { expressionParameter });

                     var predicate = Expression.Lambda<Func<SomeClass, Boolean>>(
                                     Expression.AndAlso(nullCheckLambda.Body, containsLambda.Body), parameter);
                     Console.WriteLine(predicate.ToString());
                     Console.WriteLine("Success: " + predicate.Compile().Invoke(someClassSuccess));
                     Console.WriteLine("Null: " + predicate.Compile().Invoke(someClassNull));
                     Console.WriteLine("Failure: " + predicate.Compile().Invoke(someClassFailure));
                   });
 }

[AttributeUsage(AttributeTargets.Property)]
 class FilePathAttribute : Attribute
 {

 }

 class SomeClass
 {
    public int A { get; set; }

   [FilePath]
   public string B { get; set; }
 }
 }