forked from bmwesting/Kinect-Hand-Processing
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSkeletonSensor.cpp
More file actions
304 lines (236 loc) · 7.81 KB
/
SkeletonSensor.cpp
File metadata and controls
304 lines (236 loc) · 7.81 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
#include "SkeletonSensor.h"
#include "log.h"
inline int CHECK_RC(const unsigned int rc, const char* const description)
{
if(rc != XN_STATUS_OK)
{
put_flog(LOG_ERROR, "%s failed: %s", description, xnGetStatusString(rc));
return -1;
}
return 0;
}
SkeletonSensor::SkeletonSensor()
{
pointModeProjective_ = false;
}
SkeletonSensor::~SkeletonSensor()
{
context_.Shutdown();
}
int SkeletonSensor::initialize()
{
context_.Init();
XnStatus rc = XN_STATUS_OK;
// create depth and user generators
rc = depthG_.Create(context_);
if(CHECK_RC(rc, "Create depth generator") == -1)
return -1;
rc = userG_.Create(context_);
if(CHECK_RC(rc, "Create user generator") == -1)
return -1;
rc = imageG_.Create(context_);
if(CHECK_RC(rc, "Create image generator") == -1)
return -1;
XnMapOutputMode mapMode;
depthG_.GetMapOutputMode(mapMode);
// for now, make output map VGA resolution at 30 FPS
mapMode.nXRes = XN_VGA_X_RES;
mapMode.nYRes = XN_VGA_Y_RES;
mapMode.nFPS = 30;
depthG_.SetMapOutputMode(mapMode);
imageG_.SetMapOutputMode(mapMode);
// turn on device mirroring
if(depthG_.IsCapabilitySupported("Mirror") == true)
{
rc = depthG_.GetMirrorCap().SetMirror(true);
CHECK_RC(rc, "Setting Image Mirroring on depthG");
}
// turn on device mirroring
if(imageG_.IsCapabilitySupported("Mirror") == true)
{
rc = imageG_.GetMirrorCap().SetMirror(true);
CHECK_RC(rc, "Setting Image Mirroring on imageG");
}
// make sure the user points are reported from the POV of the depth generator
userG_.GetAlternativeViewPointCap().SetViewPoint(depthG_);
depthG_.GetAlternativeViewPointCap().SetViewPoint(imageG_);
// set smoothing factor
userG_.GetSkeletonCap().SetSmoothing(0.9);
// start data streams
context_.StartGeneratingAll();
// setup callbacks
setCalibrationPoseCallbacks();
return 0;
}
void SkeletonSensor::waitForDeviceUpdateOnUser()
{
context_.WaitOneUpdateAll(userG_);
updateTrackedUsers();
}
void SkeletonSensor::updateTrackedUsers()
{
XnUserID users[64];
XnUInt16 nUsers = userG_.GetNumberOfUsers();
trackedUsers_.clear();
userG_.GetUsers(users, nUsers);
for(int i = 0; i < nUsers; i++)
{
if(userG_.GetSkeletonCap().IsTracking(users[i]))
{
trackedUsers_.push_back(users[i]);
}
}
}
bool SkeletonSensor::isTracking(const unsigned int uid)
{
return userG_.GetSkeletonCap().IsTracking(uid);
}
Skeleton SkeletonSensor::getSkeleton(const unsigned int uid)
{
Skeleton result;
// not tracking user
if(!userG_.GetSkeletonCap().IsTracking(uid))
return result;
// Array of available joints
const unsigned int nJoints = 15;
XnSkeletonJoint joints[nJoints] =
{ XN_SKEL_HEAD,
XN_SKEL_NECK,
XN_SKEL_RIGHT_SHOULDER,
XN_SKEL_LEFT_SHOULDER,
XN_SKEL_RIGHT_ELBOW,
XN_SKEL_LEFT_ELBOW,
XN_SKEL_RIGHT_HAND,
XN_SKEL_LEFT_HAND,
XN_SKEL_RIGHT_HIP,
XN_SKEL_LEFT_HIP,
XN_SKEL_RIGHT_KNEE,
XN_SKEL_LEFT_KNEE,
XN_SKEL_RIGHT_FOOT,
XN_SKEL_LEFT_FOOT,
XN_SKEL_TORSO
};
// holds the joint position components
XnSkeletonJointPosition positions[nJoints];
for (unsigned int i = 0; i < nJoints; i++)
{
userG_.GetSkeletonCap().GetSkeletonJointPosition(uid, joints[i], *(positions+i));
}
SkeletonPoint points[15];
convertXnJointsToPoints(positions, points, nJoints);
result.head = points[0];
result.neck = points[1];
result.rightShoulder = points[2];
result.leftShoulder = points[3];
result.rightElbow = points[4];
result.leftElbow = points[5];
result.rightHand = points[6];
result.leftHand = points[7];
result.rightHip = points[8];
result.leftHip = points[9];
result.rightKnee = points[10];
result.leftKnee = points[11];
result.rightFoot = points[12];
result.leftFoot = points[13];
result.torso = points[14];
return result;
}
std::vector<Skeleton> SkeletonSensor::getSkeletons()
{
std::vector<Skeleton> skeletons;
for(unsigned int i = 0; i < getNumTrackedUsers(); i++)
{
Skeleton s = getSkeleton(trackedUsers_[i]);
skeletons.push_back(s);
}
return skeletons;
}
unsigned int SkeletonSensor::getNumTrackedUsers()
{
return trackedUsers_.size();
}
unsigned int SkeletonSensor::getUID(const unsigned int index)
{
return trackedUsers_[index];
}
void SkeletonSensor::setPointModeToProjective()
{
pointModeProjective_ = true;
}
void SkeletonSensor::setPointModeToReal()
{
pointModeProjective_ = false;
}
const XnDepthPixel* SkeletonSensor::getDepthData()
{
return depthG_.GetDepthMap();
}
const XnUInt8* SkeletonSensor::getImageData()
{
return imageG_.GetImageMap();
}
const XnLabel* SkeletonSensor::getLabels()
{
xn::SceneMetaData sceneMD;
userG_.GetUserPixels(0, sceneMD);
return sceneMD.Data();
}
int SkeletonSensor::setCalibrationPoseCallbacks()
{
XnCallbackHandle hUserCallbacks, hCalibrationStart, hCalibrationComplete;
userG_.RegisterUserCallbacks(newUserCallback, lostUserCallback, this, hUserCallbacks);
userG_.GetSkeletonCap().RegisterToCalibrationStart(calibrationStartCallback, this, hCalibrationStart);
userG_.GetSkeletonCap().RegisterToCalibrationComplete(calibrationCompleteCallback, this, hCalibrationComplete);
// turn on tracking of all joints
userG_.GetSkeletonCap().SetSkeletonProfile(XN_SKEL_PROFILE_ALL);
return 0;
}
void SkeletonSensor::convertXnJointsToPoints(XnSkeletonJointPosition* const joints, SkeletonPoint* const points, unsigned int numPoints)
{
XnPoint3D xpt;
for(unsigned int i = 0; i < numPoints; i++)
{
xpt = joints[i].position;
if(pointModeProjective_)
depthG_.ConvertRealWorldToProjective(1, &xpt, &xpt);
points[i].confidence = joints[i].fConfidence;
points[i].x = xpt.X;
points[i].y = xpt.Y;
points[i].z = xpt.Z;
}
}
void XN_CALLBACK_TYPE SkeletonSensor::newUserCallback(xn::UserGenerator& generator, XnUserID nId, void* pCookie)
{
put_flog(LOG_DEBUG, "New user %d, auto-calibrating", nId);
SkeletonSensor* sensor = (SkeletonSensor*) pCookie;
sensor->userG_.GetSkeletonCap().RequestCalibration(nId, true);
}
void XN_CALLBACK_TYPE SkeletonSensor::lostUserCallback(xn::UserGenerator& generator, XnUserID nId, void* pCookie)
{
put_flog(LOG_DEBUG, "Lost user %d", nId);
}
void XN_CALLBACK_TYPE SkeletonSensor::calibrationStartCallback(xn::SkeletonCapability& capability, XnUserID nId, void* pCookie)
{
put_flog(LOG_DEBUG, "Calibration started for user %d", nId);
}
void XN_CALLBACK_TYPE SkeletonSensor::calibrationCompleteCallback(xn::SkeletonCapability& capability, XnUserID nId, XnCalibrationStatus eStatus, void* pCookie)
{
SkeletonSensor* sensor = (SkeletonSensor*) pCookie;
if(eStatus == XN_CALIBRATION_STATUS_OK)
{
put_flog(LOG_DEBUG, "Calibration completed: start tracking user %d", nId);
sensor->userG_.GetSkeletonCap().StartTracking(nId);
}
else
{
put_flog(LOG_DEBUG, "Calibration failed for user %d", nId);
sensor->userG_.GetSkeletonCap().RequestCalibration(nId, true);
}
}
void XN_CALLBACK_TYPE SkeletonSensor::poseDetectedCallback(xn::PoseDetectionCapability& capability, const XnChar* strPose, XnUserID nId, void* pCookie)
{
put_flog(LOG_DEBUG, "Pose detected for user %d", nId);
SkeletonSensor* sensor = (SkeletonSensor*) pCookie;
sensor->userG_.GetPoseDetectionCap().StopPoseDetection(nId);
sensor->userG_.GetSkeletonCap().RequestCalibration(nId, true);
}