Click here to Skip to main content
15,886,578 members
Articles / Desktop Programming / WPF

Sitting posture recognition with Kinect sensor

Rate me:
Please Sign up or sign in to vote.
4.78/5 (12 votes)
24 Oct 2011CPOL6 min read 67K   3.4K   27  
Recognition of concentrating, non-concentrating, sleeping, and raise-hand postures.
using OpenNI;
using System;
using System.Timers;
namespace Nui.Vision
{
	public class NuiHandTracker : NuiContext
	{
		public delegate void HoverHandler(object sender, NuiPositionEventArgs e);
		public delegate void PushHandler(object sender, NuiPositionEventArgs e);
		private GestureGenerator _gestureGenerator;
		private HandsGenerator _handsGenerator;
		private Timer _timer = new Timer(300.0);
		private float _pushThreshold = 100f;
		private float _currentX;
		private float _currentY;
		private float _currentZ;
		private float _previousZ;
		private bool _isFirstPush;
		public event NuiHandTracker.HoverHandler Hover;
		public event NuiHandTracker.PushHandler Push;
		public NuiHandTracker(string configuration) : base(configuration)
		{
		}
		protected override void ConstrutorTemplate()
		{
			this._timer.Elapsed += new ElapsedEventHandler(this.Timer_Elapsed);
			this._timer.Start();
			this._gestureGenerator = new GestureGenerator(base.Context);
			this._gestureGenerator.AddGesture("Wave");
            this._gestureGenerator.GestureRecognized+=new EventHandler<GestureRecognizedEventArgs>(_gestureGenerator_GestureRecognized);
			//this._gestureGenerator.add_GestureRecognized(new System.EventHandler<GestureRecognizedEventArgs>(this.GestureGenerator_GestureRecognized));
			this._handsGenerator = new HandsGenerator(base.Context);
			//this._handsGenerator.add_HandCreate(new System.EventHandler<HandCreateEventArgs>(this.HandsGenerator_HandCreate));
           // this._handsGenerator.HandCreate+=new EventHandler<HandCreateEventArgs>(_handsGenerator_HandCreate);
            this._handsGenerator.HandUpdate+=new EventHandler<HandUpdateEventArgs>(_handsGenerator_HandUpdate);
			//this._handsGenerator.add_HandUpdate(new System.EventHandler<HandUpdateEventArgs>(this.HandsGenerator_HandUpdate));
            this._handsGenerator.HandDestroy+=new EventHandler<HandDestroyEventArgs>(_handsGenerator_HandDestroy);
			//this._handsGenerator.add_HandDestroy(new System.EventHandler<HandDestroyEventArgs>(this.HandsGenerator_HandDestroy));
			this._gestureGenerator.StartGenerating();
			this._handsGenerator.StartGenerating();
			base.State = NuiState.LookingForPose;
		}

        void _handsGenerator_HandDestroy(object sender, HandDestroyEventArgs e)
        {
            this._gestureGenerator.AddGesture("Wave");
        }

        void _handsGenerator_HandUpdate(object sender, HandUpdateEventArgs e)
        {
            Point3D position = e.Position;
            this._currentX = position.X;
            position = e.Position;
            this._currentY = position.Y;
            position = e.Position;
            this._currentZ = position.Z;
            this.Hover(this, new NuiPositionEventArgs(this._currentX, this._currentY, this._currentZ));
        }

        void _handsGenerator_HandCreate(object sender, HandCreateEventArgs e)
        {
          
        }

        void _gestureGenerator_GestureRecognized(object sender, GestureRecognizedEventArgs e)
        {
            this._handsGenerator.StartTracking(e.EndPosition);
            this._gestureGenerator.RemoveGesture(e.Gesture);
            base.State = NuiState.TrackingGesture;
        }
		protected override void ThreadTemplate()
		{
		}
		private void Timer_Elapsed(object sender, ElapsedEventArgs e)
		{
			if (System.Math.Abs(this._currentZ - this._previousZ) > this._pushThreshold)
			{
				if (this._isFirstPush)
				{
					if (this.Push != null)
					{
						this.Push(this, new NuiPositionEventArgs(this._currentX, this._currentY, this._currentZ));
					}
					this._isFirstPush = false;
				}
				else
				{
					this._isFirstPush = true;
				}
			}
			this._previousZ = this._currentZ;
		}
		private void GestureGenerator_GestureRecognized(object sender, GestureRecognizedEventArgs e)
		{
			this._handsGenerator.StartTracking(e.EndPosition);
			this._gestureGenerator.RemoveGesture(e.Gesture);
			base.State = NuiState.TrackingGesture;
		}
		private void HandsGenerator_HandCreate(object sender, HandCreateEventArgs e)
		{
		}
		private void HandsGenerator_HandUpdate(object sender, HandUpdateEventArgs e)
		{
			Point3D position = e.Position;
			this._currentX = position.X;
			position = e.Position;
			this._currentY = position.Y;
			position = e.Position;
			this._currentZ = position.Z;
			this.Hover(this, new NuiPositionEventArgs(this._currentX, this._currentY, this._currentZ));
		}
		private void HandsGenerator_HandDestroy(object sender, HandDestroyEventArgs e)
		{
			this._gestureGenerator.AddGesture("Wave");
		}
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) Altair
Russian Federation Russian Federation
Ph.D., Image processing, Neural net, C++, C#, OPenCv,ASP.Net MVC, JScript, Qt,SQL,Kinnect,SilvelLight,

Comments and Discussions