Kinect

imageimage

I have the opportunity to borrow a Kinect hardware  – to make an application( I will make a simple anti-theft application).

The steps for developing in Kinect are:

  • Download the SDK from http://www.microsoft.com/en-us/kinectforwindows/develop/overview.aspx – it contains the drivers also
  • Connect Kinect to USB , plug in the socket
  • Run the Kinect Explorer or Kinect Shape Game from Kinect SDK Sample Browser installed at 1.  Ensure it works.
  • Read the Kinect Explorer source –it is SO clear!
  • Read documentation – finally, you should RTFM Winking smile

Optional resources:

  1. http://kinectcontrib.codeplex.com/ – Visual Studio template for Skeleton, Audio, Video. Simple example that works ( simpler than Kinect Explorer !)
  2. http://c4fkinect.codeplex.com/ – added methods to Kinect.
  3. http://kinecttoolbox.codeplex.com/ – detection of gesture.

Example 1: Integrating saving image in Kinect Explorer when a skeleton is detected

Add reference to optional resource 2( either download , either via Nuget)

Search for  KinectAllFramesReady in KinectSkeletonViewer.xaml.cs and put this code

						var takePic = this.skeletonData.Count(item => item.TrackingState == SkeletonTrackingState.Tracked) > 0;

						if (takePic)
						{
							DateTime imgDate = DateTime.Now;
							string imageName = "andrei" + imgDate.ToString("yyyyMMdd_HHmmss") + ".jpg";
							if (!File.Exists(imageName))
							{
								using (var image = e.OpenColorImageFrame())
								{
									if (image != null && this.skeletonData.Length > 0 && this.skeletonData.Count(item => item.TrackingState != SkeletonTrackingState.NotTracked) > 0)
										if (image != null && takePic)
										{
											var x = image.ToBitmapSource();
											var b = Save(x, ImageFormat.Jpeg);

											var t = Task.Factory.StartNew(

												(img) =>
												{

													imgDate = DateTime.Now;
													imageName = "andrei" + imgDate.ToString("yyyyMMdd_HHmmss") + ".jpg";
													if (File.Exists(imageName))
														return;
													byte[] i = img as byte[];
													if (i != null)
													{
														File.WriteAllBytes(imageName, i);
													};
												}, b);
											//t.Start();
										}
								}
							}
					}

Example 2 : Detecting circle by right hand in Kinect Explorer when a skeleton is detected

Add reference to optional resource 3( either download , either via Nuget)

Download circleKB.save file and put in your project. Ensure “Copy to output directory” is “copy always/copy if newer”

Add a variable named

TemplatedGestureDetector circleGestureRecognizer;

In KinectSkeletonViewer.xaml.cs in constructor put

			using (Stream recordStream = File.Open("circleKB.save", FileMode.Open))
			{
				circleGestureRecognizer = new TemplatedGestureDetector("Circle", recordStream);
				circleGestureRecognizer.OnGestureDetected += new Action<string>(circleGestureRecognizer_OnGestureDetected);
//TODO : implement circleGestureRecognizer_OnGestureDetected : void circleGestureRecognizer_OnGestureDetected(string obj)
			}

Search for KinectAllFramesReady in KinectSkeletonViewer.xaml.cs and put this code


foreach (Joint joint in skeleton.Joints)//existing code
							{
								Point mappedPoint = this.GetPosition2DLocation(depthImageFrame, joint.Position);//existing code
								jointMapping[joint.JointType] = new JointMapping//existing code
									{
										Joint = joint,
										MappedPoint = mappedPoint
									};
//new code from here:
									 if ((joint.TrackingState == JointTrackingState.Tracked) && (joint.JointType == JointType.HandRight))
									{
											circleGestureRecognizer.Add(joint.Position, Kinect);
									}

							}

That will be all…
If you have developed with Kinect, please share your sources as comments.