-
Notifications
You must be signed in to change notification settings - Fork 63
/
rosOpenpose.cpp
486 lines (413 loc) · 19.7 KB
/
rosOpenpose.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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
/**
* rosOpenpose.cpp: the main file. it consists of two workers input and output worker.
* the job of the input worker is to provide color images to openpose wrapper.
* the job of the output worker is to receive the keypoints detected in 2D
* space. it then converts 2D pixels to 3D coordinates (wrt camera coordinate
* system)/
* Author: Ravi Joshi
* Date: 2019/09/27
* src: https://github.com/CMU-Perceptual-Computing-Lab/openpose/tree/master/examples/tutorial_api_cpp
*/
// todo: merge the 'for' loop for body and hand keypoints into one
// ROS headers
#include <ros/ros.h>
// ros_openpose headers
#include <ros_openpose/Frame.h>
#include <ros_openpose/cameraReader.hpp>
// OpenPose headers
#include <openpose/flags.hpp>
#include <openpose/headers.hpp>
// define sleep for input and output worker in milliseconds
const int SLEEP_MS = 10;
// define a few datatype
typedef std::shared_ptr<op::Datum> sPtrDatum;
typedef std::shared_ptr<std::vector<sPtrDatum>> sPtrVecSPtrDatum;
// the input worker. the job of this worker is to provide color imagees to
// openpose wrapper
class WUserInput : public op::WorkerProducer<sPtrVecSPtrDatum>
{
public:
WUserInput(const std::shared_ptr<ros_openpose::CameraReader>& sPtrCameraReader) : mSPtrCameraReader(sPtrCameraReader)
{
}
void initializationOnThread()
{
}
sPtrVecSPtrDatum workProducer()
{
try
{
auto frameNumber = mSPtrCameraReader->getFrameNumber();
if (frameNumber == 0 || frameNumber == mFrameNumber)
{
// display the error at most once per 10 seconds
ROS_WARN_THROTTLE(10, "Waiting for color image frame...");
std::this_thread::sleep_for(std::chrono::milliseconds{SLEEP_MS});
return nullptr;
}
else
{
// update frame number
mFrameNumber = frameNumber;
// get the latest color image from the camera
auto& colorImage = mSPtrCameraReader->getColorFrame();
if (!colorImage.empty())
{
// create new datum
auto datumsPtr = std::make_shared<std::vector<sPtrDatum>>();
datumsPtr->emplace_back();
auto& datumPtr = datumsPtr->at(0);
datumPtr = std::make_shared<op::Datum>();
// fill the datum
datumPtr->cvInputData = colorImage;
return datumsPtr;
}
else
{
// display the error at most once per 10 seconds
ROS_WARN_THROTTLE(10, "Empty color image frame detected. Ignoring...");
return nullptr;
}
}
}
catch (const std::exception& e)
{
this->stop();
// display the error at most once per 10 seconds
ROS_ERROR_THROTTLE(10, "Error %s at line number %d on function %s in file %s", e.what(), __LINE__, __FUNCTION__, __FILE__);
return nullptr;
}
}
private:
ullong mFrameNumber = 0ULL;
const std::shared_ptr<ros_openpose::CameraReader> mSPtrCameraReader;
};
// the outpout worker. the job of the output worker is to receive the keypoints
// detected in 2D space. it then converts 2D pixels to 3D coordinates (wrt
// camera coordinate system).
class WUserOutput : public op::WorkerConsumer<sPtrVecSPtrDatum>
{
public:
// clang-format off
WUserOutput(const ros::Publisher& framePublisher,
const std::shared_ptr<ros_openpose::CameraReader>& sPtrCameraReader,
const std::string& frameId, const bool noDepth)
: mFramePublisher(framePublisher), mSPtrCameraReader(sPtrCameraReader), mNoDepth(noDepth)
{
mFrame.header.frame_id = frameId;
}
// clang-format on
void initializationOnThread()
{
}
template <typename Array>
void fillBodyROSMsg(Array& poseKeypoints, int person, int bodyPartCount)
{
#pragma omp parallel for
for (auto bodyPart = 0; bodyPart < bodyPartCount; bodyPart++)
{
// src:
// https://github.com/CMU-Perceptual-Computing-Lab/openpose/blob/master/doc/output.md#keypoint-format-in-the-c-api
const auto baseIndex = poseKeypoints.getSize(2) * (person * bodyPartCount + bodyPart);
const auto x = poseKeypoints[baseIndex];
const auto y = poseKeypoints[baseIndex + 1];
const auto score = poseKeypoints[baseIndex + 2];
float point3D[3];
// compute 3D point only if depth flag is set
if (!mNoDepth)
mSPtrCameraReader->compute3DPoint(x, y, point3D);
mFrame.persons[person].bodyParts[bodyPart].pixel.x = x;
mFrame.persons[person].bodyParts[bodyPart].pixel.y = y;
mFrame.persons[person].bodyParts[bodyPart].score = score;
mFrame.persons[person].bodyParts[bodyPart].point.x = point3D[0];
mFrame.persons[person].bodyParts[bodyPart].point.y = point3D[1];
mFrame.persons[person].bodyParts[bodyPart].point.z = point3D[2];
}
}
template <typename ArrayOfArray>
void fillHandROSMsg(ArrayOfArray& handKeypoints, int person, int handPartCount)
{
#pragma omp parallel for
for (auto handPart = 0; handPart < handPartCount; handPart++)
{
const auto baseIndex = handKeypoints[0].getSize(2) * (person * handPartCount + handPart);
// left hand
const auto xLeft = handKeypoints[0][baseIndex];
const auto yLeft = handKeypoints[0][baseIndex + 1];
const auto scoreLeft = handKeypoints[0][baseIndex + 2];
// right hand
const auto xRight = handKeypoints[1][baseIndex];
const auto yRight = handKeypoints[1][baseIndex + 1];
const auto scoreRight = handKeypoints[1][baseIndex + 2];
float point3DLeft[3];
float point3DRight[3];
// compute 3D point only if depth flag is set
if (!mNoDepth)
{
mSPtrCameraReader->compute3DPoint(xLeft, yLeft, point3DLeft);
mSPtrCameraReader->compute3DPoint(xRight, yRight, point3DRight);
}
mFrame.persons[person].leftHandParts[handPart].pixel.x = xLeft;
mFrame.persons[person].leftHandParts[handPart].pixel.y = yLeft;
mFrame.persons[person].leftHandParts[handPart].score = scoreLeft;
mFrame.persons[person].leftHandParts[handPart].point.x = point3DLeft[0];
mFrame.persons[person].leftHandParts[handPart].point.y = point3DLeft[1];
mFrame.persons[person].leftHandParts[handPart].point.z = point3DLeft[2];
mFrame.persons[person].rightHandParts[handPart].pixel.x = xRight;
mFrame.persons[person].rightHandParts[handPart].pixel.y = yRight;
mFrame.persons[person].rightHandParts[handPart].score = scoreRight;
mFrame.persons[person].rightHandParts[handPart].point.x = point3DRight[0];
mFrame.persons[person].rightHandParts[handPart].point.y = point3DRight[1];
mFrame.persons[person].rightHandParts[handPart].point.z = point3DRight[2];
}
}
void workConsumer(const sPtrVecSPtrDatum& datumsPtr)
{
try
{
if (datumsPtr != nullptr && !datumsPtr->empty())
{
// update timestamp
mFrame.header.stamp = ros::Time::now();
// make sure to clear previous data
mFrame.persons.clear();
// we use the latest depth image for computing point in 3D space
mSPtrCameraReader->lockLatestDepthImage();
// accesing each element of the keypoints
const auto& poseKeypoints = datumsPtr->at(0)->poseKeypoints;
const auto& handKeypoints = datumsPtr->at(0)->handKeypoints;
// get the size
const auto personCount = poseKeypoints.getSize(0);
const auto bodyPartCount = poseKeypoints.getSize(1);
const auto handPartCount = handKeypoints[0].getSize(1);
mFrame.persons.resize(personCount);
// update with the new data
for (auto person = 0; person < personCount; person++)
{
mFrame.persons[person].bodyParts.resize(bodyPartCount);
mFrame.persons[person].leftHandParts.resize(handPartCount);
mFrame.persons[person].rightHandParts.resize(handPartCount);
fillBodyROSMsg(poseKeypoints, person, bodyPartCount);
fillHandROSMsg(handKeypoints, person, handPartCount);
}
mFramePublisher.publish(mFrame);
}
else
{
// display the error at most once per 10 seconds
ROS_WARN_THROTTLE(10, "Waiting for datum...");
std::this_thread::sleep_for(std::chrono::milliseconds{SLEEP_MS});
}
}
catch (const std::exception& e)
{
this->stop();
ROS_ERROR("Error %s at line number %d on function %s in file %s", e.what(), __LINE__, __FUNCTION__, __FILE__);
}
}
private:
const bool mNoDepth;
ros_openpose::Frame mFrame;
const ros::Publisher mFramePublisher;
const std::shared_ptr<ros_openpose::CameraReader> mSPtrCameraReader;
};
// clang-format off
void configureOpenPose(op::Wrapper& opWrapper,
const std::shared_ptr<ros_openpose::CameraReader>& cameraReader,
const ros::Publisher& framePublisher,
const std::string& frameId, const bool noDepth)
// clang-format on
{
try
{
// Configuring OpenPose
// clang-format off
// logging_level
op::check(0 <= FLAGS_logging_level && FLAGS_logging_level <= 255,
"Wrong logging_level value.",
__LINE__,
__FUNCTION__,
__FILE__);
op::ConfigureLog::setPriorityThreshold((op::Priority)FLAGS_logging_level);
op::Profiler::setDefaultX(FLAGS_profile_speed);
// Applying user defined configuration - GFlags to program variables
// outputSize
const auto outputSize = op::flagsToPoint(FLAGS_output_resolution, "-1x-1");
// netInputSize
const auto netInputSize = op::flagsToPoint(FLAGS_net_resolution, "-1x368");
// faceNetInputSize
const auto faceNetInputSize = op::flagsToPoint(FLAGS_face_net_resolution, "368x368 (multiples of 16)");
// handNetInputSize
const auto handNetInputSize = op::flagsToPoint(FLAGS_hand_net_resolution, "368x368 (multiples of 16)");
// poseMode
const auto poseMode = op::flagsToPoseMode(FLAGS_body);
// poseModel
const auto poseModel = op::flagsToPoseModel(FLAGS_model_pose);
// JSON saving
if (!FLAGS_write_keypoint.empty())
ROS_INFO("Flag `write_keypoint` is deprecated and will eventually be removed. Please, use `write_json` instead.");
// keypointScaleMode
const auto keypointScaleMode = op::flagsToScaleMode(FLAGS_keypoint_scale);
// heatmaps to add
const auto heatMapTypes = op::flagsToHeatMaps(FLAGS_heatmaps_add_parts,
FLAGS_heatmaps_add_bkg,
FLAGS_heatmaps_add_PAFs);
const auto heatMapScaleMode = op::flagsToHeatMapScaleMode(FLAGS_heatmaps_scale);
// >1 camera view?
// const auto multipleView = (FLAGS_3d || FLAGS_3d_views > 1 || FLAGS_flir_camera);
const auto multipleView = false;
// Face and hand detectors
const auto faceDetector = op::flagsToDetector(FLAGS_face_detector);
const auto handDetector = op::flagsToDetector(FLAGS_hand_detector);
// Enabling Google Logging
const bool enableGoogleLogging = true;
// Initializing the user custom classes
auto wUserInput = std::make_shared<WUserInput>(cameraReader);
auto wUserOutput = std::make_shared<WUserOutput>(framePublisher, cameraReader, frameId, noDepth);
// Add custom processing
const auto workerInputOnNewThread = true;
opWrapper.setWorker(op::WorkerType::Input, wUserInput, workerInputOnNewThread);
const auto workerOutputOnNewThread = true;
opWrapper.setWorker(op::WorkerType::Output, wUserOutput, workerOutputOnNewThread);
// Pose configuration (use WrapperStructPose{} for default and recommended configuration)
const op::WrapperStructPose wrapperStructPose{poseMode,
netInputSize,
outputSize,
keypointScaleMode,
FLAGS_num_gpu,
FLAGS_num_gpu_start,
FLAGS_scale_number,
(float)FLAGS_scale_gap,
op::flagsToRenderMode(FLAGS_render_pose,
multipleView),
poseModel,
!FLAGS_disable_blending,
(float)FLAGS_alpha_pose,
(float)FLAGS_alpha_heatmap,
FLAGS_part_to_show,
FLAGS_model_folder,
heatMapTypes,
heatMapScaleMode,
FLAGS_part_candidates,
(float)FLAGS_render_threshold,
FLAGS_number_people_max,
FLAGS_maximize_positives,
FLAGS_fps_max,
FLAGS_prototxt_path,
FLAGS_caffemodel_path,
(float)FLAGS_upsampling_ratio,
enableGoogleLogging};
opWrapper.configure(wrapperStructPose);
// Face configuration (use op::WrapperStructFace{} to disable it)
const op::WrapperStructFace wrapperStructFace{FLAGS_face,
faceDetector,
faceNetInputSize,
op::flagsToRenderMode(FLAGS_face_render,
multipleView,
FLAGS_render_pose),
(float)FLAGS_face_alpha_pose,
(float)FLAGS_face_alpha_heatmap,
(float)FLAGS_face_render_threshold};
opWrapper.configure(wrapperStructFace);
// Hand configuration (use op::WrapperStructHand{} to disable it)
const op::WrapperStructHand wrapperStructHand{FLAGS_hand,
handDetector,
handNetInputSize,
FLAGS_hand_scale_number,
(float)FLAGS_hand_scale_range,
op::flagsToRenderMode(FLAGS_hand_render,
multipleView,
FLAGS_render_pose),
(float)FLAGS_hand_alpha_pose,
(float)FLAGS_hand_alpha_heatmap,
(float)FLAGS_hand_render_threshold};
opWrapper.configure(wrapperStructHand);
// Extra functionality configuration (use op::WrapperStructExtra{} to disable it)
const op::WrapperStructExtra wrapperStructExtra{FLAGS_3d,
FLAGS_3d_min_views,
FLAGS_identification,
FLAGS_tracking,
FLAGS_ik_threads};
opWrapper.configure(wrapperStructExtra);
// Output (comment or use default argument to disable any output)
const op::WrapperStructOutput wrapperStructOutput{FLAGS_cli_verbose,
FLAGS_write_keypoint,
op::stringToDataFormat(FLAGS_write_keypoint_format),
FLAGS_write_json,
FLAGS_write_coco_json,
FLAGS_write_coco_json_variants,
FLAGS_write_coco_json_variant,
FLAGS_write_images,
FLAGS_write_images_format,
FLAGS_write_video,
FLAGS_write_video_fps,
FLAGS_write_video_with_audio,
FLAGS_write_heatmaps,
FLAGS_write_heatmaps_format,
FLAGS_write_video_3d,
FLAGS_write_video_adam,
FLAGS_write_bvh,
FLAGS_udp_host,
FLAGS_udp_port};
opWrapper.configure(wrapperStructOutput);
// GUI (comment or use default argument to disable any visual output)
const op::WrapperStructGui wrapperStructGui{op::flagsToDisplayMode(FLAGS_display,
FLAGS_3d),
!FLAGS_no_gui_verbose,
FLAGS_fullscreen};
opWrapper.configure(wrapperStructGui);
// clang-format on
// Set to single-thread (for sequential processing and/or debugging and/or reducing latency)
if (FLAGS_disable_multi_thread)
opWrapper.disableMultiThreading();
}
catch (const std::exception& e)
{
ROS_ERROR("Error %s at line number %d on function %s in file %s", e.what(), __LINE__, __FUNCTION__, __FILE__);
}
}
int main(int argc, char* argv[])
{
ros::init(argc, argv, "ros_openpose_node");
ros::NodeHandle nh("~");
// define the parameters, we are going to read
bool noDepth;
std::string colorTopic, depthTopic, camInfoTopic, frameId, pubTopic;
// read the parameters from relative nodel handle
nh.getParam("frame_id", frameId);
nh.getParam("pub_topic", pubTopic);
nh.param("no_depth", noDepth, false); // default value is false
nh.getParam("color_topic", colorTopic);
nh.getParam("depth_topic", depthTopic);
nh.getParam("cam_info_topic", camInfoTopic);
if (pubTopic.empty())
{
ROS_FATAL("Missing 'pub_topic' info in launch file. Please make sure that you have executed 'run.launch' file.");
exit(-1);
}
// parsing command line flags
gflags::ParseCommandLineFlags(&argc, &argv, true);
const auto cameraReader = std::make_shared<ros_openpose::CameraReader>(nh, colorTopic, depthTopic, camInfoTopic);
// the frame consists of the location of detected body parts of each person
const ros::Publisher framePublisher = nh.advertise<ros_openpose::Frame>(pubTopic, 1);
try
{
ROS_INFO("Starting ros_openpose...");
op::Wrapper opWrapper;
configureOpenPose(opWrapper, cameraReader, framePublisher, frameId, noDepth);
// start and run
opWrapper.start();
// exit when Ctrl-C is pressed, or the node is shutdown by the master
ros::spin();
// return successful message
ROS_INFO("Exiting ros_openpose...");
// stop processing
opWrapper.stop();
return 0;
}
catch (const std::exception& e)
{
ROS_ERROR("Error %s at line number %d on function %s in file %s", e.what(), __LINE__, __FUNCTION__, __FILE__);
return -1;
}
}