/// <summary> /// Creates a new Gestures Handler /// </summary> /// <param name="manager">The manager that controls the handler</param> internal KinectGestureTracker(OpenNIManager manager) { //Assign the manager _manager = manager; manager.Deactivated += new EventHandler<EventArgs>(manager_Deactivated); //Add the wave recognizer as this will start a session. //_startSessionGenerator = manager.ONIContext.FindExistingNode(NodeType.Gesture) as GestureGenerator; _startSessionGenerator = new GestureGenerator(manager.ONIContext); _startSessionGenerator.AddGesture("Wave"); _startSessionGenerator.AddGesture("Click"); _startSessionGenerator.GestureRecognized += new GestureGenerator.GestureRecognizedHandler(_startSessionGenerator_GestureRecognized); _startSessionGenerator.GestureChanged += new StateChangedHandler(_startSessionGenerator_GestureChanged); _startSessionGenerator.GestureProgress += new GestureGenerator.GestureProgressHandler(_startSessionGenerator_GestureProgress); _startSessionGenerator.GenerationRunningChanged += new StateChangedHandler(_startSessionGenerator_GenerationRunningChanged); _startSessionGenerator.NewDataAvailable += new StateChangedHandler(_startSessionGenerator_NewDataAvailable); //The gesture generator will detect gestures after the session has been activated. _gestureGenerator = new GestureGenerator(manager.ONIContext); _gestureGenerator.GestureRecognized += new GestureGenerator.GestureRecognizedHandler(_gestureGenerator_GestureRecognized); _gestureGenerator.GestureChanged += new StateChangedHandler(_gestureGenerator_GestureChanged); _gestureGenerator.GestureProgress += new GestureGenerator.GestureProgressHandler(_gestureGenerator_GestureProgress); _gestureGenerator.GenerationRunningChanged += new StateChangedHandler(_gestureGenerator_GenerationRunningChanged); _gestureGenerator.NewDataAvailable += new StateChangedHandler(_gestureGenerator_NewDataAvailable); _handGestures = new List<HandGesture>(); }
/// <summary> /// Destructor /// </summary> public void Dispose() { File.Delete(this._xmlFileName); if (this._hands != null) { this._hands.Dispose(); this._hands = null; } if (this._gesture != null) { this._gesture.Dispose(); this._gesture = null; } this._context.Dispose(); this._context = null; }
/// <summary> /// Constructor /// </summary> public KinectWrap(KinectWrapProperties properties) { string xml; if (properties == null) { throw new Exception("KinectWrapProperties not provided"); } if (properties.HasGestureRecognition && properties.Gestures == null) { throw new Exception("KinectWrapProperties Gestures must at least have 1 gesture"); } this._resources = new ResourceManager(typeof(XmlFiles)); xml = this._resources.GetString("openniconfig"); this._xmlFileName = Guid.NewGuid().ToString().Replace("{", "").Replace("}", "").Replace("-", "") + ".xml"; this._xmlFileName = System.Reflection.Assembly.GetExecutingAssembly().Location + this._xmlFileName; using (StreamWriter wr = new StreamWriter(this._xmlFileName)) { wr.Write(xml); } this._context = new OpenNI.Context(this._xmlFileName); if (properties.HasHandTracking) { this._hands = new OpenNI.HandsGenerator(this._context); this._hands.HandUpdate += new OpenNI.HandsGenerator.HandUpdateHandler( delegate(OpenNI.ProductionNode node, uint id, ref OpenNI.Point3D position, float fTime) { if (properties.HandTrackHandler != null) { properties.HandTrackHandler.Invoke(new Point3D() { X = position.X, Y = position.Y, Z = position.Z }); } }); if (!properties.HasGestureRecognition) { this._hands.HandCreate += new OpenNI.HandsGenerator.HandCreateHandler( delegate(OpenNI.ProductionNode node, uint id, ref OpenNI.Point3D position, float fTime) { this._hands.StartTracking(ref position); }); } } if (properties.HasGestureRecognition) { this._gesture = new OpenNI.GestureGenerator(this._context); foreach (GestureType g in properties.Gestures) { switch (g) { case GestureType.Wave: { this._gesture.AddGesture("Wave"); break; } default: { break; } } } this._gesture.GestureRecognized += new OpenNI.GestureGenerator.GestureRecognizedHandler( delegate(OpenNI.ProductionNode node, string strGesture, ref OpenNI.Point3D idPosition, ref OpenNI.Point3D endPosition) { if (properties.GestureHandler != null) { GestureType gt; switch (strGesture) { case "Wave": { gt = GestureType.Wave; break; } default: { gt = GestureType.Wave; break; } } properties.GestureHandler.Invoke(gt, new Point3D() { X = endPosition.X, Y = endPosition.Y, Z = endPosition.Z }); } if (properties.HasHandTracking) { this._hands.StartTracking(ref endPosition); } }); } if (properties.HasHandTracking) { this._hands.StartGenerating(); } if (properties.HasGestureRecognition) { this._gesture.StartGenerating(); } System.Threading.Thread th = new System.Threading.Thread(delegate() { try { while (true) { this._context.WaitAnyUpdateAll(); } } finally { this.Dispose(); } }); th.Start(); }
private void Track(AsyncStateData asyncData) { asyncData.Running = true; InitOpenNi(asyncData); while (!asyncData.Canceled) { try { _niContext.WaitAndUpdateAll(); // update image metadata _imageNode.GetMetaData(_imageMeta); _depthNode.GetMetaData(_depthMeta); //_sceneNode..GetMetaData(_sceneMeta); GestureGenerator gg = new GestureGenerator(_niContext); gg.AddGesture("Wave"); gg.GestureRecognized += new GestureGenerator.GestureRecognizedHandler(gg_GestureRecognized); gg.StartGenerating(); _depthHist.Update(_depthMeta); // _sceneMap.Update(_sceneMeta); // continue update on UI thread asyncData.AsyncOperation.SynchronizationContext.Send( delegate { // Must be called on the synchronization thread. CopyWritableBitmap(_imageMeta, _rgbImageSource); // CopyWritableBitmap(_depthMeta, _depthImageSource); _depthHist.Paint(_depthMeta, _depthImageSource); //CopyWritableBitmap(_sceneMeta, _sceneImageSource); //_sceneMap.Paint(_sceneMeta, _sceneImageSource); InvokeUpdateViewPort(EventArgs.Empty); }, null); } catch(Exception ex) { System.Diagnostics.Debug.WriteLine("An error has occured in Track: " + ex.Message); } } asyncData.Running = false; asyncData.AsyncOperation.PostOperationCompleted(evt => InvokeTrackinkgCompleted(EventArgs.Empty), null); }
void Start() { Debug.Log ("Start(): Initializing nodes."); this.context = new Context (SAMPLE_XML_FILE); this.depth = context.FindExistingNode (NodeType.Depth) as DepthGenerator; if (this.depth == null) { Debug.LogError ("Viewer must have a depth node!"); } this.hands = context.FindExistingNode (NodeType.Hands) as HandsGenerator; if (this.hands == null) { Debug.LogError ("Viewer must have a hands node!"); } this.gestures = context.FindExistingNode (NodeType.Gesture) as GestureGenerator; if (this.gestures == null) { Debug.LogError ("Viewer must have a gestures node!"); } this.hands.HandCreate += new HandsGenerator.HandCreateHandler (hands_HandCreate); this.hands.HandUpdate += new HandsGenerator.HandUpdateHandler (hands_HandUpdate); this.hands.HandDestroy += new HandsGenerator.HandDestroyHandler (hands_HandDestroy); this.gestures.AddGesture ("Wave"); this.gestures.AddGesture ("RaiseHand"); this.gestures.GestureRecognized += new GestureGenerator.GestureRecognizedHandler (gestures_GestureRecognized); this.gestures.StartGenerating (); }