-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKinectFace.h
executable file
·235 lines (197 loc) · 8.09 KB
/
KinectFace.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
/**
* @file KinectFace.h
* @ingroup Kinect
* @author Dominique Vaufreydaz, Grenoble Alpes University, Inria
* @copyright All right reserved.
*/
#ifndef __KINECT_FACE_H__
#define __KINECT_FACE_H__
#ifdef KINECT_2
#include "KinectBasics.h"
#include "KinectDataAsMemoryBuffer.h"
#ifdef ACTIVATE_KINECT_DRAWING
#include "opencv2/video/tracking.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/video/background_segm.hpp"
#include "opencv2/features2d/features2d.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#endif // ACTIVATE_KINECT_DRAWING
namespace MobileRGBD { namespace Kinect2 {
/**
* @class KinectFace KinectFace.cpp KinectFace.h
* @brief Class to manage face data from Kinect. All data are gathered in a unique buffer.
*
* @author Dominique Vaufreydaz, Grenoble Alpes University, Inria
*/
class KinectFace : public KinectDataAsMemoryBuffer
{
public:
/** @brief constructor.
*/
KinectFace()
{
DWORD * FaceFrameFeatures = (DWORD *)nullptr;
UINT64 * TrackingId = (UINT64 *)nullptr; // Gets the tracking ID.
Vector4 * RotationQuaternion = (Vector4 *)nullptr; // FaceQuaternion
RectI * BoundingBoxInColorSpace = (RectI *)nullptr; // BoundingBox
RectI * BoundingBoxInInfraredSpace = (RectI *)nullptr; // BoundingBox
PointF * FacePointsInColorSpace = (PointF *)nullptr;
PointF * FacePointsInInfraredSpace = (PointF *)nullptr;
DetectionResult * FaceProperties = (DetectionResult *)nullptr;
};
/** @brief constructor. Will set the data in memory.
*
* @param Buffer [in] Buffer to use for KinectFace data
*/
KinectFace(unsigned char *Buffer)
{
KinectFace();
Set(Buffer);
}
/** @brief Virtual destructor, always.
*/
virtual ~KinectFace() {};
/** @brief Set will put all reference to memory buffer for KinectBody data
*
* @param Buffer [in] Buffer to use for KinectFace data
*/
virtual void Set(unsigned char *Buffer)
{
unsigned char * InitBuffer = Buffer;
// Engaged = (DetectionResult*)Buffer; Buffer += sizeof(DetectionResult);
SetAddressAndIncreaseBuffer(TrackingId, Buffer);
SetAddressAndIncreaseBuffer(RotationQuaternion, Buffer);
SetAddressAndIncreaseBuffer(BoundingBoxInColorSpace, Buffer);
SetAddressAndIncreaseBuffer(BoundingBoxInInfraredSpace, Buffer);
SetAddressAndIncreaseBuffer(FacePointsInColorSpace, Buffer, FacePointType::FacePointType_Count);
SetAddressAndIncreaseBuffer(FacePointsInInfraredSpace, Buffer, FacePointType::FacePointType_Count);
SetAddressAndIncreaseBuffer(FaceProperties, Buffer, FaceProperty::FaceProperty_Count);
FaceSize = int(Buffer - InitBuffer);
}
#ifdef KINECT_LIVE
/** @brief Fill KinectBody buffer with data coming from the Kinect2. Exists only if KINECT_LIVE is defined.
*
* @param pFaceFrame [in] Pointer to the Kinect2 structure.
*/
bool Init(IFaceFrame* pFaceFrame)
{
if ( pFaceFrame == nullptr )
{
return false;
}
IFaceFrameResult* pFaceFrameResult = nullptr;
HRESULT hr = pFaceFrame->get_FaceFrameResult(&pFaceFrameResult);
// need to verify if pFaceFrameResult contains data before trying to access it
if (FAILED(hr) || pFaceFrameResult == nullptr)
{
return false;
}
hr = pFaceFrame->get_TrackingId(TrackingId);
hr = pFaceFrameResult->get_FaceRotationQuaternion(RotationQuaternion);
hr = pFaceFrameResult->get_FaceBoundingBoxInColorSpace(BoundingBoxInColorSpace);
hr = pFaceFrameResult->get_FaceBoundingBoxInInfraredSpace(BoundingBoxInInfraredSpace);
hr = pFaceFrameResult->GetFacePointsInColorSpace(FacePointType::FacePointType_Count, FacePointsInColorSpace);
hr = pFaceFrameResult->GetFacePointsInInfraredSpace(FacePointType::FacePointType_Count, FacePointsInInfraredSpace);
hr = pFaceFrameResult->GetFaceProperties(FaceProperty::FaceProperty_Count, FaceProperties);
pFaceFrameResult->Release();
return true;
}
#endif
#ifdef ACTIVATE_KINECT_DRAWING
/** @brief Draw one face in image.
*
* @param WhereToDraw [in] cv::Mat object to draw in.
* @param DrawInDepth [in] Do we draw in Depth frame or in RGB frame?
*/
void Draw(cv::Mat WhereToDraw, bool DrawInDepth = true )
{
// Compute actual scaling factor
if ( DrawInDepth == true )
{
float scale_x = (float)WhereToDraw.cols/(float)DepthWidth;
float scale_y = (float)WhereToDraw.rows/(float)DepthHeight;
cv::Rect FaceBB((int)(scale_x*(float)BoundingBoxInInfraredSpace->Left), (int)(scale_y*(float)BoundingBoxInInfraredSpace->Top),
(int)(scale_x*(float)(BoundingBoxInInfraredSpace->Right-BoundingBoxInInfraredSpace->Left)),
(int)(scale_y*(float)(BoundingBoxInInfraredSpace->Bottom-BoundingBoxInInfraredSpace->Top)) );
cv::rectangle( WhereToDraw, FaceBB, cv::Scalar(0,0,0) );
for( int i = 0; i < FacePointType_Count; i++ )
{
cv::circle( WhereToDraw, cv::Point((int)(scale_x*FacePointsInInfraredSpace[i].X), (int)(scale_y*FacePointsInInfraredSpace[i].Y)), 2, cv::Scalar(0,0,255), -1 );
}
}
else
{
float scale_x = (float)WhereToDraw.cols/(float)CamWidth;
float scale_y = (float)WhereToDraw.rows/(float)CamHeight;
cv::Rect FaceBB((int)(scale_x*(float)BoundingBoxInColorSpace->Left), (int)(scale_y*(float)BoundingBoxInColorSpace->Top),
(int)(scale_x*(float)(BoundingBoxInColorSpace->Right-BoundingBoxInColorSpace->Left)),
(int)(scale_y*(float)(BoundingBoxInColorSpace->Bottom-BoundingBoxInColorSpace->Top)) );
cv::rectangle( WhereToDraw, FaceBB, cv::Scalar(0,0,0) );
for( int i = 0; i < FacePointType_Count; i++ )
{
cv::circle( WhereToDraw, cv::Point((int)(scale_x*FacePointsInColorSpace[i].X), (int)(scale_y*FacePointsInColorSpace[i].Y)), 2, cv::Scalar(0,0,255), -1 );
}
}
}
#endif // ACTIVATE_KINECT_DRAWING
static int FaceSize; /*!< @brief Size of all KinectFace buffer. To compute it, one can call Set with nullptr */
UINT64 * TrackingId; /*!< @brief Gets the tracking ID. */
Vector4 * RotationQuaternion; /*!< @brief FaceQuaternion */
RectI * BoundingBoxInColorSpace; /*!< @brief BoundingBox in color image */
RectI * BoundingBoxInInfraredSpace; /*!< @brief BoundingBox in Depth image */
PointF * FacePointsInColorSpace; /*!< @brief Face point in Color image */
PointF * FacePointsInInfraredSpace; /*!< @brief Face point in Depth image */
DetectionResult * FaceProperties; /*!< @brief Face properties */
};
/**
* @class KinectFaces KinectBody.cpp KinectBody.h
* @brief Class to handle several KinectFace. All data are gathered in a unique buffer.
*
* @author Dominique Vaufreydaz, Grenoble Alpes University, Inria
*/
class KinectFaces
{
public:
/** @brief constructor. Will set the data in memory for up to BODY_COUNT KinectFace instances.
*
* @param Buffer [in] Buffer to use for KinectBody data
*/
KinectFaces(unsigned char * ExtBuffer = nullptr)
{
// Compute size of a body/face in bytes => result in KinectBody::BodySize KinectFace::FaceSize static values
FacesInformation[0].Set(nullptr);
// Allocate memory
if ( ExtBuffer == (unsigned char*)nullptr )
{
AllocatedBuffer.SetNewBufferSize(BODY_COUNT*KinectFace::FaceSize);
FaceData = (unsigned char*)AllocatedBuffer;
}
else
{
FaceData = ExtBuffer;
}
// Initialize memory for all faces
ActualNbFaces = 0;
unsigned char * lBuffer = FaceData;
for( int i = 0; i < BODY_COUNT; i++ )
{
FacesInformation[i].Set(lBuffer);
lBuffer += KinectFace::FaceSize;
FaceIsTracked[i] = false;
}
}
/** @brief Virtual destructor, always.
*/
virtual ~KinectFaces()
{
}
bool FaceIsTracked[BODY_COUNT]; /*!< @brief Store tracked state of faces. */
KinectFace FacesInformation[BODY_COUNT]; /*!< @brief Up to BODY_COUNT KinectFace can be handled. */
unsigned int ActualNbFaces; /*!< @brief Actual number of faces in the buffer. */
unsigned char * FaceData; /*!< @brief Store current faces information in one memory block for save/load operation. */
protected:
Omiscid::MemoryBuffer AllocatedBuffer; /*!< @brief Memory manager for BodyData. */
};
}} // namespace MobileRGBD::Kinect2
#endif // KINECT_2
#endif // __KINECT_FACE_H__