質問編集履歴
2
文の修正
title
CHANGED
@@ -1,1 +1,1 @@
|
|
1
|
-
KinectV2プログラミング(C++)の改良
|
1
|
+
KinectV2プログラミング HDFace(C++)の改良
|
body
CHANGED
@@ -1,12 +1,296 @@
|
|
1
1
|
###前提・実現したいこと
|
2
|
-
KinectV2 HDfaceで1347点の顔特徴点を表示するプログラムを以下の
|
2
|
+
KinectV2 HDfaceで1347点の顔特徴点を表示するプログラムを以下のように組んだのですが、
|
3
3
|
特徴点が多すぎるためKinect.Face.hで定義されたHighDetailFacePointsの数十点の特徴点のみを
|
4
4
|
表示するプログラムを組みたいです。どのように改良させればよいでしょうか?
|
5
5
|
|
6
|
-
URL: https://github.com/K4W2-Book/K4W2-Book/blob/master/C%2B%2B(Native)/08_Face/KinectV2-Face-02/KinectV2/main.cpp
|
7
6
|
|
8
7
|
``````ここに言語を入力
|
8
|
+
#include <iostream>
|
9
|
+
#include <sstream>
|
10
|
+
#include <vector>
|
11
|
+
#include <array>
|
12
|
+
#define _USE_MATH_DEFINES
|
13
|
+
#include <cmath>
|
14
|
+
#include <atlbase.h>
|
15
|
+
|
16
|
+
#include <Windows.h>
|
17
|
+
#include <Kinect.h>
|
18
|
+
#include <Kinect.Face.h>
|
19
|
+
|
20
|
+
#include <opencv2/opencv.hpp>
|
21
|
+
|
22
|
+
#define ERROR_CHECK( ret ) \
|
23
|
+
if( FAILED( ret ) ){ \
|
24
|
+
std::stringstream ss; \
|
25
|
+
ss << "failed " #ret " " << std::hex << ret << std::endl; \
|
26
|
+
throw std::runtime_error( ss.str().c_str() ); \
|
27
|
+
}
|
28
|
+
|
29
|
+
class KinectApp
|
30
|
+
{
|
31
|
+
private:
|
32
|
+
CComPtr<IKinectSensor> kinect;
|
33
|
+
CComPtr<IColorFrameReader> colorFrameReader;
|
34
|
+
CComPtr<IBodyFrameReader> bodyFrameReader;
|
35
|
+
CComPtr<ICoordinateMapper> coordinateMapper;
|
36
|
+
std::vector<BYTE> colorBuffer;
|
37
|
+
int colorWidth;
|
38
|
+
int colorHeight;
|
39
|
+
unsigned int colorBytesPerPixel;
|
40
|
+
ColorImageFormat colorFormat = ColorImageFormat::ColorImageFormat_Bgra;
|
41
|
+
cv::Mat colorImage;
|
42
|
+
|
43
|
+
CComPtr<IHighDefinitionFaceFrameReader> hdFaceFrameReader;
|
44
|
+
CComPtr<IFaceModelBuilder> faceModelBuilder;
|
45
|
+
CComPtr<IFaceAlignment> faceAlignment;
|
46
|
+
CComPtr<IFaceModel> faceModel;
|
47
|
+
std::array<float, FaceShapeDeformations::FaceShapeDeformations_Count> shapeUnits;
|
48
|
+
UINT32 vertexCount;
|
49
|
+
UINT64 trackingId;
|
50
|
+
int trackingCount;
|
51
|
+
bool produced;
|
52
|
+
|
53
|
+
std::array<cv::Scalar, BODY_COUNT> colors;
|
54
|
+
int font = cv::FONT_HERSHEY_SIMPLEX;
|
55
|
+
|
56
|
+
public:
|
57
|
+
void initialize()
|
58
|
+
{
|
59
|
+
ERROR_CHECK( GetDefaultKinectSensor( &kinect ) );
|
60
|
+
ERROR_CHECK( kinect->Open() );
|
61
|
+
BOOLEAN isOpen;
|
62
|
+
ERROR_CHECK( kinect->get_IsOpen( &isOpen ) );
|
63
|
+
if( !isOpen ){
|
64
|
+
throw std::runtime_error( "failed IKinectSensor::get_IsOpen( &isOpen )" );
|
65
|
+
}
|
66
|
+
|
67
|
+
ERROR_CHECK( kinect->get_CoordinateMapper( &coordinateMapper ) );
|
68
|
+
CComPtr<IColorFrameSource> colorFrameSource;
|
69
|
+
ERROR_CHECK( kinect->get_ColorFrameSource( &colorFrameSource ) );
|
70
|
+
ERROR_CHECK( colorFrameSource->OpenReader( &colorFrameReader ) );
|
71
|
+
CComPtr<IFrameDescription> colorFrameDescription;
|
72
|
+
ERROR_CHECK( colorFrameSource->CreateFrameDescription( colorFormat, &colorFrameDescription ) );
|
73
|
+
ERROR_CHECK( colorFrameDescription->get_Width( &colorWidth ) );
|
74
|
+
ERROR_CHECK( colorFrameDescription->get_Height( &colorHeight ) );
|
75
|
+
ERROR_CHECK( colorFrameDescription->get_BytesPerPixel( &colorBytesPerPixel ) );
|
76
|
+
colorBuffer.resize( colorWidth * colorHeight * colorBytesPerPixel );
|
77
|
+
CComPtr<IBodyFrameSource> bodyFrameSource;
|
78
|
+
ERROR_CHECK( kinect->get_BodyFrameSource( &bodyFrameSource ) );
|
79
|
+
ERROR_CHECK( bodyFrameSource->OpenReader( &bodyFrameReader ) );
|
80
|
+
|
81
|
+
initializeHDFace();
|
82
|
+
|
83
|
+
// Lookup Table
|
84
|
+
colors[0] = cv::Scalar( 255, 0, 0 );
|
85
|
+
colors[1] = cv::Scalar( 0, 255, 0 );
|
86
|
+
colors[2] = cv::Scalar( 0, 0, 255 );
|
87
|
+
colors[3] = cv::Scalar( 255, 255, 0 );
|
88
|
+
colors[4] = cv::Scalar( 255, 0, 255 );
|
89
|
+
colors[5] = cv::Scalar( 0, 255, 255 );
|
90
|
+
}
|
91
|
+
|
92
|
+
void run()
|
93
|
+
{
|
94
|
+
while( 1 ){
|
95
|
+
update();
|
96
|
+
draw();
|
97
|
+
|
98
|
+
int key = cv::waitKey( 10 );
|
99
|
+
if( key == VK_ESCAPE ){
|
100
|
+
cv::destroyAllWindows();
|
101
|
+
break;
|
102
|
+
}
|
103
|
+
}
|
104
|
+
}
|
105
|
+
|
106
|
+
private:
|
107
|
+
void initializeHDFace()
|
108
|
+
{
|
109
|
+
CComPtr<IHighDefinitionFaceFrameSource> hdFaceFrameSource;
|
110
|
+
ERROR_CHECK( CreateHighDefinitionFaceFrameSource( kinect, &hdFaceFrameSource ) );
|
111
|
+
|
112
|
+
ERROR_CHECK( hdFaceFrameSource->OpenReader( &hdFaceFrameReader ) );
|
113
|
+
|
114
|
+
ERROR_CHECK( CreateFaceAlignment( &faceAlignment ) );
|
115
|
+
|
116
|
+
ERROR_CHECK( CreateFaceModel( 1.0f, FaceShapeDeformations::FaceShapeDeformations_Count, &shapeUnits[0], &faceModel ) );
|
117
|
+
ERROR_CHECK( GetFaceModelVertexCount( &vertexCount ) );
|
118
|
+
|
119
|
+
FaceModelBuilderAttributes attribures = FaceModelBuilderAttributes::FaceModelBuilderAttributes_None;
|
120
|
+
ERROR_CHECK( hdFaceFrameSource->OpenModelBuilder( attribures, &faceModelBuilder ) );
|
121
|
+
ERROR_CHECK( faceModelBuilder->BeginFaceDataCollection() );
|
122
|
+
}
|
123
|
+
|
124
|
+
void update()
|
125
|
+
{
|
126
|
+
updateColorFrame();
|
127
|
+
updateBodyFrame();
|
128
|
+
updateHDFaceFrame();
|
129
|
+
}
|
130
|
+
|
131
|
+
void updateColorFrame()
|
132
|
+
{
|
133
|
+
CComPtr<IColorFrame> colorFrame;
|
134
|
+
HRESULT ret = colorFrameReader->AcquireLatestFrame( &colorFrame );
|
135
|
+
if( FAILED( ret ) ){
|
136
|
+
return;
|
137
|
+
}
|
138
|
+
|
139
|
+
ERROR_CHECK( colorFrame->CopyConvertedFrameDataToArray( static_cast<UINT>( colorBuffer.size() ), &colorBuffer[0], colorFormat ) );
|
140
|
+
|
141
|
+
colorImage = cv::Mat( colorHeight, colorWidth, CV_8UC4, &colorBuffer[0] );
|
142
|
+
}
|
143
|
+
|
144
|
+
void updateBodyFrame()
|
145
|
+
{
|
146
|
+
CComPtr<IBodyFrame> bodyFrame;
|
147
|
+
HRESULT ret = bodyFrameReader->AcquireLatestFrame( &bodyFrame );
|
148
|
+
if( FAILED( ret ) ){
|
149
|
+
return;
|
150
|
+
}
|
151
|
+
|
152
|
+
std::array<CComPtr<IBody>, BODY_COUNT> bodies;
|
153
|
+
ERROR_CHECK( bodyFrame->GetAndRefreshBodyData( BODY_COUNT, &bodies[0] ) );
|
154
|
+
|
155
|
+
findClosestBody( bodies );
|
156
|
+
}
|
157
|
+
|
158
|
+
inline void findClosestBody( const std::array<CComPtr<IBody>, BODY_COUNT>& bodies )
|
159
|
+
{
|
160
|
+
float closest = FLT_MAX;
|
161
|
+
for( int count = 0; count < BODY_COUNT; count++ ){
|
162
|
+
CComPtr<IBody> body = bodies[count];
|
163
|
+
BOOLEAN tracked;
|
164
|
+
ERROR_CHECK( body->get_IsTracked( &tracked ) );
|
165
|
+
if( !tracked ){
|
166
|
+
continue;
|
167
|
+
}
|
168
|
+
|
169
|
+
std::array<Joint, JointType::JointType_Count> joints;
|
170
|
+
ERROR_CHECK( body->GetJoints( JointType::JointType_Count, &joints[0] ) );
|
171
|
+
Joint joint = joints[JointType::JointType_Head];
|
172
|
+
if( joint.TrackingState == TrackingState::TrackingState_NotTracked ){
|
173
|
+
continue;
|
174
|
+
}
|
175
|
+
|
176
|
+
CameraSpacePoint point = joint.Position;
|
177
|
+
float distance = std::sqrt( std::pow( point.X, 2 ) + std::pow( point.Y, 2 ) + std::pow( point.Z, 2 ) );
|
178
|
+
if( closest <= distance ){
|
179
|
+
continue;
|
180
|
+
}
|
181
|
+
closest = distance;
|
182
|
+
|
183
|
+
UINT64 id;
|
184
|
+
ERROR_CHECK( body->get_TrackingId( &id ) );
|
185
|
+
if( trackingId != id ){
|
186
|
+
trackingId = id;
|
187
|
+
trackingCount = count;
|
188
|
+
produced = false;
|
189
|
+
|
190
|
+
|
191
|
+
CComPtr<IHighDefinitionFaceFrameSource> hdFaceFrameSource;
|
192
|
+
ERROR_CHECK( hdFaceFrameReader->get_HighDefinitionFaceFrameSource( &hdFaceFrameSource ) );
|
193
|
+
ERROR_CHECK( hdFaceFrameSource->put_TrackingId( trackingId ) );
|
194
|
+
}
|
195
|
+
}
|
196
|
+
}
|
197
|
+
|
198
|
+
void updateHDFaceFrame()
|
199
|
+
{
|
200
|
+
CComPtr<IHighDefinitionFaceFrame> hdFaceFrame;
|
201
|
+
HRESULT ret = hdFaceFrameReader->AcquireLatestFrame( &hdFaceFrame );
|
202
|
+
if( FAILED( ret ) ){
|
203
|
+
return;
|
204
|
+
}
|
205
|
+
|
206
|
+
BOOLEAN tracked;
|
207
|
+
ERROR_CHECK( hdFaceFrame->get_IsFaceTracked( &tracked ) );
|
208
|
+
if( !tracked ){
|
209
|
+
return;
|
210
|
+
}
|
211
|
+
|
212
|
+
ERROR_CHECK( hdFaceFrame->GetAndRefreshFaceAlignmentResult( faceAlignment ) );
|
213
|
+
if( faceAlignment != nullptr ){
|
214
|
+
// Face Modelのフィッティング
|
215
|
+
buildFaceModel();
|
216
|
+
|
217
|
+
// 結果を取得、描画
|
218
|
+
result();
|
219
|
+
}
|
220
|
+
}
|
221
|
+
|
222
|
+
inline void buildFaceModel()
|
223
|
+
{
|
224
|
+
if( produced ){
|
225
|
+
cv::putText( colorImage, "Status : Complete", cv::Point( 50, 50 ), font, 1.0f, colors[trackingCount], 2, CV_AA );
|
226
|
+
return;
|
227
|
+
}
|
228
|
+
|
229
|
+
FaceModelBuilderCollectionStatus collection;
|
230
|
+
ERROR_CHECK( faceModelBuilder->get_CollectionStatus( &collection ) );
|
231
|
+
if( collection ){
|
232
|
+
// コレクション状態
|
233
|
+
cv::putText( colorImage, "Status : " + std::to_string( collection ), cv::Point( 50, 50 ), font, 1.0f, colors[trackingCount], 2, CV_AA );
|
234
|
+
std::string status = status2string( collection );
|
235
|
+
cv::putText( colorImage, status, cv::Point( 50, 80 ), font, 1.0f, colors[trackingCount], 2, CV_AA );
|
236
|
+
|
237
|
+
// キャプチャー状態
|
238
|
+
FaceModelBuilderCaptureStatus capture;
|
239
|
+
ERROR_CHECK( faceModelBuilder->get_CaptureStatus( &capture ) );
|
240
|
+
status = status2string( capture );
|
241
|
+
cv::putText( colorImage, status, cv::Point( 50, 110 ), font, 1.0f, colors[trackingCount], 2, CV_AA );
|
242
|
+
|
243
|
+
return;
|
244
|
+
}
|
245
|
+
|
246
|
+
CComPtr<IFaceModelData> faceModelData;
|
247
|
+
ERROR_CHECK( faceModelBuilder->GetFaceData( &faceModelData ) );
|
248
|
+
if( faceModelData != nullptr ){
|
249
|
+
ERROR_CHECK( faceModelData->ProduceFaceModel( &faceModel ) );
|
250
|
+
produced = true;
|
251
|
+
}
|
252
|
+
}
|
253
|
+
|
254
|
+
inline std::string status2string( const FaceModelBuilderCollectionStatus collection )
|
255
|
+
{
|
256
|
+
//文字数の都合上省略します。
|
257
|
+
return status;
|
258
|
+
}
|
259
|
+
|
260
|
+
inline std::string status2string( const FaceModelBuilderCaptureStatus capture )
|
261
|
+
{
|
262
|
+
//文字数の都合上省略します。
|
263
|
+
}
|
264
|
+
|
265
|
+
inline void result()
|
266
|
+
{
|
267
|
+
//文字数の都合上省略します。
|
268
|
+
}
|
269
|
+
|
270
|
+
void draw()
|
271
|
+
{
|
272
|
+
drawHDFaceFrame();
|
273
|
+
}
|
274
|
+
|
275
|
+
void drawHDFaceFrame()
|
276
|
+
{
|
277
|
+
if( !colorImage.empty() ){
|
278
|
+
cv::imshow( "HDFace", colorImage );
|
279
|
+
}
|
280
|
+
}
|
281
|
+
};
|
282
|
+
|
283
|
+
void main()
|
284
|
+
{
|
9
|
-
|
285
|
+
try{
|
286
|
+
KinectApp app;
|
287
|
+
app.initialize();
|
288
|
+
app.run();
|
289
|
+
}
|
290
|
+
catch( std::exception& ex ){
|
291
|
+
std::cout << ex.what() << std::endl;
|
292
|
+
}
|
293
|
+
}
|
10
294
|
```
|
11
295
|
コード
|
12
296
|
```
|
1
title
CHANGED
File without changes
|
body
CHANGED
File without changes
|