public void RenderSkeleton(DrawingContext dc, Skeleton[] skeletons, DrawingGroup drawingGroup) { // Draw a transparent background to set the render size dc.DrawRectangle(Brushes.White, null, new Rect(0.0, 0.0, SkeletonHandler.RenderWidth, SkeletonHandler.RenderHeight)); bool noSkelTracked = true; if (skeletons.Length != 0) { Skeleton skel = null; ChooseSkeleton(); // Track this skeleton foreach (Skeleton skeleton in skeletons) { if (nearestSkeleton == skeleton.TrackingId) { skel = skeleton; } } if (skel != null) { SkeletonHandler.RenderClippedEdges(skel, dc); if (skel.TrackingState == SkeletonTrackingState.Tracked) { skeletonHandler.DrawBonesAndJoints(skel, dc); noSkelTracked = false; lastSkeletonTimeStamp = DateTime.Now; } else if (skel.TrackingState == SkeletonTrackingState.PositionOnly) { dc.DrawEllipse( skeletonHandler.centerPointBrush, null, skeletonHandler.SkeletonPointToScreen(skel.Position), SkeletonHandler.BodyCenterThickness, SkeletonHandler.BodyCenterThickness); noSkelTracked = false; lastSkeletonTimeStamp = DateTime.Now; } } } if (noSkelTracked) { if ((DateTime.Now - lastSkeletonTimeStamp).TotalMilliseconds > 1000) { speechInteraction.fsm.Fire(SpeechInteraction.Command.FramesNotReady); lastSkeletonTimeStamp = DateTime.Now; } } else { speechInteraction.fsm.Fire(SpeechInteraction.Command.FramesReady); } // prevent drawing outside of our render area drawingGroup.ClipGeometry = new RectangleGeometry(new Rect(0.0, 0.0, SkeletonHandler.RenderWidth, SkeletonHandler.RenderHeight)); }
/// <summary> /// Called when the KinectSensorChooser gets a new sensor /// </summary> /// <param name="sender">sender of the event</param> /// <param name="args">event arguments</param> private void SensorChooserOnKinectChanged(object sender, KinectChangedEventArgs args) { if (args.OldSensor != null) { try { args.OldSensor.AllFramesReady -= this.SensorAllFramesReady; args.OldSensor.DepthStream.Disable(); args.OldSensor.ColorStream.Disable(); args.OldSensor.SkeletonStream.Disable(); // Create the background removal stream to process the data and remove background, and initialize it. if (null != this.backgroundRemovedColorStream) { this.backgroundRemovedColorStream.BackgroundRemovedFrameReady -= this.BackgroundRemovedFrameReadyHandler; this.backgroundRemovedColorStream.Dispose(); this.backgroundRemovedColorStream = null; } } catch (InvalidOperationException) { // KinectSensor might enter an invalid state while enabling/disabling streams or stream features. // E.g.: sensor might be abruptly unplugged. } } if (args.NewSensor != null) { try { args.NewSensor.DepthStream.Enable(DepthFormat); args.NewSensor.ColorStream.Enable(ColorFormat); args.NewSensor.SkeletonStream.Enable(); this.backgroundRemovedColorStream = new BackgroundRemovedColorStream(args.NewSensor); this.backgroundRemovedColorStream.Enable(ColorFormat, DepthFormat); // Allocate space to put the depth, color, and skeleton data we'll receive if (null == this.skeletons) { this.skeletons = new Skeleton[args.NewSensor.SkeletonStream.FrameSkeletonArrayLength]; } // Add an event handler to be called when the background removed color frame is ready, so that we can // composite the image and output to the app this.backgroundRemovedColorStream.BackgroundRemovedFrameReady += this.BackgroundRemovedFrameReadyHandler; // Add an event handler to be called whenever there is new depth frame data args.NewSensor.AllFramesReady += this.SensorAllFramesReady; args.NewSensor.SkeletonFrameReady += this.SensorSkeletonFrameReady; this.sensor = args.NewSensor; this.sensor.Start(); skeletonHandler = new SkeletonHandler(sensor, speechInteraction); try { /*args.NewSensor.DepthStream.Range = this.checkBoxNearMode.IsChecked.GetValueOrDefault() * ? DepthRange.Near * : DepthRange.Default; * args.NewSensor.SkeletonStream.EnableTrackingInNearRange = true;*/ args.NewSensor.DepthStream.Range = DepthRange.Default; args.NewSensor.SkeletonStream.EnableTrackingInNearRange = true; } catch (InvalidOperationException) { // Non Kinect for Windows devices do not support Near mode, so reset back to default mode. args.NewSensor.DepthStream.Range = DepthRange.Default; args.NewSensor.SkeletonStream.EnableTrackingInNearRange = false; } this.speechHandler = new SpeechHandler(speechInteraction, args.NewSensor); this.speechHandler.InitializeSpeechRecognizer(); this.speechInteraction.speechHandler = speechHandler; } catch (InvalidOperationException) { // KinectSensor might enter an invalid state while enabling/disabling streams or stream features. // E.g.: sensor might be abruptly unplugged. } } }