forked from vislab-tecnico-lisboa/KinectBody2Yarp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKinectBody2Yarp.cpp
More file actions
318 lines (268 loc) · 10.4 KB
/
KinectBody2Yarp.cpp
File metadata and controls
318 lines (268 loc) · 10.4 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
305
306
307
308
309
310
311
312
313
314
315
316
317
// KinectBody2Yarp.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include <cstdio>
#include <stdio.h>
#include <Windows.h>
#include <NuiKinectFusionApi.h>
#include <opencv2/opencv.hpp>
/* Get all OS and signal processing YARP classes */
#include <opencv2/legacy/compat.hpp> // for cvCopyImage
#include <yarp/os/all.h>
#include <yarp/sig/all.h>
#include <yarp/os/Wire.h>
#include <yarp/os/idl/WireTypes.h>
#include "KinectTrackedPeopleMessage.h"
#include <chrono>
#include <ctime> // to fill the timestamp field of ROS Message
using namespace yarp::os;
using namespace yarp::sig;
using namespace std;
template<class Interface>
inline void SafeRelease(Interface *& pInterfaceToRelease)
{
if (pInterfaceToRelease != NULL){
pInterfaceToRelease->Release();
pInterfaceToRelease = NULL;
}
}
int main()
{
Network yarp;
if (!yarp.checkNetwork())
{
fprintf(stdout, "YARP server not available!\n");
return 1;
}
BufferedPort<Bottle> bodiesOutputPort;
bodiesOutputPort.open("/kinect/bodyBottle/out");
if (bodiesOutputPort.isClosed()){
throw(runtime_error("bodiesOutputPort did not open."));
}
BufferedPort<KinectTrackedPeopleMessage> bodiesOutputPortROS;
bodiesOutputPortROS.open("/kinect/bodyBottleROS/out");
if (bodiesOutputPortROS.isClosed()){
throw(runtime_error("bodiesOutputPortROS did not open."));
}
Network::connect("/kinect/bodyBottleROS/out", "/read");
// Sensor
IKinectSensor* pSensor;
HRESULT hResult = S_OK;
hResult = GetDefaultKinectSensor(&pSensor);
if (FAILED(hResult)){
std::cerr << "Error : GetDefaultKinectSensor" << std::endl;
return -1;
}
hResult = pSensor->Open();
if (FAILED(hResult)){
std::cerr << "Error : IKinectSensor::Open()" << std::endl;
return -1;
}
// Source
IColorFrameSource* pColorSource;
hResult = pSensor->get_ColorFrameSource(&pColorSource);
if (FAILED(hResult)){
std::cerr << "Error : IKinectSensor::get_ColorFrameSource()" << std::endl;
return -1;
}
IBodyFrameSource* pBodySource;
hResult = pSensor->get_BodyFrameSource(&pBodySource);
if (FAILED(hResult)){
std::cerr << "Error : IKinectSensor::get_BodyFrameSource()" << std::endl;
return -1;
}
// Reader
IColorFrameReader* pColorReader;
hResult = pColorSource->OpenReader(&pColorReader);
if (FAILED(hResult)){
std::cerr << "Error : IColorFrameSource::OpenReader()" << std::endl;
return -1;
}
IBodyFrameReader* pBodyReader;
hResult = pBodySource->OpenReader(&pBodyReader);
if (FAILED(hResult)){
std::cerr << "Error : IBodyFrameSource::OpenReader()" << std::endl;
return -1;
}
// Description
IFrameDescription* pDescription;
hResult = pColorSource->get_FrameDescription(&pDescription);
if (FAILED(hResult)){
std::cerr << "Error : IColorFrameSource::get_FrameDescription()" << std::endl;
return -1;
}
int width = 0;
int height = 0;
pDescription->get_Width(&width); // 1920
pDescription->get_Height(&height); // 1080
unsigned int bufferSize = width * height * 4 * sizeof(unsigned char);
cv::Mat bufferMat(height, width, CV_8UC4);
cv::Mat bodyMat(height / 2, width / 2, CV_8UC4);
cv::namedWindow("Body");
// Color Table
cv::Vec3b color[BODY_COUNT];
color[0] = cv::Vec3b(255, 0, 0);
color[1] = cv::Vec3b(0, 255, 0);
color[2] = cv::Vec3b(0, 0, 255);
color[3] = cv::Vec3b(255, 255, 0);
color[4] = cv::Vec3b(255, 0, 255);
color[5] = cv::Vec3b(0, 255, 255);
// Coordinate Mapper
ICoordinateMapper* pCoordinateMapper;
hResult = pSensor->get_CoordinateMapper(&pCoordinateMapper);
if (FAILED(hResult)){
std::cerr << "Error : IKinectSensor::get_CoordinateMapper()" << std::endl;
return -1;
}
while (1){
// Frame
IColorFrame* pColorFrame = nullptr;
hResult = pColorReader->AcquireLatestFrame(&pColorFrame);
if (SUCCEEDED(hResult)){
hResult = pColorFrame->CopyConvertedFrameDataToArray(bufferSize, reinterpret_cast<BYTE*>(bufferMat.data), ColorImageFormat::ColorImageFormat_Bgra);
if (SUCCEEDED(hResult)){
cv::resize(bufferMat, bodyMat, cv::Size(), 0.5, 0.5);
}
}
IBodyFrame* pBodyFrame = nullptr;
hResult = pBodyReader->AcquireLatestFrame(&pBodyFrame);
if (SUCCEEDED(hResult)){
IBody* pBody[BODY_COUNT] = { 0 };
hResult = pBodyFrame->GetAndRefreshBodyData(BODY_COUNT, pBody);
if (SUCCEEDED(hResult)){
Bottle& sendBottle = bodiesOutputPort.prepare(); // Yarp Bottle
KinectTrackedPeopleMessage& sendBottleROS = bodiesOutputPortROS.prepare(); // ROS Message
sendBottleROS.bodies.clear();
Bottle bodiesBottle;
int numTrackedPpl = 0;
for (int count = 0; count < BODY_COUNT; count++){
BOOLEAN bTracked = false;
hResult = pBody[count]->get_IsTracked(&bTracked);
if (SUCCEEDED(hResult) && bTracked){
numTrackedPpl++;
Joint joint[JointType::JointType_Count];
HRESULT hResultJoints = S_OK;
hResultJoints = pBody[count]->GetJoints(JointType::JointType_Count, joint);
JointOrientation jointsOrientations[JointType::JointType_Count];
HRESULT hResultJointsOrientations = S_OK;
hResultJointsOrientations = pBody[count]->GetJointOrientations(JointType::JointType_Count, jointsOrientations);
if (SUCCEEDED(hResultJoints) && SUCCEEDED(hResultJointsOrientations)){
// Add the tracking ID
UINT64 trackingID = 0;
pBody[count]->get_TrackingId(&trackingID);
char str[100];
sprintf_s(str, "%I64u", trackingID); // WINDOWS ONLY code
Bottle bodyBottle; // Yarp Bottle
bodyBottle.addString(str);
Body body; // ROS Message
body.trackID = str;
// Joints
for (int type = 0; type < JointType::JointType_Count; type++){
ColorSpacePoint colorSpacePoint = { 0 };
pCoordinateMapper->MapCameraPointToColorSpace(joint[type].Position, &colorSpacePoint);
int x = static_cast<int>(colorSpacePoint.X);
int y = static_cast<int>(colorSpacePoint.Y);
if ((x >= 0) && (x < width) && (y >= 0) && (y < height)){
cv::circle(bufferMat, cv::Point(x, y), 5, static_cast<cv::Scalar>(color[count]), -1); // , CV_AA );
}
Bottle jointBottle;
Bottle typeBottle;
typeBottle.addInt(type);
const char * JointTypesStr[] = { "SpineBase", "SpineMid", "Neck", "Head", "ShoulderLeft",
"ElbowLeft", "WristLeft", "HandLeft", "ShoulderRight", "ElbowRight", "WristRight",
"HandRight", "HipLeft", "KneeLeft", "AnkleLeft", "FootLeft", "HipRight", "KneeRight",
"AnkleRight", "FootRight", "SpineShoulder", "HandTipLeft", "ThumbLeft", "HandTipRight", "ThumbRight" };
typeBottle.addString(JointTypesStr[type]);
jointBottle.addList() = typeBottle;
Bottle trackingBottle;
trackingBottle.addInt(joint[type].TrackingState);
jointBottle.addList() = trackingBottle;
Bottle positionBottle;
positionBottle.addDouble(joint[type].Position.X);
positionBottle.addDouble(joint[type].Position.Y);
positionBottle.addDouble(joint[type].Position.Z);
jointBottle.addList() = positionBottle;
Bottle orientationBottle;
orientationBottle.addDouble(jointsOrientations[type].Orientation.x);
orientationBottle.addDouble(jointsOrientations[type].Orientation.y);
orientationBottle.addDouble(jointsOrientations[type].Orientation.z);
orientationBottle.addDouble(jointsOrientations[type].Orientation.w);
jointBottle.addList() = orientationBottle;
bodyBottle.addList() = jointBottle;
// ROS Message
body.joints[type].typeNum = type;
body.joints[type].typeStr = JointTypesStr[type];
body.joints[type].pose.position.x = joint[type].Position.X;
body.joints[type].pose.position.y = joint[type].Position.Y;
body.joints[type].pose.position.z = joint[type].Position.Z;
body.joints[type].pose.orientation.x = jointsOrientations[type].Orientation.x;
body.joints[type].pose.orientation.y = jointsOrientations[type].Orientation.y;
body.joints[type].pose.orientation.z = jointsOrientations[type].Orientation.z;
body.joints[type].pose.orientation.w = jointsOrientations[type].Orientation.w;
}
//cout << sendBottle.toString().c_str() << endl;
bodiesBottle.addList() = bodyBottle; // Yarp Bottle
sendBottleROS.bodies.push_back(body); // ROS Message
}
}
}
cv::resize(bufferMat, bodyMat, cv::Size(), 0.5, 0.5);
// Only send bottle when people tracked (could send bottle with just floor estimation)
if (numTrackedPpl){
// Yarp Bottle (only need to compute time stamp and floor if we are going to send the message)
Bottle floorBottle;
Vector4 floorPlane;
pBodyFrame->get_FloorClipPlane(&floorPlane);
floorBottle.addDouble(floorPlane.x);
floorBottle.addDouble(floorPlane.y);
floorBottle.addDouble(floorPlane.z);
floorBottle.addDouble(floorPlane.w);
sendBottle.addString("floor");
sendBottle.addList() = floorBottle;
sendBottle.addString("bodies");
sendBottle.addList() = bodiesBottle;
bodiesOutputPort.write();
// ROS Message (only need to compute time stamp and floor if we are going to send the message)
sendBottleROS.floorPlane.x = floorPlane.x; // Fill in floor
sendBottleROS.floorPlane.y = floorPlane.y;
sendBottleROS.floorPlane.z = floorPlane.z;
sendBottleROS.floorPlane.w = floorPlane.w;
auto nanoseconds_since_epoch =
std::chrono::duration_cast<std::chrono::nanoseconds>
(std::chrono::system_clock::now().time_since_epoch()).count();
auto seconds_since_epoch =
std::chrono::duration_cast<std::chrono::seconds>
(std::chrono::system_clock::now().time_since_epoch()).count();
auto nanoseconds = nanoseconds_since_epoch - seconds_since_epoch * 1000000000;
sendBottleROS.header.stamp.sec = seconds_since_epoch; // Fill in time
sendBottleROS.header.stamp.nsec = nanoseconds;
sendBottleROS.header.frame_id = "0"; // Unused field
sendBottleROS.header.seq = 0; // Unused field
bodiesOutputPortROS.write();
}
}
for (int count = 0; count < BODY_COUNT; count++){
SafeRelease(pBody[count]);
}
}
SafeRelease(pColorFrame);
SafeRelease(pBodyFrame);
cv::imshow("Body", bodyMat);
if (cv::waitKey(10) == VK_ESCAPE){
break;
}
}
SafeRelease(pColorSource);
SafeRelease(pBodySource);
SafeRelease(pColorReader);
SafeRelease(pBodyReader);
SafeRelease(pDescription);
SafeRelease(pCoordinateMapper);
if (pSensor){
pSensor->Close();
}
SafeRelease(pSensor);
cv::destroyAllWindows();
return 0;
}