-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathedpr-april.cpp
More file actions
579 lines (495 loc) · 19 KB
/
Copy pathedpr-april.cpp
File metadata and controls
579 lines (495 loc) · 19 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
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
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
/*
Author: Franco Di Pietro, Arren Glover
*/
#include <yarp/cv/Cv.h>
#include <yarp/os/all.h>
#include <yarp/sig/Image.h>
#include <event-driven/core.h>
#include <hpe-core/utility.h>
#include <hpe-core/motion_estimation.h>
#include <hpe-core/fusion.h>
#include <hpe-core/motion.h>
#include <opencv2/opencv.hpp>
#include <vector>
#include <string>
#include "april_msgs/yarp/rosmsg/april_msgs/NChumanPose.h"
#include <yarp/rosmsg/sensor_msgs/Image.h>
using namespace yarp::os;
using namespace yarp::sig;
using std::vector;
class externalDetector
{
private:
double period{0.1}, tic{0.0};
bool waiting{false};
BufferedPort<ImageOf<PixelMono>> output_port;
BufferedPort<Bottle> input_port;
public:
bool init(std::string output_name, std::string input_name, double rate)
{
if (!output_port.open(output_name))
return false;
if (!input_port.open(input_name))
return false;
period = 1.0 / rate;
return true;
}
void close()
{
output_port.close();
input_port.close();
}
bool update(const cv::Mat &latest_image, double latest_ts, hpecore::stampedPose &previous_skeleton)
{
// send an update if the timer has elapsed
if(latest_ts < tic) tic = latest_ts - 2.0;
if ((!waiting && latest_ts - tic > period) || (latest_ts - tic > 2.0))
{
static cv::Mat cv_image;
latest_image.convertTo(cv_image, CV_8U);
cv::GaussianBlur(cv_image, cv_image, cv::Size(5, 5), 0, 0);
output_port.prepare().copy(yarp::cv::fromCvMat<PixelMono>(cv_image));
output_port.write();
tic = latest_ts;
waiting = true;
}
// read a ready data
Bottle *mn_container = input_port.read(false);
if (mn_container)
{
previous_skeleton.pose = hpecore::extractSkeletonFromYARP<Bottle>(*mn_container);
previous_skeleton.conf = hpecore::extractConfidenceFromYARP<Bottle>(*mn_container);
previous_skeleton.timestamp = tic;
previous_skeleton.delay = latest_ts - tic;
waiting = false;
}
return mn_container != nullptr;
}
};
class delayedGT
{
private:
bool delay{false};
double rate{10};
BufferedPort<Bottle> input_port;
hpecore::stampedPose internal{0.0, -1.0, {0}};
public:
bool init(std::string input_name, double rate, bool delay)
{
if (!input_port.open(input_name))
return false;
this->delay = delay;
this->rate = rate;
return true;
}
void close()
{
input_port.close();
}
bool update(double latest_ts, hpecore::stampedPose &previous_skeleton)
{
Bottle *gt_container = input_port.read(false);
if (gt_container && (rate * (latest_ts - internal.timestamp) > 1.0))
{
// if we have delay set the previous result to be returned
if (delay)
previous_skeleton = internal;
// grab the new skeleton and set the timestamp to now
internal.pose = hpecore::extractSkeletonFromYARP<Bottle>(*gt_container);
internal.timestamp = latest_ts;
// if we don't delay set the current result to be returned
if (!delay)
previous_skeleton = internal;
// the delay is the difference between now and returned timestamp
previous_skeleton.delay = latest_ts - previous_skeleton.timestamp;
return true;
}
return false;
}
};
class APRIL_HPE : public RFModule
{
private:
// event reading
std::thread camera_handler_thread;
std::thread hpe_thread;
ev::window<ev::AE> input_events;
// detection handlers
externalDetector mn_handler;
delayedGT gt_handler;
hpecore::EROS eros_handler;
hpecore::SAE sae_handler;
hpecore::BIN binary_handler;
// velocity and fusion
hpecore::pwtripletvelocity velocity_estimator;
hpecore::pwTripletVelocity pw_trip_velocity;
hpecore::multiJointLatComp state;
// internal data structures
//hpecore::skeleton13 skeleton_gt{0};
//hpecore::skeleton13 skeleton_detection{0};
hpecore::stampedPose detected_pose;
cv::Size image_size;
cv::Mat edpr_logo;
// parameters
int detF{10}, roiSize{20};
bool pltVel{false}, pltDet{false}, pltTra{false};
int alt_view{0};
bool latency_compensation{true};
double scaler{1.0};
double th_period{0.01}, thF{100.0};
bool pltRoi{false};
double c_thresh{0.4};
cv::Scalar colors[13] = {{0, 0, 180}, {0, 180, 0}, {0, 0, 180},
{180, 180, 0}, {180, 0, 180}, {0, 180, 180},
{120, 0, 180}, {120, 180, 0}, {0, 120, 180},
{120, 120, 180}, {120, 180, 120}, {120, 120, 180}, {120, 120, 120}};
bool started{false};
double tnow;
// ros
yarp::os::Node* ros_node{nullptr};
yarp::os::Publisher<yarp::rosmsg::april_msgs::NChumanPose> ros_publisher;
yarp::rosmsg::april_msgs::NChumanPose ros_output;
typedef yarp::os::Publisher<yarp::rosmsg::sensor_msgs::Image> ImageTopicType;
ImageTopicType publisherPort_eros, publisherPort_evs;
int counter{0};
public:
bool configure(yarp::os::ResourceFinder &rf) override
{
if(rf.check("help")) {
yInfo() << " EDPR APRIL HPE ";
yInfo() << "--name <string> : name of module for YARP ports";
yInfo() << "--f_vis <float> : visualisation rate [20]";
yInfo() << "--f_det <float> : HPE detection rate [5]";
yInfo() << "--pu <float> : KF process uncertainty [10.0]";
yInfo() << "--muD <float> : KF measurement uncertainty [1.0]";
yInfo() << "--confidence <float> : threshold for skeleton confidence [0.4]";
return false;
}
// =====SET UP YARP=====
if (!yarp::os::Network::checkNetwork(2.0))
{
std::cout << "Could not connect to YARP" << std::endl;
return false;
}
// set the module name used to name ports
setName((rf.check("name", Value("/edpr_april")).asString()).c_str());
if (!input_events.open(getName("/AE:i")))
{
yError() << "Could not open events input port";
return false;
}
// =====READ PARAMETERS=====
pltDet = rf.check("pltDet") && rf.check("pltDet", Value(true)).asBool();
pltTra = rf.check("pltTra") && rf.check("pltTra", Value(true)).asBool();
pltRoi = rf.check("pr") && rf.check("pr", Value(true)).asBool();
detF = rf.check("f_det", Value(10)).asInt32();
image_size = cv::Size(rf.check("w", Value(640)).asInt32(),
rf.check("h", Value(480)).asInt32());
roiSize = rf.check("roi", Value(20)).asInt32();
double procU = rf.check("pu", Value(1e-1)).asFloat64();
double measUD = rf.check("muD", Value(1e-4)).asFloat64();
double measUV = rf.check("muV", Value(0)).asFloat64();
std::string checkpoint_path = rf.check("checkpoint_path", Value("/usr/local/src/hpe-core/example/movenet/models/e97_valacc0.81209.pth")).asString();
latency_compensation = rf.check("use_lc") && rf.check("use_lc", Value(true)).asBool();
double lc = latency_compensation ? 1.0 : 0.0;
thF = rf.check("f_vis", Value(100.0)).asFloat64();
th_period = 1/thF;
c_thresh = rf.check("confidence", Value(0.4)).asFloat64();
// pltDet = true;
pltTra = true;
// concatenate the checkpoint path
std::string command = "python3 /usr/local/src/hpe-core/example/movenet/movenet_online.py --gpu --checkpoint_path " + checkpoint_path + " &";
int r = system(command.c_str());
while (!yarp::os::NetworkBase::exists("/movenet/sklt:o"))
sleep(1);
yInfo() << "MoveEnet started correctly";
if (!mn_handler.init(getName("/eros:o"), getName("/movenet:i"), detF))
{
yError() << "Could not open movenet ports";
return false;
}
// ===== SET UP INTERNAL VARIABLE/DATA STRUCTURES =====
// shared images
eros_handler.init(image_size.width, image_size.height, 7, 0.3);
binary_handler.init(image_size.width, image_size.height);
sae_handler.init(image_size.width, image_size.height);
edpr_logo = cv::imread("/usr/local/src/EDPR-APRIL/edpr_logo.png");
//velocity estimation
pw_trip_velocity.setParameters(roiSize, 1, image_size);
// fusion
if (!state.initialise({procU, measUD, measUV, lc}))
{
yError() << "Not KF initialized";
return false;
}
// ===== TRY DEFAULT CONNECTIONS =====
Network::connect("/file/ch0dvs:o", getName("/AE:i"), "fast_tcp");
Network::connect("/atis3/AE:o", getName("/AE:i"), "fast_tcp");
Network::connect("/file/ch2GT50Hzskeleton:o", getName("/gt:i"), "fast_tcp");
Network::connect("/movenet/sklt:o", getName("/movenet:i"), "fast_tcp");
Network::connect("/zynqGrabber/AE:o", getName("/AE:i"), "fast_tcp");
Network::connect(getName("/eros:o"), "/movenet/img:i", "fast_tcp");
Network::connect("/file/atis/AE:o", getName("/AE:i"), "fast_tcp");
cv::namedWindow("edpr-april", cv::WINDOW_NORMAL);
cv::resizeWindow("edpr-april", image_size);
// set-up ROS interface
ros_node = new yarp::os::Node("/edpraprilhpe");
if (!ros_publisher.topic("/pem/neuromorphic_camera/data"))
{
yError() << "Could not open ROS pose output publisher";
return false;
}
if (!publisherPort_eros.topic("/isim/neuromorphic_camera/eros"))
{
yError() << "Could not open ROS EROS output publisher";
return false;
}
if (!publisherPort_evs.topic("/isim/neuromorphic_camera/evs"))
{
yError() << "Could not open ROS EVS output publisher";
return false;
}
camera_handler_thread = std::thread([this]{ this->run_camera_interface(); });
hpe_thread = std::thread([this]{ this->run_hpe(); });
return true;
}
double getPeriod() override
{
// run the module as fast as possible. Only as fast as new images are
// available and then limited by how fast OpenPose takes to run
return th_period;
}
bool interruptModule() override
{
// if the module is asked to stop ask the asynchronous thread to stop
input_events.stop();
mn_handler.close();
camera_handler_thread.join();
hpe_thread.join();
int r = system("killall python3");
return true;
}
bool close() override
{
// when the asynchronous thread is asked to stop, close ports and do other clean up
return true;
}
void drawEROS(cv::Mat img)
{
cv::Mat eros8;
eros_handler.getSurface().convertTo(eros8, CV_8U);
cv::GaussianBlur(eros8, eros8, {9, 9}, 0);
cv::normalize(eros8, eros8, 0, 255, cv::NORM_MINMAX);
cv::cvtColor(eros8, img, cv::COLOR_GRAY2BGR);
}
void drawEVENTS(cv::Mat &img)
{
cv::Mat eventsmono;
binary_handler.getSurface().convertTo(eventsmono, CV_8U);
cv::cvtColor(eventsmono, img, CV_GRAY2BGR);
}
void drawSAE(cv::Mat &img)
{
cv::Mat sae64, saemono;
sae_handler.getSurface().copyTo(sae64);
double maxval;
cv::minMaxLoc(sae64, nullptr, &maxval);
sae64 -= (maxval - 1.0); //show 2 seconds of surface
//cv::threshold(sae64, sae64, 0, 0, cv::THRESH_BINARY);
sae64.convertTo(saemono, CV_8U, 255.0);
cv::cvtColor(saemono, img, CV_GRAY2BGR);
}
void drawROI(cv::Mat img)
{
for(int i=0; i<13 ; i++)
{
float cx = state.query()[i].u;
float cy = state.query()[i].v;
cv::Point2d p1(cx-roiSize, cy-roiSize);
cv::Point2d p2(cx+roiSize, cy+roiSize);
cv::rectangle(img, cv::Rect(p1, p2), colors[i], 1);
}
}
// synchronous thread
bool updateModule() override
{
//yInfo() << (int)(counter/th_period) << "Hz";
counter = 0;
static cv::Mat canvas = cv::Mat(image_size, CV_8UC3);
canvas.setTo(cv::Vec3b(0, 0, 0));
// plot the image
// check if we plot events or alternative (PIM or EROS)
if (alt_view == 0)
drawEROS(canvas);
else if(alt_view == 1)// events
drawEVENTS(canvas);
else if(alt_view == 2)
drawSAE(canvas);
static yarp::os::Stamp ystamp;
ystamp.update();
// publish images using ROS
// EROS
static cv::Mat cvEROS = cv::Mat(image_size, CV_8UC3);
cvEROS.setTo(cv::Vec3b(0, 0, 0));
drawEROS(cvEROS);
auto yarpEROS = yarp::cv::fromCvMat<yarp::sig::PixelRgb>(cvEROS);
yarp::rosmsg::sensor_msgs::Image& rosEROS = publisherPort_eros.prepare();
rosEROS.data.resize(yarpEROS.getRawImageSize());
rosEROS.width = yarpEROS.width();
rosEROS.height = yarpEROS.height();
rosEROS.encoding = "bgr8";//yarp::dev::ROSPixelCode::yarp2RosPixelCode(yarpEROS.getPixelCode());
rosEROS.step = yarpEROS.getRowSize();
rosEROS.is_bigendian = 0;
rosEROS.header.frame_id = "eros";
rosEROS.header.seq = ystamp.getCount();
rosEROS.header.stamp = ystamp.getTime();
memcpy(rosEROS.data.data(), yarpEROS.getRawImage(), yarpEROS.getRawImageSize());
publisherPort_eros.setEnvelope(ystamp);
publisherPort_eros.write();
// EV image
static cv::Mat cvEVS;
drawEVENTS(cvEVS);
auto yarpEVS = yarp::cv::fromCvMat<yarp::sig::PixelRgb>(cvEVS);
yarp::rosmsg::sensor_msgs::Image& rosEVS = publisherPort_evs.prepare();
rosEVS.data.resize(yarpEVS.getRawImageSize());
rosEVS.width = yarpEVS.width();
rosEVS.height = yarpEVS.height();
rosEVS.encoding = "bgr8";//yarp::dev::ROSPixelCode::yarp2RosPixelCode(yarpEVS.getPixelCode());
rosEVS.step = yarpEVS.getRowSize();
rosEVS.is_bigendian = 0;
rosEVS.header.frame_id = "eventimage";
rosEVS.header.seq = ystamp.getCount();
rosEVS.header.stamp = ystamp.getTime();
memcpy(rosEVS.data.data(), yarpEVS.getRawImage(), yarpEVS.getRawImageSize());
publisherPort_evs.setEnvelope(ystamp);
publisherPort_evs.write();
binary_handler.getSurface().setTo(0.0);
// plot skeletons
hpecore::stampedPose pose_copy = detected_pose;
hpecore::drawSkeleton(canvas, pose_copy, {255, 0, 0}, 3, c_thresh);
hpecore::drawVel(canvas, pose_copy, state.queryDP(), {255, 255, 102}, 2, c_thresh);
pose_copy.pose = state.query();
hpecore::drawSkeleton(canvas, pose_copy, {0, 0, 255}, 3, c_thresh);
if (!edpr_logo.empty())
{
static cv::Mat mask;
cv::cvtColor(edpr_logo, mask, CV_BGR2GRAY);
edpr_logo.copyTo(canvas, mask);
}
std::stringstream ss;
ss << std::fixed << std::setprecision(1) << scaler;
std::string mystring = ss.str();
cv::imshow("edpr-april", canvas);
char key_pressed = cv::waitKey(10);
if (key_pressed > 0)
{
switch (key_pressed)
{
case 'v':
pltVel = !pltVel;
break;
case 'd':
pltDet = !pltDet;
break;
case 'e':
++alt_view %= 3;
break;
case 't':
pltTra = !pltTra;
break;
case 'r':
pltRoi = !pltRoi;
break;
case '[':
if(scaler>0)
scaler-=0.5;
break;
case ']':
if(scaler<30)
scaler+=0.5;
break;
case '\e':
stopModule();
break;
}
}
return true;
}
void run_camera_interface()
{
while (!isStopping())
{
ev::info stats = input_events.readAll(true);
tnow = stats.timestamp;
//check a reset
static double pts = 0;
if(tnow < pts) {
sae_handler.getSurface().setTo(0.0);
binary_handler.getSurface().setTo(0.0);
eros_handler.getSurface().setTo(0.0);
state.reset();
velocity_estimator.prev_update_ts = 0;
}
pts = tnow;
//do the update of surfaces
for(auto &v : input_events) {
eros_handler.update(v.x, v.y);
binary_handler.update(v.x, v.y);
sae_handler.update(v.x, v.y, tnow);
}
}
}
void run_hpe()
{
while(!isStopping()) {
//detection
bool was_detected = mn_handler.update(eros_handler.getSurface(), tnow, detected_pose);
if (was_detected && hpecore::poseNonZero(detected_pose.pose))
{
if (state.poseIsInitialised())
state.updateFromPosition(detected_pose.pose, detected_pose.timestamp);
else
state.set(detected_pose.pose, tnow);
}
if (!state.poseIsInitialised())
continue;
//velocity
auto jvs = velocity_estimator.multi_area_velocity(sae_handler.getSurface(), tnow, state.query(), roiSize);
state.setVelocity(jvs);
state.updateFromVelocity(jvs, tnow);
counter++;
yarp::os::Time::delay(0.0005);
//send to ros as fast as possible
static double timer = yarp::os::Time::now();
double dt = yarp::os::Time::now() - timer;
if (dt > 0.01) {
timer += dt;
auto &ros_output = ros_publisher.prepare();
hpecore::skeleton13 pos = state.query();
hpecore::skeleton13 vel = state.queryVelocity();
ros_output.pose.resize(pos.size()*2);
ros_output.velocity.resize(vel.size()*2);
ros_output.confidence.resize(detected_pose.conf.size());
for (int j = 0; j < pos.size(); j++) {
ros_output.pose[j * 2] = pos[j].u;
ros_output.pose[j * 2 + 1] = pos[j].v;
ros_output.velocity[j * 2] = vel[j].u;
ros_output.velocity[j * 2 + 1] = vel[j].v;
ros_output.confidence[j] = detected_pose.conf[j];
}
ros_output.timestamp = tnow;
ros_publisher.write();
}
}
}
};
int main(int argc, char *argv[])
{
/* prepare and configure the resource finder */
yarp::os::ResourceFinder rf;
rf.setVerbose(false);
rf.configure(argc, argv);
/* create the module */
APRIL_HPE instance;
return instance.runModule(rf);
}