Notice: MediaWiki has been updated. Report any rough edges to marcan@marcan.st

CSharp Wrapper

From OpenKinect
Jump to: navigation, search

About

This effort will provide a wrapper to access the Kinect through C# or most other .NET languages such as VB.NET. Instead of being a light wrapper, this aims to bring the capabilities of the low level wrapper and let it be used in a way that conforms to .NET patterns. In the following page, it is assumed that you are using C#, but the library should work just as well with any other .NET language that has similar language constructs such as events, threading etc. VB.NET is a good example of an alternative .NET language.

Who is involved

Coordinator/Development: Aditya Gaddam (User:LostInCake)

Media

Screenshot of demo application.

Installation/Configuration

NOTE: As of June 02, 2011, the Windows version is running fairly slow. I am not sure if it's just the drivers on windows that are to blame. But the demo DOES run.

General Overview

  1. Build the base library. Please refer to Getting Started for this.
  2. Copy libfreenect.so (linux) or libfreenect.dll (windows) to {Libfreenect Directory}/wrappers/csharp/bin
  3. If you are on windows, make sure libusb0.dll is in your PATH somewhere. Alternatively, you can copy over libusb0.dll from your libusb directory to {Libfreenect Directory}/wrappers/csharp/bin
  4. Build C# wrapper (freenect.dll) and demo application (KinectDemo) using the VS2008 or VS2010 solutions. Detailed directions are below.
  5. Run. Enjoy

Building C# wrapper and demo application

The following instructions use MonoDevelop. If you have access to Visual Studio or Visual Studio Express, you can safely follow the same instructions and perform analogous steps inside the Visual Studio IDE.

  1. Download and install MonoDevelop. In Ubuntu, this can be found under the Software Center.
  2. Browse to {Libfreenect Directory}/wrappers/csharp/src/lib/ and then use the solutions in either VS2008 or VS2010 depending on your version of MonoDevelop.
    1. Once you build freenectlib, a freenect.dll should appear in {Libfreenect Directory}/wrappers/csharp/bin
  3. Download OpenTK binaries. Place OpenTK.dll and OpenTK.GLControl.dll in {Libfreenect Directory}/wrappers/csharp/support. On Linux, you can use fetch_opentk.sh in {Libfreenect Directory}/wrappers/csharp/support/ to do this for you.
  4. Browse to {Libfreenect Directory}/wrappers/csharp/src/test/KinectDemo and then use the solutions in either VS2008 or VS2010 depending on your version of MonoDevelop.
    1. If you put the OpenTK dlls in the right place as instructed in the above steps, the solutions should build as is. Otherwise, you will have to fix references to OpenTK in the projects manually.
    2. Once you build KinectDemo, a KinectDemo.exe should appear in {Libfreenect Directory}/wrappers/csharp/bin

Sample Code

The following sample code provides a basic look at how the C# wrapper is used. Full a full blown example, please look at the KinectDemo application in the repository.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using System.Threading;
using freenect;
using System.Drawing;
using System.Diagnostics;

namespace KinectNetSample
{
	public class Sample : Form
	{
		
		/// <summary>
		/// Constructor
		/// </summary>
		public Sample()
		{
			// Initialize UI stuff
			this.InitializeComponents();
			
			// Find device count
			Console.WriteLine("There are {0} Kinect Devices Connected", Kinect.DeviceCount);
			
			if(Kinect.DeviceCount > 0)
			{
				// Connect to first device
				Kinect kinect = new Kinect(0);
				kinect.Open();
				
				// Setup event handlers
				kinect.VideoCamera.DataReceived += HandleKinectVideoCameraDataReceived;
				kinect.DepthCamera.DataReceived += HandleKinectDepthCameraDataReceived;
				
				// Start cameras
				kinect.VideoCamera.Start();
				kinect.DepthCamera.Start();

				// Set LED to Yellow
				kinect.LED.Color = LEDColor.Yellow;

				// Set tilt to halfway up
				kinect.Motor.Tilt = 0.5;
				
				// Start update thread
				Thread t = new Thread(new ThreadStart(delegate()
				{
					while(true)
					{
						// Update status of accelerometer/motor etc.
						kinect.UpdateStatus();

						// Process any pending events.
						Kinect.ProcessEvents();
					}
				}));
			}
		}

		/// <summary>
		/// Handle depth data
		/// </summary>
		/// <param name="sender">
		/// A <see cref="System.Object"/>
		/// </param>
		/// <param name="e">
		/// A <see cref="BaseCamera.DataReceivedEventArgs"/>
		/// </param>
		private void HandleKinectDepthCameraDataReceived (object sender, BaseCamera.DataReceivedEventArgs e)
		{
			Console.WriteLine("Depth data received at {0}", e.Timestamp);
			
			// Actual data is in e.Data
		}

		/// <summary>
		/// Handle video data
		/// </summary>
		/// <param name="sender">
		/// A <see cref="System.Object"/>
		/// </param>
		/// <param name="e">
		/// A <see cref="BaseCamera.DataReceivedEventArgs"/>
		/// </param>
		private void HandleKinectVideoCameraDataReceived (object sender, BaseCamera.DataReceivedEventArgs e)
		{
			Console.WriteLine("Video data received at {0}", e.Timestamp);
			
			// Actual data is in e.Data
		}
	}
}