-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGestureRecog.cpp
More file actions
361 lines (308 loc) · 13.9 KB
/
GestureRecog.cpp
File metadata and controls
361 lines (308 loc) · 13.9 KB
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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
#include <XnCppWrapper.h>
#include <XnOpenNI.h>
#include <XnLog.h>
#include <XnFPSCalculator.h>
#include <string.h>
#include "identifyLights.hpp"
//---------------------------------------------------------------------------
// Defines
//---------------------------------------------------------------------------
#define SAMPLE_XML_PATH "/home/prannoy/Downloads/OpenNI/Data/SamplesConfig.xml"
#define SAMPLE_XML_PATH_LOCAL "SamplesConfig.xml"
//---------------------------------------------------------------------------
// Globals
//---------------------------------------------------------------------------
xn::Context g_Context;
xn::ScriptNode g_scriptNode;
xn::UserGenerator g_UserGenerator;
XnBool g_bNeedPose = FALSE;
XnChar g_strPose[20] = "";
#define MAX_NUM_USERS 15
//---------------------------------------------------------------------------
// Code
//---------------------------------------------------------------------------
using namespace xn;
typedef struct{
float real_x,real_y,real_z;
}light;
XnBool fileExists(const char *fn)
{
XnBool exists;
xnOSDoesFileExist(fn, &exists);
return exists;
}
// Callback: New user was detected
void XN_CALLBACK_TYPE User_NewUser(xn::UserGenerator& /*generator*/, XnUserID nId, void* /*pCookie*/)
{
XnUInt32 epochTime = 0;
xnOSGetEpochTime(&epochTime);
printf("%d New User %d\n", epochTime, nId);
// New user found
if (g_bNeedPose)
{
g_UserGenerator.GetPoseDetectionCap().StartPoseDetection(g_strPose, nId);
}
else
{
g_UserGenerator.GetSkeletonCap().RequestCalibration(nId, TRUE);
}
}
// Callback: An existing user was lost
void XN_CALLBACK_TYPE User_LostUser(xn::UserGenerator& /*generator*/, XnUserID nId, void* /*pCookie*/)
{
XnUInt32 epochTime = 0;
xnOSGetEpochTime(&epochTime);
printf("%d Lost user %d\n", epochTime, nId);
}
// Callback: Detected a pose
void XN_CALLBACK_TYPE UserPose_PoseDetected(xn::PoseDetectionCapability& /*capability*/, const XnChar* strPose, XnUserID nId, void* /*pCookie*/)
{
XnUInt32 epochTime = 0;
xnOSGetEpochTime(&epochTime);
printf("%d Pose %s detected for user %d\n", epochTime, strPose, nId);
g_UserGenerator.GetPoseDetectionCap().StopPoseDetection(nId);
g_UserGenerator.GetSkeletonCap().RequestCalibration(nId, TRUE);
}
// Callback: Started calibration
void XN_CALLBACK_TYPE UserCalibration_CalibrationStart(xn::SkeletonCapability& /*capability*/, XnUserID nId, void* /*pCookie*/)
{
XnUInt32 epochTime = 0;
xnOSGetEpochTime(&epochTime);
printf("%d Calibration started for user %d\n", epochTime, nId);
}
void XN_CALLBACK_TYPE UserCalibration_CalibrationComplete(xn::SkeletonCapability& /*capability*/, XnUserID nId, XnCalibrationStatus eStatus, void* /*pCookie*/)
{
XnUInt32 epochTime = 0;
xnOSGetEpochTime(&epochTime);
if (eStatus == XN_CALIBRATION_STATUS_OK)
{
// Calibration succeeded
printf("%d Calibration complete, start tracking user %d\n", epochTime, nId);
g_UserGenerator.GetSkeletonCap().StartTracking(nId);
}
else
{
// Calibration failed
printf("%d Calibration failed for user %d\n", epochTime, nId);
if(eStatus==XN_CALIBRATION_STATUS_MANUAL_ABORT)
{
printf("Manual abort occured, stop attempting to calibrate!");
return;
}
if (g_bNeedPose)
{
g_UserGenerator.GetPoseDetectionCap().StartPoseDetection(g_strPose, nId);
}
else
{
g_UserGenerator.GetSkeletonCap().RequestCalibration(nId, TRUE);
}
}
}
#define CHECK_RC(nRetVal, what) \
if (nRetVal != XN_STATUS_OK) \
{ \
printf("%s failed: %s\n", what, xnGetStatusString(nRetVal)); \
return nRetVal; \
}
int main(int argc, char *argv[])
{
XnStatus nRetVal = XN_STATUS_OK;
xn::EnumerationErrors errors;
//DepthGenerator depth;
//DepthMetaData depthMD;
int number_lights,config=0,gesture=0;
Light *croom;
if( strcmp("config",argv[1]) == 0 ){
number_lights=getLightCoordinates(&croom);
config=1;
}
else{
gesture=1;
}
const char *fn = NULL;
if (fileExists(SAMPLE_XML_PATH)) fn = SAMPLE_XML_PATH;
else if (fileExists(SAMPLE_XML_PATH_LOCAL)) fn = SAMPLE_XML_PATH_LOCAL;
else {
// printf("Could not find '%s' nor '%s'. Aborting.\n" , SAMPLE_XML_PATH, SAMPLE_XML_PATH_LOCAL);
return XN_STATUS_ERROR;
}
printf("Reading config from: '%s'\n", fn);
nRetVal = g_Context.InitFromXmlFile(fn, g_scriptNode, &errors);
if (nRetVal == XN_STATUS_NO_NODE_PRESENT)
{
XnChar strError[1024];
errors.ToString(strError, 1024);
// printf("%s\n", strError);
return (nRetVal);
}
else if (nRetVal != XN_STATUS_OK)
{
//printf("Open failed: %s\n", xnGetStatusString(nRetVal));
return (nRetVal);
}
nRetVal = g_Context.FindExistingNode(XN_NODE_TYPE_USER, g_UserGenerator);
if (nRetVal != XN_STATUS_OK)
{
nRetVal = g_UserGenerator.Create(g_Context);
CHECK_RC(nRetVal, "Find user generator");
}
XnCallbackHandle hUserCallbacks, hCalibrationStart, hCalibrationComplete, hPoseDetected;
if (!g_UserGenerator.IsCapabilitySupported(XN_CAPABILITY_SKELETON))
{
//printf("Supplied user generator doesn't support skeleton\n");
return 1;
}
nRetVal = g_UserGenerator.RegisterUserCallbacks(User_NewUser, User_LostUser, NULL, hUserCallbacks);
CHECK_RC(nRetVal, "Register to user callbacks");
nRetVal = g_UserGenerator.GetSkeletonCap().RegisterToCalibrationStart(UserCalibration_CalibrationStart, NULL, hCalibrationStart);
CHECK_RC(nRetVal, "Register to calibration start");
nRetVal = g_UserGenerator.GetSkeletonCap().RegisterToCalibrationComplete(UserCalibration_CalibrationComplete, NULL, hCalibrationComplete);
CHECK_RC(nRetVal, "Register to calibration complete");
if(config==1){
DepthGenerator depth;
nRetVal = g_Context.FindExistingNode(XN_NODE_TYPE_DEPTH, depth);
CHECK_RC(nRetVal, "Find depth generator");
XnFPSData xnFPS;
nRetVal = xnFPSInit(&xnFPS, 180);
CHECK_RC(nRetVal, "FPS Init");
DepthMetaData depthMD;
xnFPSMarkFrame(&xnFPS);
depth.GetMetaData(depthMD);
printf("--2");//Print Number of Lights
for(int i=0;i<2;i++){
//printf("Frame %d Middle point is: %u FPS: %f\n", depthMD.FrameID(), depthMD(room[i].y,room[i].x), xnFPSCalc(&xnFPS));
XnPoint3D my_projective_point = {croom[i].y,croom[i].x,depthMD(croom[i].y,croom[i].x)};
XnPoint3D my_real_point; //the converted coordinates will be stored here
depth.ConvertProjectiveToRealWorld(
1, //convert one point
&my_projective_point,
&my_real_point
);
printf("!%6.2f %6.2f %6.2f",my_real_point.X,my_real_point.Y,my_real_point.Z);
}
depth.Release();
}
if(gesture==1){
light room[2];
for(int i=0;i<2;i++){
room[i].real_x=atof(argv[(3*i)+2]);
room[i].real_y=atof(argv[(3*i)+3]);
if(room[i].real_x<0)
room[i].real_z=atof(argv[(3*i)+4])-2200;
else
room[i].real_z=atof(argv[(3*i)+4]);
}
if (g_UserGenerator.GetSkeletonCap().NeedPoseForCalibration())
{
g_bNeedPose = TRUE;
if (!g_UserGenerator.IsCapabilitySupported(XN_CAPABILITY_POSE_DETECTION))
{
// printf("Pose required, but not supported\n");
return 1;
}
nRetVal = g_UserGenerator.GetPoseDetectionCap().RegisterToPoseDetected(UserPose_PoseDetected, NULL, hPoseDetected);
CHECK_RC(nRetVal, "Register to Pose Detected");
g_UserGenerator.GetSkeletonCap().GetCalibrationPose(g_strPose);
}
g_UserGenerator.GetSkeletonCap().SetSkeletonProfile(XN_SKEL_PROFILE_ALL);
nRetVal = g_Context.StartGeneratingAll();
CHECK_RC(nRetVal, "StartGenerating");
XnUserID aUsers[MAX_NUM_USERS];
XnUInt16 nUsers;
XnSkeletonJointTransformation lelbowJoint;
XnSkeletonJointTransformation headJoint;
XnSkeletonJointTransformation lwristJoint;
XnSkeletonJointTransformation relbowJoint;
XnSkeletonJointTransformation rwristJoint;
XnSkeletonJointTransformation head;
XnSkeletonJointTransformation elbow;
XnSkeletonJointTransformation wrist;
printf("Starting to run\n");
if(g_bNeedPose)
{
// printf("Assume calibration pose\n");
}
int gestureflag=1;
while (!xnOSWasKeyboardHit() || gestureflag)
{
g_Context.WaitOneUpdateAll(g_UserGenerator);
// print the torso information for the first user already tracking
nUsers=MAX_NUM_USERS;
g_UserGenerator.GetUsers(aUsers, nUsers);
for(XnUInt16 i=0; i<nUsers; i++)
{
if(g_UserGenerator.GetSkeletonCap().IsTracking(aUsers[i])==FALSE)
continue;
//Doing everything for left hand
g_UserGenerator.GetSkeletonCap().GetSkeletonJoint(aUsers[i],XN_SKEL_LEFT_ELBOW,lelbowJoint);
g_UserGenerator.GetSkeletonCap().GetSkeletonJoint(aUsers[i],XN_SKEL_RIGHT_ELBOW,relbowJoint);
// printf("user %d: left elbow at (%6.2f,%6.2f,%6.2f)\n",aUsers[i],
// lelbowJoint.position.position.X,
// lelbowJoint.position.position.Y,
// lelbowJoint.position.position.Z);
g_UserGenerator.GetSkeletonCap().GetSkeletonJoint(aUsers[i],XN_SKEL_HEAD,headJoint);
// printf("user %d: head at (%6.2f,%6.2f,%6.2f)\n",aUsers[i],
// headJoint.position.position.X,
// headJoint.position.position.Y,
// headJoint.position.position.Z);
g_UserGenerator.GetSkeletonCap().GetSkeletonJoint(aUsers[i],XN_SKEL_LEFT_HAND,lwristJoint);
g_UserGenerator.GetSkeletonCap().GetSkeletonJoint(aUsers[i],XN_SKEL_RIGHT_HAND,rwristJoint);
//printf("user %d: left wrist at (%6.2f,%6.2f,%6.2f)\n",aUsers[i],
// lwristJoint.position.position.X,
// lwristJoint.position.position.Y,
// lwristJoint.position.position.Z);
int pointing=0; //-1 if pointing backwards or 1 if pointing forwards,0 if not pointing
/* Direction Not necessary,eqn takes care of everything
// int direction=0;//1 if right and 0 if left
if((lwristJoint.position.position.Y-lelbowJoint.position.position.Y)>0){
pointing=((headJoint.position.position.Z-lwristJoint.position.position.Z)>0)?1:-1;
if(lwristJoint.position.position.X>0 && lelbowJoint.position.position.X>0)
direction=((lwristJoint.position.position.X-lelbowJoint.position.position.X)>0)?0:1;
else if(lwristJoint.position.position.X<0 && lelbowJoint.position.position.X<0)
direction=((lwristJoint.position.position.X-lelbowJoint.position.position.X)<0)?0:1;
else if(lwristJoint.position.position.X>lelbowJoint.position.position.X)
direction=1;
} */
head=headJoint;
if((lwristJoint.position.position.Y-lelbowJoint.position.position.Y)>0){
pointing=((headJoint.position.position.Z-lwristJoint.position.position.Z)>0)?1:-1;
wrist=lwristJoint;
elbow=lelbowJoint;
// printf("left hand %d \n",pointing);
}
if((rwristJoint.position.position.Y-relbowJoint.position.position.Y)>0){
pointing=((headJoint.position.position.Z-rwristJoint.position.position.Z)>0)?1:-1;
wrist=rwristJoint;
elbow=relbowJoint;
//printf("right hand %d \n",pointing);
}
float slope,intercept,depth;
float threshold=1000;//Error accepted for each light,usually dependent on number of lights
slope=((wrist.position.position.Z-elbow.position.position.Z)/(wrist.position.position.X-elbow.position.position.X));
intercept=(wrist.position.position.Z-(slope*wrist.position.position.X));
if(pointing==1 || pointing==-1){
for(int j=0;j<2;j++){
depth=((slope*room[j].real_x)+intercept);
//printf("i am a light at %6.2f \n",room[i].real_x);
// printf("i am pointing to a depth of %6.2f for light %6.2f \n",(depth-room[j].real_z),room[j].real_x);
if((depth-room[j].real_z)>(-1000) && (depth-room[j].real_z)<(1000)){
if(pointing==1 && (head.position.position.Z-room[j].real_z)>0){
//Switch on this particular light
printf("--%d\n",j);
return 0;
}
else if(pointing==-1 && (head.position.position.Z-room[j].real_z)<0){
//Switch on this light
printf("--%d\n",j);
return 0;
}
}}
}
}
}
}//if gesture==1
g_scriptNode.Release();
g_UserGenerator.Release();
g_Context.Release();
}