Skip to content

Commit 6d178a9

Browse files
lab12
1 parent b13fa20 commit 6d178a9

7 files changed

+587
-4
lines changed
130 KB
Binary file not shown.

Kinect2Sample/GestureDetector.cs

+247
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,247 @@
1+
//------------------------------------------------------------------------------
2+
// <copyright file="GestureDetector.cs" company="Microsoft">
3+
// Copyright (c) Microsoft Corporation. All rights reserved.
4+
// </copyright>
5+
//------------------------------------------------------------------------------
6+
7+
namespace Kinect2Sample
8+
{
9+
using System;
10+
using System.Collections.Generic;
11+
using Microsoft.Kinect;
12+
using Microsoft.Kinect.VisualGestureBuilder;
13+
using WindowsPreview.Kinect;
14+
using Windows.UI.Xaml;
15+
using WindowsPreview.Data;
16+
17+
/// <summary>
18+
/// Gesture Detector class which listens for VisualGestureBuilderFrame events from the service
19+
/// and updates the associated GestureResultView object with the latest results for the gesture
20+
/// </summary>
21+
public class GestureDetector : IDisposable
22+
{
23+
//public RoutedEventHandler GestureRecognized { get; set; }
24+
25+
//important lab 13
26+
/// <summary> Path to the gesture database that was trained with VGB </summary>
27+
private readonly string gestureDatabase = @"Database\HandsAboveHead.gbd";
28+
29+
//important lab 13
30+
/// <summary> Name of the discrete gesture in the database that we want to track </summary>
31+
private readonly string handsAboveHeadGestureName = "HandsAboveHead";
32+
33+
/// <summary> Gesture frame source which should be tied to a body tracking ID </summary>
34+
private VisualGestureBuilderFrameSource vgbFrameSource = null;
35+
36+
/// <summary> Gesture frame reader which will handle gesture events coming from the sensor </summary>
37+
private VisualGestureBuilderFrameReader vgbFrameReader = null;
38+
39+
/// <summary>
40+
/// Initializes a new instance of the GestureDetector class along with the gesture frame source and reader
41+
/// </summary>
42+
/// <param name="kinectSensor">Active sensor to initialize the VisualGestureBuilderFrameSource object with</param>
43+
/// <param name="gestureResultView">GestureResultView object to store gesture results of a single body to</param>
44+
public GestureDetector(KinectSensor kinectSensor, GestureResultView gestureResultView)
45+
{
46+
if (kinectSensor == null)
47+
{
48+
throw new ArgumentNullException("kinectSensor");
49+
}
50+
51+
if (gestureResultView == null)
52+
{
53+
throw new ArgumentNullException("gestureResultView");
54+
}
55+
56+
this.GestureResultView = gestureResultView;
57+
58+
// create the vgb source. The associated body tracking ID will be set when a valid body frame arrives from the sensor.
59+
this.vgbFrameSource = new VisualGestureBuilderFrameSource(kinectSensor, 0);
60+
this.vgbFrameSource.TrackingIdLost += this.Source_TrackingIdLost;
61+
62+
// open the reader for the vgb frames
63+
this.vgbFrameReader = this.vgbFrameSource.OpenReader();
64+
if (this.vgbFrameReader != null)
65+
{
66+
this.vgbFrameReader.IsPaused = true;
67+
this.vgbFrameReader.FrameArrived += this.Reader_GestureFrameArrived;
68+
}
69+
70+
// load the 'Seated' gesture from the gesture database
71+
using (VisualGestureBuilderDatabase database = new VisualGestureBuilderDatabase(this.gestureDatabase))
72+
{
73+
// we could load all available gestures in the database with a call to vgbFrameSource.AddGestures(database.AvailableGestures),
74+
// but for this program, we only want to track one discrete gesture from the database, so we'll load it by name
75+
//foreach (Gesture gesture in database.AvailableGestures)
76+
//{
77+
// if (gesture.Name.Equals(this.seatedGestureName))
78+
// {
79+
// this.vgbFrameSource.AddGesture(gesture);
80+
// }
81+
//}
82+
83+
// we could load all available gestures in the database with a call to vgbFrameSource.AddGestures(database.AvailableGestures),
84+
// but for this program, we only want to track one discrete gesture from the database, so we'll load it by name
85+
foreach (Gesture gesture in database.AvailableGestures)
86+
{
87+
if (gesture.Name.Equals(this.handsAboveHeadGestureName))
88+
{
89+
this.vgbFrameSource.AddGesture(gesture);
90+
}
91+
}
92+
}
93+
}
94+
95+
/// <summary> Gets the GestureResultView object which stores the detector results for display in the UI </summary>
96+
public GestureResultView GestureResultView { get; private set; }
97+
98+
/// <summary>
99+
/// Gets or sets the body tracking ID associated with the current detector
100+
/// The tracking ID can change whenever a body comes in/out of scope
101+
/// </summary>
102+
public ulong TrackingId
103+
{
104+
get
105+
{
106+
return this.vgbFrameSource.TrackingId;
107+
}
108+
109+
set
110+
{
111+
if (this.vgbFrameSource.TrackingId != value)
112+
{
113+
this.vgbFrameSource.TrackingId = value;
114+
}
115+
}
116+
}
117+
118+
/// <summary>
119+
/// Gets or sets a value indicating whether or not the detector is currently paused
120+
/// If the body tracking ID associated with the detector is not valid, then the detector should be paused
121+
/// </summary>
122+
public bool IsPaused
123+
{
124+
get
125+
{
126+
return this.vgbFrameReader.IsPaused;
127+
}
128+
129+
set
130+
{
131+
if (this.vgbFrameReader.IsPaused != value)
132+
{
133+
this.vgbFrameReader.IsPaused = value;
134+
}
135+
}
136+
}
137+
138+
public bool GestureRecognized
139+
{
140+
get
141+
{
142+
return this.GestureRecognized;
143+
}
144+
145+
set
146+
{
147+
if (this.GestureRecognized != value)
148+
{
149+
this.GestureRecognized = value;
150+
}
151+
}
152+
}
153+
154+
/// <summary>
155+
/// Disposes all unmanaged resources for the class
156+
/// </summary>
157+
public void Dispose()
158+
{
159+
this.Dispose(true);
160+
GC.SuppressFinalize(this);
161+
}
162+
163+
/// <summary>
164+
/// Disposes the VisualGestureBuilderFrameSource and VisualGestureBuilderFrameReader objects
165+
/// </summary>
166+
/// <param name="disposing">True if Dispose was called directly, false if the GC handles the disposing</param>
167+
protected virtual void Dispose(bool disposing)
168+
{
169+
if (disposing)
170+
{
171+
if (this.vgbFrameReader != null)
172+
{
173+
this.vgbFrameReader.FrameArrived -= this.Reader_GestureFrameArrived;
174+
this.vgbFrameReader.Dispose();
175+
this.vgbFrameReader = null;
176+
}
177+
178+
if (this.vgbFrameSource != null)
179+
{
180+
this.vgbFrameSource.TrackingIdLost -= this.Source_TrackingIdLost;
181+
this.vgbFrameSource.Dispose();
182+
this.vgbFrameSource = null;
183+
}
184+
}
185+
}
186+
187+
/// <summary>
188+
/// Handles gesture detection results arriving from the sensor for the associated body tracking Id
189+
/// </summary>
190+
/// <param name="sender">object sending the event</param>
191+
/// <param name="e">event arguments</param>
192+
private void Reader_GestureFrameArrived(object sender, VisualGestureBuilderFrameArrivedEventArgs e)
193+
{
194+
VisualGestureBuilderFrameReference frameReference = e.FrameReference;
195+
using (VisualGestureBuilderFrame frame = frameReference.AcquireFrame())
196+
{
197+
if (frame != null)
198+
{
199+
// get the discrete gesture results which arrived with the latest frame
200+
IReadOnlyDictionary<Gesture, DiscreteGestureResult> discreteResults = frame.DiscreteGestureResults;
201+
202+
if (discreteResults != null)
203+
{
204+
// we only have one gesture in this source object, but you can get multiple gestures
205+
foreach (Gesture gesture in this.vgbFrameSource.Gestures)
206+
{
207+
//if (gesture.Name.Equals(this.seatedGestureName) && gesture.GestureType == GestureType.Discrete)
208+
//{
209+
// DiscreteGestureResult result = null;
210+
// discreteResults.TryGetValue(gesture, out result);
211+
212+
// if (result != null)
213+
// {
214+
// // update the GestureResultView object with new gesture result values
215+
// this.GestureResultView.UpdateGestureResult(true, result.Detected, result.Confidence);
216+
// }
217+
//}
218+
219+
if (gesture.Name.Equals(this.handsAboveHeadGestureName) && gesture.GestureType == GestureType.Discrete)
220+
{
221+
DiscreteGestureResult result = null;
222+
discreteResults.TryGetValue(gesture, out result);
223+
224+
if (result != null)
225+
{
226+
// update the GestureResultView object with new gesture result values
227+
this.GestureResultView.UpdateGestureResult(true, result.Detected, result.Confidence);
228+
}
229+
}
230+
}
231+
}
232+
}
233+
}
234+
}
235+
236+
/// <summary>
237+
/// Handles the TrackingIdLost event for the VisualGestureBuilderSource object
238+
/// </summary>
239+
/// <param name="sender">object sending the event</param>
240+
/// <param name="e">event arguments</param>
241+
private void Source_TrackingIdLost(object sender, TrackingIdLostEventArgs e)
242+
{
243+
// update the GestureResultView object to show the 'Not Tracked' image in the UI
244+
this.GestureResultView.UpdateGestureResult(false, false, 0.0f);
245+
}
246+
}
247+
}

0 commit comments

Comments
 (0)