-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKinectSensor-v1.cpp
executable file
·351 lines (282 loc) · 9.56 KB
/
KinectSensor-v1.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
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
/**
* @file KinectSensor-v1.cpp
* @ingroup Kinect
* @author Dominique Vaufreydaz, Grenoble Alpes University, Inria
* @copyright All right reserved.
*/
#ifndef KINECT_2
#include "KinectSensor-v1.h"
#ifdef DO_FADO_FACE_TRACKING
#include <FaceTrackLib.h>
#endif
#include <tchar.h>
#include <math.h>
#include <iostream>
#define BUFSIZE MAX_PATH
using namespace std;
namespace MobileRGBD { namespace Kinect1 {
KinectSensor::KinectSensor()
{
// Create all events
for( auto i = 0; i < NumberOfEvents; i++ )
{
FrameEvents[i] = CreateEvent(NULL, TRUE, FALSE, NULL);
}
m_pDepthStreamHandle = NULL;
m_pVideoStreamHandle = NULL;
m_bNuiInitialized = false;
GatheredSources = FrameSourceTypes_None;
NearMode = false;
SeatedMode = false;
}
KinectSensor::~KinectSensor()
{
Release();
}
bool KinectSensor::SetKinectAngle( LONG RequestedKinectAngle )
{
if ( RequestedKinectAngle < NUI_CAMERA_ELEVATION_MINIMUM || RequestedKinectAngle > NUI_CAMERA_ELEVATION_MAXIMUM )
{
return false;
}
// Put Kinect tilt position (elevation) at RequestedKinectAngle° if needed
// Set CameraAngle to impossible value
LONG CameraAngle = (NUI_CAMERA_ELEVATION_MINIMUM-1);
// retrieve camera angle
NuiCameraElevationGetAngle( &CameraAngle );
// If camera angle is not suitable for us, move it to the right position
if ( CameraAngle != RequestedKinectAngle )
{
NuiCameraElevationSetAngle( RequestedKinectAngle );
Omiscid::Thread::Sleep( 1000 );
}
return true;
}
bool KinectSensor::Init( int DesiredSources )
{
HRESULT hr = NOERROR;
GatheredSources = DesiredSources;
if ( GatheredSources & (FrameSourceTypes_Infrared|FrameSourceTypes_LongExposureInfrared) )
{
fprintf( stderr, "Kinect v1.X does not support Infrared Streams\n");
return false;
}
Release(); // Deal with double initializations.
const NUI_IMAGE_TYPE colorType = NUI_IMAGE_TYPE_COLOR;
const NUI_IMAGE_TYPE depthType = NUI_IMAGE_TYPE_DEPTH_AND_PLAYER_INDEX;
// KINECT INIT
#ifdef PROCESS_AUDIO
hr = NuiInitialize( NUI_INITIALIZE_FLAG_USES_DEPTH_AND_PLAYER_INDEX | NUI_INITIALIZE_FLAG_USES_SKELETON | NUI_INITIALIZE_FLAG_USES_COLOR | NUI_INITIALIZE_FLAG_USES_AUDIO );
#else
hr = NuiInitialize( NUI_INITIALIZE_FLAG_USES_DEPTH_AND_PLAYER_INDEX | NUI_INITIALIZE_FLAG_USES_SKELETON | NUI_INITIALIZE_FLAG_USES_COLOR );
#endif
if (FAILED(hr))
{
return false;
}
m_bNuiInitialized = true;
// Depth event init
hr = NuiImageStreamOpen( depthType, NUI_IMAGE_RESOLUTION_640x480, (NearMode)? NUI_IMAGE_STREAM_FLAG_ENABLE_NEAR_MODE : 0, 2, FrameEvents[DepthFrameEvent], &m_pDepthStreamHandle );
if ( FAILED(hr) )
{
return false;
}
// Video event init
hr = NuiImageStreamOpen( colorType, NUI_IMAGE_RESOLUTION_640x480, 0, 2, FrameEvents[VideoFrameEvent], &m_pVideoStreamHandle );
if (FAILED(hr))
{
return false;
}
// Skeleton event init
DWORD dwSkeletonFlags = NUI_SKELETON_TRACKING_FLAG_ENABLE_IN_NEAR_RANGE;
if ( SeatedMode == true )
{
dwSkeletonFlags |= NUI_SKELETON_TRACKING_FLAG_ENABLE_SEATED_SUPPORT;
}
hr = NuiSkeletonTrackingEnable( FrameEvents[SkeletonFrameEvent], dwSkeletonFlags );
if (FAILED(hr))
{
return false;
}
// Start pump data
DeviceFullyStarted.Reset();
if ( StartThread() == false )
{
return false;
}
// Wait 20s for the thread to fully start its jobs
if ( DeviceFullyStarted.Wait(20000) == false )
{
fprintf( stderr, "Initialisation of the device failed\n" );
return false;
}
return true;
}
void KinectSensor::Release()
{
// Stop the Nui processing thread
StopThread();
StopRecording();
// Destroy handle all events
for( auto i = 0; i < NumberOfEvents; i++ )
{
if ( FrameEvents[i] != INVALID_HANDLE_VALUE )
{
CloseHandle( FrameEvents[i] );
FrameEvents[i] = NULL;
}
}
if ( m_bNuiInitialized == true )
{
NuiShutdown();
}
m_bNuiInitialized = false;
}
void FUNCTION_CALL_TYPE KinectSensor::Run()
{
// Define all RecContext to this
// KinectRecording* ColorRecContext = nullptr;
KinectRecording* ColorRecContext = nullptr;
KinectRecording* DepthRecContext = nullptr;
KinectRecording* BodyRecContext = nullptr;
KinectRecording* FaceRecContext = nullptr;
if ( GatheredSources & FrameSourceTypes_BodyIndex ) { fprintf( stderr, "BodyIndex (aka player detection) is enbedded in the Depth map"); }
// Activate Mandatory Recording Contexts
if ( GatheredSources & FrameSourceTypes_Color ) { ColorRecContext = reinterpret_cast<VideoKinectRecording*>(AddRecContext( "video", "RGBA", RecordingContextFactory::VideoRecContext)); }
if ( GatheredSources & FrameSourceTypes_Depth ) { DepthRecContext = AddRecContext( "depth", "UINT16", RecordingContextFactory::VideoRecContext); }
if ( GatheredSources & FrameSourceTypes_Body )
{
BodyRecContext = AddRecContext( "skeleton", "KinectBody", RecordingContextFactory::VideoRecContext);
if ( GatheredSources & FrameSourceTypes_Face )
{
// Create face context, no concurrency between thread, KFT.StartThread did not occur yet
FaceRecContext = reinterpret_cast<VideoKinectRecording*>(AddRecContext( "face", "KinectFace", RecordingContextFactory::VideoRecContext));
}
}
// Here everyting is initialized
DeviceFullyStarted.Signal();
//Timestamp
struct timeb lTimestamp;
// forever
while(StopPending() != true)
{
// Wait for an event to be signaled
WaitForMultipleObjects( NumberOfEvents, FrameEvents, FALSE, 20);
// Get time of this event (from Client side)
ftime(&lTimestamp);
// Process signal events
if (WAIT_OBJECT_0 == WaitForSingleObject(FrameEvents[DepthFrameEvent], 0))
{
VideoKinectRecording& rcs = *reinterpret_cast<VideoKinectRecording*>(DepthRecContext);
if ( GetRawFrame(lTimestamp, m_pDepthStreamHandle, rcs, 2 /* Bytes per pixels */ ) )
{
ProcessDepthFrame(rcs.BufferData, rcs.FrameLengthInPixels, rcs.Width, rcs.Height, KS_UINT16_12, rcs.InputNumber, lTimestamp, rcs.LastFrameTime);
}
}
// VIDEO
if ( WAIT_OBJECT_0 == WaitForSingleObject(FrameEvents[VideoFrameEvent], 0) )
{
VideoKinectRecording& rcs = *reinterpret_cast<VideoKinectRecording*>(ColorRecContext);
if ( GetRawFrame(lTimestamp, m_pVideoStreamHandle, rcs, 4 /* Bytes per pixels */ ) )
{
ProcessColorFrame(rcs.BufferData, rcs.FrameLengthInPixels, rcs.Width, rcs.Height, KS_RGBA, rcs.InputNumber, lTimestamp, rcs.LastFrameTime);
}
}
//SKELETON
if ( WAIT_OBJECT_0 == WaitForSingleObject(FrameEvents[SkeletonFrameEvent], 0) )
{
ProcessSkeletonFrame(lTimestamp, m_pDepthStreamHandle, *reinterpret_cast<VideoKinectRecording*>(ColorRecContext) );
#ifdef LIVE_RENDERING
for( int i = 0 ; i < NUI_SKELETON_COUNT ; i++ )
{
if( SkeletonFrame.SkeletonData[i].eTrackingState == NUI_SKELETON_TRACKED )
{
DrawSkel.DrawElement( ImgDepth, &SkeletonFrame.SkeletonData[i] );
}
}
#endif // LIVE_RENDERING
}
}
return;
}
bool KinectSensor::GetRawFrame( struct timeb& lTimestamp, HANDLE CurrentStream, VideoKinectRecording& RecContext, int NumberOfBytesPerPixels )
{
if ( RecContext.InitDescription == true)
{
DWORD width = 0;
DWORD height = 0;
NuiImageResolutionToSize(NUI_IMAGE_RESOLUTION_640x480, width, height);
RecContext.StartTime = (double)lTimestamp.time + (((double)lTimestamp.millitm)/1000.0);
RecContext.Width = width;
RecContext.Height = height;
RecContext.BytesPerPixel = NumberOfBytesPerPixels;
RecContext.BufferSize = width*height*RecContext.BytesPerPixel;
RecContext.FrameLengthInPixels = width*height*RecContext.BytesPerPixel;
RecContext.InitDescription = false;
}
const NUI_IMAGE_FRAME* pImageFrame = NULL;
if ( FAILED(NuiImageStreamGetNextFrame(CurrentStream, 0, &pImageFrame)) ) return false;
// Get the internal timestamp
RecContext.LastFrameTime = pImageFrame->liTimeStamp.QuadPart;
NUI_LOCKED_RECT LockedRect;
pImageFrame->pFrameTexture->LockRect(0, &LockedRect, NULL, 0);
if ( LockedRect.Pitch != 0 )
{
// Copy depth frame to face tracking
memcpy( RecContext.BufferData, PBYTE(LockedRect.pBits), RecContext.FrameLengthInPixels );
}
else
{
fprintf( stderr, "Buffer length of received texture is bogus\r\n" );
NuiImageStreamReleaseFrame(CurrentStream, pImageFrame);
return false;
}
// Release current frame
NuiImageStreamReleaseFrame(CurrentStream, pImageFrame);
// Save data if mandatory
if ( IsRecording == true )
{
// Save data
SaveDataAndIncreaseInputNumber( RecContext, lTimestamp, nullptr );
}
return true;
}
void KinectSensor::ProcessSkeletonFrame( struct timeb& lTimestamp, HANDLE CurrentStream, VideoKinectRecording& RecContext )
{
HRESULT hr = NuiSkeletonGetNextFrame(0, &SkeletonFrame);
if(FAILED(hr))
{
return;
}
// Count tracked skeletons
unsigned int NumberOfSkeletons = 0;
for( auto i = 0 ; i < NUI_SKELETON_COUNT ; i++ )
{
if( SkeletonFrame.SkeletonData[i].eTrackingState == NUI_SKELETON_TRACKED )
{
NumberOfSkeletons++;
}
}
if ( NumberOfSkeletons != 0 && IsRecording == true )
{
SaveSkeletonsInfo( SkeletonFrame.SkeletonData, NumberOfSkeletons, lTimestamp );
}
if ( NumberOfSkeletons != 0 )
{
ProcessBodyFrame(SkeletonFrame.SkeletonData, NumberOfSkeletons, 0, lTimestamp, 0);
}
else
{
ProcessBodyFrame(nullptr, 0, 0, lTimestamp, 0);
}
}
void KinectSensor::SaveSkeletonsInfo( NUI_SKELETON_DATA * _skeletonData, int _nbSkel, struct timeb& lTimestamp )
{
if ( IsRecording == false )
{
return;
}
// To do
}
}} // MobileRGBD::Kinect1
#endif // #ifndef KINECT_2