-
Notifications
You must be signed in to change notification settings - Fork 46
/
SimpleGesture.cpp
302 lines (270 loc) · 8.25 KB
/
SimpleGesture.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
// Created by Heresy @ 2015/08/11
// Blog Page: https://kheresy.wordpress.com/2015/09/04/vgb-cpp-api/
// This sample is used to read the gesture databases from Visual Gesture Builder and detect gestures.
// Standard Library
#include <string>
#include <iostream>
// Kinect for Windows SDK Header
#include <Kinect.h>
#include <Kinect.VisualGestureBuilder.h>
using namespace std;
int main(int argc, char** argv)
{
#pragma region Sensor related code
// Get default Sensor
cout << "Try to get default sensor" << endl;
IKinectSensor* pSensor = nullptr;
if (GetDefaultKinectSensor(&pSensor) != S_OK)
{
cerr << "Get Sensor failed" << endl;
return -1;
}
// Open sensor
cout << "Try to open sensor" << endl;
if (pSensor->Open() != S_OK)
{
cerr << "Can't open sensor" << endl;
return -1;
}
#pragma endregion
#pragma region Body releated code
// Get body frame source
cout << "Try to get body source" << endl;
IBodyFrameSource* pBodyFrameSource = nullptr;
if (pSensor->get_BodyFrameSource(&pBodyFrameSource) != S_OK)
{
cerr << "Can't get body frame source" << endl;
return -1;
}
// Get the number of body
INT32 iBodyCount = 0;
if (pBodyFrameSource->get_BodyCount(&iBodyCount) != S_OK)
{
cerr << "Can't get body count" << endl;
return -1;
}
cout << " > Can trace " << iBodyCount << " bodies" << endl;
// Allocate resource for bodies
IBody** aBody = new IBody*[iBodyCount];
for (int i = 0; i < iBodyCount; ++i)
aBody[i] = nullptr;
// get body frame reader
cout << "Try to get body frame reader" << endl;
IBodyFrameReader* pBodyFrameReader = nullptr;
if (pBodyFrameSource->OpenReader(&pBodyFrameReader) != S_OK)
{
cerr << "Can't get body frame reader" << endl;
return -1;
}
#pragma endregion
#pragma region Visual Gesture Builder Database
// Load gesture dataase from File
wstring sDatabaseFile = L"test.gbd"; // Modify this file to load other file
IVisualGestureBuilderDatabase* pGestureDatabase = nullptr;
wcout << L"Try to load gesture database file " << sDatabaseFile << endl;
if (CreateVisualGestureBuilderDatabaseInstanceFromFile(sDatabaseFile.c_str(), &pGestureDatabase) != S_OK)
{
wcerr << L"Can't read database file " << sDatabaseFile << endl;
return -1;
}
// Get the number of gestures in database
UINT iGestureCount = 0;
cout << "Try to read gesture list" << endl;
if (pGestureDatabase->get_AvailableGesturesCount(&iGestureCount) != S_OK)
{
cerr << "Can't read the gesture count" << endl;
return -1;
}
if (iGestureCount == 0)
{
cerr << "There is no gesture in the database" << endl;
return -1;
}
// get the list of gestures
IGesture** aGestureList = new IGesture*[iGestureCount];
if (pGestureDatabase->get_AvailableGestures(iGestureCount, aGestureList) != S_OK)
{
cerr << "Can't read the gesture list" << endl;
return -1;
}
else
{
// output the gesture list
cout << "There are " << iGestureCount << " gestures in the database: " << endl;
GestureType mType;
const UINT uTextLength = 260; // magic number, if value smaller than 260, can't get name
wchar_t sName[uTextLength];
for (UINT i = 0; i < iGestureCount; ++i)
{
if (aGestureList[i]->get_GestureType(&mType) == S_OK)
{
if (mType == GestureType_Discrete)
cout << "\t[D] ";
else if (mType == GestureType_Continuous)
cout << "\t[C] ";
if (aGestureList[i]->get_Name(uTextLength, sName) == S_OK)
wcout << sName << endl;
}
}
}
#pragma endregion
#pragma region Gesture frame related code
// create for each possible body
IVisualGestureBuilderFrameSource** aGestureSources = new IVisualGestureBuilderFrameSource*[iBodyCount];
IVisualGestureBuilderFrameReader** aGestureReaders = new IVisualGestureBuilderFrameReader*[iBodyCount];
for (int i = 0; i < iBodyCount; ++i)
{
// frame source
aGestureSources[i] = nullptr;
if (CreateVisualGestureBuilderFrameSource(pSensor, 0, &aGestureSources[i]) != S_OK)
{
cerr << "Can't create IVisualGestureBuilderFrameSource" << endl;
return -1;
}
// set gestures
if (aGestureSources[i]->AddGestures(iGestureCount, aGestureList) != S_OK)
{
cerr << "Add gestures failed" << endl;
return -1;
}
// frame reader
aGestureReaders[i] = nullptr;
if (aGestureSources[i]->OpenReader(&aGestureReaders[i]) != S_OK)
{
cerr << "Can't open IVisualGestureBuilderFrameReader" << endl;
return -1;
}
}
#pragma endregion
// Enter main loop
int iStep = 0;
while (iStep < 100000)
{
// 4a. Get last frame
IBodyFrame* pBodyFrame = nullptr;
if (pBodyFrameReader->AcquireLatestFrame(&pBodyFrame) == S_OK)
{
++iStep;
// 4b. get Body data
if (pBodyFrame->GetAndRefreshBodyData(iBodyCount, aBody) == S_OK)
{
// 4c. for each body
for (int i = 0; i < iBodyCount; ++i)
{
IBody* pBody = aBody[i];
// check if is tracked
BOOLEAN bTracked = false;
if ((pBody->get_IsTracked(&bTracked) == S_OK) && bTracked)
{
// get tracking ID of body
UINT64 uTrackingId = 0;
if (pBody->get_TrackingId(&uTrackingId) == S_OK)
{
// get tracking id of gesture
UINT64 uGestureId = 0;
if (aGestureSources[i]->get_TrackingId(&uGestureId) == S_OK)
{
if (uGestureId != uTrackingId)
{
// assign traking ID if the value is changed
cout << "Gesture Source " << i << " start to track user " << uTrackingId << endl;
aGestureSources[i]->put_TrackingId(uTrackingId);
}
}
}
// Get gesture frame for this body
IVisualGestureBuilderFrame* pGestureFrame = nullptr;
if (aGestureReaders[i]->CalculateAndAcquireLatestFrame(&pGestureFrame) == S_OK)
{
// check if the gesture of this body is tracked
BOOLEAN bGestureTracked = false;
if (pGestureFrame->get_IsTrackingIdValid(&bGestureTracked) == S_OK && bGestureTracked)
{
GestureType mType;
const UINT uTextLength = 260;
wchar_t sName[uTextLength];
// for each gestures
for (UINT j = 0; j < iGestureCount; ++j)
{
// get gesture information
aGestureList[j]->get_GestureType(&mType);
aGestureList[j]->get_Name(uTextLength, sName);
if (mType == GestureType_Discrete)
{
// get gesture result
IDiscreteGestureResult* pGestureResult = nullptr;
if (pGestureFrame->get_DiscreteGestureResult(aGestureList[j], &pGestureResult) == S_OK)
{
// check if is detected
BOOLEAN bDetected = false;
if (pGestureResult->get_Detected(&bDetected) == S_OK && bDetected)
{
float fConfidence = 0.0f;
pGestureResult->get_Confidence(&fConfidence);
// output information
wcout << L"Detected Gesture " << sName << L" @" << fConfidence << endl;
}
pGestureResult->Release();
}
}
else if (mType == GestureType_Continuous)
{
// get gesture result
IContinuousGestureResult* pGestureResult = nullptr;
if (pGestureFrame->get_ContinuousGestureResult(aGestureList[j], &pGestureResult) == S_OK)
{
// get progress
float fProgress = 0.0f;
if (pGestureResult->get_Progress(&fProgress) == S_OK)
{
if (fProgress > 0.5f)
{
// output information
wcout << L"Detected Gesture " << sName << L" " << fProgress << endl;
}
}
pGestureResult->Release();
}
}
}
}
pGestureFrame->Release();
}
}
}
}
else
{
cerr << "Can't read body data" << endl;
}
// release frame
pBodyFrame->Release();
}
}
#pragma region Resource release
// release gesture data
for (UINT i = 0; i < iGestureCount; ++i)
aGestureList[i]->Release();
delete[] aGestureList;
// release body data
for (int i = 0; i < iBodyCount; ++i)
aBody[i]->Release();
delete[] aBody;
// release gesture source and reader
for (int i = 0; i < iBodyCount; ++i)
{
aGestureReaders[i]->Release();
aGestureSources[i]->Release();
}
delete [] aGestureReaders;
delete [] aGestureSources;
// release body frame source and reader
pBodyFrameReader->Release();
pBodyFrameSource->Release();
// Close Sensor
pSensor->Close();
// Release Sensor
pSensor->Release();
#pragma endregion
return 0;
}