-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKinectSensor-v2.h
executable file
·350 lines (288 loc) · 11.1 KB
/
KinectSensor-v2.h
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
#pragma once
#if defined WIN32 || defined WIN54
#ifdef KINECT_2
#include <System/Thread.h>
#include <System/TypedMemoryBuffer.h>
#include <string>
#include <sstream>
#include <time.h>
#include <vector>
#ifdef KINECT_LIVE
#include <Kinect.h>
#include <Kinect.Face.h>
#endif
#include <memory>
// #define ACTIVATE_KINECT_DRAWING
#include "KinectBasics.h"
#include "KinectDraw.h"
#include "KinectBody.h"
#include "KinectFace.h"
#include "RecordingManagement.h"
#include "KinectSensorCommon.h"
#include "DepthCameraIntrinsics.h"
class DepthCameraIntrinsics;
namespace MobileRGBD { namespace Kinect2 {
template<typename DATA_TYPE>
class DataEvent : public Omiscid::Event
{
public:
DataEvent() {}
virtual ~DataEvent() {}
DATA_TYPE Data;
};
class KinectSensor : public Omiscid::Thread, public KinectDraw, public RecordingManagement
{
public:
KinectSensor();
virtual ~KinectSensor();
// Callback functions
virtual void ProcessColorFrame(void * Buffer, unsigned int BufferSize, int Width, int Height, ImgType FrameType, int NumFrame, const struct timeb& FrameTimestamp, TIMESPAN InternalFrameTime) {}
virtual void ProcessDepthFrame(void * Buffer, unsigned int BufferSize, int Width, int Height, ImgType FrameType, int NumFrame, const struct timeb& FrameTimestamp, TIMESPAN InternalFrameTime) {}
virtual void ProcessInfaredFrame(void * Buffer, unsigned int BufferSize, int Width, int Height, ImgType FrameType, int NumFrame, const struct timeb& FrameTimestamp, TIMESPAN InternalFrameTime) {}
virtual void ProcessLongExposureInfaredFrame(void * Buffer, unsigned int BufferSize, int Width, int Height, ImgType FrameType, int NumFrame, const struct timeb& FrameTimestamp, TIMESPAN InternalFrameTime) {}
virtual void ProcessBodyIndexFrame(void * Buffer, unsigned int BufferSize, int Width, int Height, ImgType FrameType, int NumFrame, const struct timeb& FrameTimestamp, TIMESPAN InternalFrameTime) {}
virtual void ProcessBodyFrame(MobileRGBD::Kinect2::KinectBodies& CurrentBodies, int NumFrame, const struct timeb& FrameTimestamp, TIMESPAN InternalFrameTime) {}
virtual void ProcessFaceFrame(MobileRGBD::Kinect2::KinectFaces& CurrentFaces, int NumFrame, const struct timeb& FrameTimestamp, TIMESPAN InternalFrameTime) {}
virtual void ProcessAudioFrame(float * CurrentAudio, int NumFrame, const struct timeb& FrameTimestamp, TIMESPAN InternalFrameTime) {}
// Init start with source selection
bool Init(int DesiredSources);
protected:
friend class KinectAudioStream;
friend class KinectRGBStream;
friend class KinectFaceStream;
int GatheredSources;
IKinectSensor * pSensor;
virtual void FUNCTION_CALL_TYPE Run();
// Estimate floor Angle using Kinect SDK
enum { NoEstimation, OnlineFloorEstimation, EstimateFloorUntilStable };
int FloorEstimationProcess;
Vector4 FloorClipPlane;
float AngleWithFloor;
int NumberOfIdenticalFloorEStimation;
Omiscid::Event FloorPlaneEstimated;
DataEvent<bool> InitStepDone;
// Utility functions
bool IsDepthFrameToCameraSpaceLookupTableFilled;
Omiscid::TypedMemoryBuffer<PointF> DepthFrameToCameraSpaceLookupTable;
bool GetDepthFrameToCameraSpaceTable()
{
if ( pSensor == nullptr )
{
return false;
}
ICoordinateMapper * pMapper;
if ( FAILED(pSensor->get_CoordinateMapper(&pMapper)) )
{
fprintf( stderr, "Unable to retrieve CoordinateMapper\n" );
return false;
}
UINT32 NumberOfElementInTable = 0;
PointF *tableEntries = nullptr;
if ( FAILED(pMapper->GetDepthFrameToCameraSpaceTable(&NumberOfElementInTable, &tableEntries)) )
{
return false;
}
// Set space to number of depth pixels
DepthFrameToCameraSpaceLookupTable.SetNewNumberOfElementsInBuffer( MobileRGBD::Kinect2::DepthWidth * MobileRGBD::Kinect2::DepthHeight );
// Copy data
memcpy( (PointF*)DepthFrameToCameraSpaceLookupTable, tableEntries, NumberOfElementInTable*sizeof(PointF) );
// Free memory provided by Kinect
CoTaskMemFree(tableEntries);
return true;
}
DepthCameraIntrinsics DepthCameraInts;
bool GetDepthCameraIntrinsics()
{
if ( pSensor == nullptr )
{
return false;
}
ICoordinateMapper * pMapper;
if ( FAILED(pSensor->get_CoordinateMapper(&pMapper)) )
{
fprintf( stderr, "Unable to retrieve CoordinateMapper\n" );
return false;
}
pMapper->GetDepthCameraIntrinsics(&DepthCameraInts.IntrinsicsParameters);
return true;
}
// Templating for code generation !
template<class Interface>
inline void GetFrameDescription(Interface * pInterfaceToFrame, RawKinectRecording& RecordContext)
{
IFrameDescription * pFrameDesc = nullptr;
if (FAILED(pInterfaceToFrame->get_FrameDescription(&pFrameDesc)))
{
fprintf(stderr, "Could not get frame description\n");
return;
}
pFrameDesc->get_Height(&RecordContext.Height);
pFrameDesc->get_Width(&RecordContext.Width);
pFrameDesc->get_HorizontalFieldOfView(&RecordContext.HorizontalFieldOfView);
pFrameDesc->get_VerticalFieldOfView(&RecordContext.VerticalFieldOfView);
pFrameDesc->get_DiagonalFieldOfView(&RecordContext.DiagonalFieldOfView);
// For serialization reasons
unsigned int pVal;
pFrameDesc->get_LengthInPixels(&pVal);
RecordContext.FrameLengthInPixels = pVal;
pFrameDesc->get_BytesPerPixel(&pVal);
RecordContext.BytesPerPixel = pVal;
RecordContext.BufferSize = RecordContext.FrameLengthInPixels * RecordContext.BytesPerPixel;
}
template<class Interface, typename BufferType>
inline void GetFrame(Interface * pInterfaceToFrame,
// HRESULT (STDMETHODCALLTYPE Interface::*RetrieveFrameBuffer)( _Out_ UINT, _Out_writes_all_(capacity)BufferType *),
HRESULT(STDMETHODCALLTYPE Interface::*RetrieveFrameBuffer)(UINT*, BufferType **),
RawKinectRecording& RecordContext, const char * DataTypeName)
{
BufferType * FrameData = nullptr;
if (RecordContext.InitDescription == true)
{
GetFrameDescription(pInterfaceToFrame, RecordContext);
RecordContext.InitDescription = false;
}
// return buffer size if wrong in august version of sdk, used computed one
UINT BufferSize = 0;
if (FAILED(((pInterfaceToFrame)->*(RetrieveFrameBuffer))(&BufferSize, &FrameData)) || FrameData == nullptr)
{
fprintf(stderr, "Could not get %s buffer\n", DataTypeName);
return;
}
else
{
// fprintf(stderr, "get %s \n", DataTypeName );
// char * BufferData = new char[1024*1024]
memcpy(RecordContext.BufferData, FrameData, RecordContext.BufferSize);
}
// Get Frame timestamp if possible
RecordContext.LastFrameTime = 0;
pInterfaceToFrame->get_RelativeTime(&RecordContext.LastFrameTime);
}
inline void GetBodyFrame(IBodyFrame * pInterfaceToFrame, KinectRecording& RecordContext, MobileRGBD::Kinect2::KinectBodies& Bodies, const char * DataTypeName)
{
// if we are tracking floor clip plane and floor angle
if (FloorEstimationProcess != NoEstimation)
{
pInterfaceToFrame->get_FloorClipPlane(&FloorClipPlane);
float tmpf = ceilf(atan2f(FloorClipPlane.z, FloorClipPlane.y) * 180.0f / 3.14f);
if (tmpf == AngleWithFloor)
{
NumberOfIdenticalFloorEStimation++;
}
else
{
// Get the new value
AngleWithFloor = tmpf;
NumberOfIdenticalFloorEStimation = 0;
}
if (FloorEstimationProcess == EstimateFloorUntilStable && NumberOfIdenticalFloorEStimation > 5 * 30)
{
// Stop processing
FloorEstimationProcess = NoEstimation;
NumberOfIdenticalFloorEStimation = 0;
// Signal that we found something
FloorPlaneEstimated.Signal();
}
}
// TODO : free previous IBody
for( int iBody = 0; iBody < BODY_COUNT; iBody++ )
{
MobileRGBD::Kinect2::SafeRelease(Bodies.ppBodies[iBody]);
}
// memset(Bodies.ppBodies, 0, sizeof(IBody*)*BODY_COUNT);
if (FAILED(pInterfaceToFrame->GetAndRefreshBodyData(_countof(Bodies.ppBodies), Bodies.ppBodies)))
{
// Thus, We do not have bodies
Bodies.ActualNbBody = 0;
fprintf(stderr, "Could not get %s buffer\n", DataTypeName);
return;
}
// Get Frame timestamp if possible
pInterfaceToFrame->get_RelativeTime(&RecordContext.LastFrameTime);
RecordContext.BufferSize = Bodies.GatherBodiesInformationAndReturnSizeOfBodyBuffer();
}
inline void GetBodyFrameReferenceAndFrameBuffer(IMultiSourceFrame * pFrame, KinectRecording& RecordContext,
MobileRGBD::Kinect2::KinectBodies& Bodies, const char * DataTypeName)
{
IBodyFrame * pLocalFrame = nullptr;
IBodyFrameReference * pLocalRef = nullptr;
RecordContext.LastFrameTime = 0;
if (SUCCEEDED(pFrame->get_BodyFrameReference(&pLocalRef)))
{
pLocalRef->AcquireFrame(&pLocalFrame);
MobileRGBD::Kinect2::SafeRelease(pLocalRef);
// retrieve Data
if (pLocalFrame != nullptr)
{
GetBodyFrame(pLocalFrame, RecordContext, Bodies, DataTypeName);
MobileRGBD::Kinect2::SafeRelease(pLocalFrame);
}
}
}
inline void GetAudioFrameReferenceAndFrameBuffer(IMultiSourceFrame * pFrame, RawKinectRecording& RecordContext,
MobileRGBD::Kinect2::KinectBodies& Bodies, const char * DataTypeName)
{
IBodyFrame * pLocalFrame = nullptr;
IBodyFrameReference * pLocalRef = nullptr;
RecordContext.LastFrameTime = 0;
if (SUCCEEDED(pFrame->get_BodyFrameReference(&pLocalRef)))
{
pLocalRef->AcquireFrame(&pLocalFrame);
MobileRGBD::Kinect2::SafeRelease(pLocalRef);
// retrieve Data
if (pLocalFrame != nullptr)
{
GetBodyFrame(pLocalFrame, RecordContext, Bodies, DataTypeName);
MobileRGBD::Kinect2::SafeRelease(pLocalFrame);
}
}
}
template<class FrameReferenceClass, class Interface, typename BufferType>
inline void GetFrameReferenceAndFrameBuffer(IMultiSourceFrame * pFrame,
HRESULT(STDMETHODCALLTYPE IMultiSourceFrame::*GetFrameReference)(_COM_Outptr_ FrameReferenceClass **),
// HRESULT (STDMETHODCALLTYPE Interface::*RetrieveFrameBuffer)( _Out_ UINT, _Out_writes_all_(capacity)BufferType *),
HRESULT(STDMETHODCALLTYPE Interface::*RetrieveFrameBuffer)(UINT*, BufferType **),
RawKinectRecording& RecordContext, const char * DataTypeName)
{
Interface * pLocalFrame = nullptr;
FrameReferenceClass * pLocalRef = nullptr;
RecordContext.LastFrameTime = 0;
if (SUCCEEDED(((pFrame)->*(GetFrameReference))(&pLocalRef)))
{
pLocalRef->AcquireFrame(&pLocalFrame);
MobileRGBD::Kinect2::SafeRelease(pLocalRef);
// retrieve Data
if (pLocalFrame != nullptr)
{
GetFrame(pLocalFrame, RetrieveFrameBuffer, RecordContext, DataTypeName);
MobileRGBD::Kinect2::SafeRelease(pLocalFrame);
}
else
{
// fprintf(stderr, "Could not get reference class for %s\n", DataTypeName );
return;
}
}
}
};
// Class to multithread recording (maximum multisources and performance)
class KinectExtraRecorder
{
public:
KinectExtraRecorder(KinectSensor& KinectSensorCaller) : Caller(KinectSensorCaller)
{
}
virtual ~KinectExtraRecorder() {}
void SetRecContext(RawKinectRecording* Ptr)
{
Omiscid::SmartLocker SL_Protection(Protection);
RecContext = Ptr;
}
Omiscid::Mutex Protection;
protected:
RawKinectRecording* RecContext = nullptr;
KinectSensor& Caller;
};
}} // namespace MOBILERGBD::Kinect2
#endif // ifdef KINECT_2
#endif // WIN32 ou WIN64