-
Notifications
You must be signed in to change notification settings - Fork 200
/
leg_detector.cpp
1006 lines (868 loc) · 31 KB
/
leg_detector.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
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
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*********************************************************************
* Software License Agreement (BSD License)
*
* Copyright (c) 2008, Willow Garage, Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided
* with the distribution.
* * Neither the name of the Willow Garage nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*********************************************************************/
#include <ros/ros.h>
#include <leg_detector/LegDetectorConfig.h>
#include <leg_detector/laser_processor.h>
#include <leg_detector/calc_leg_features.h>
#include <opencv/cxcore.h>
#include <opencv/cv.h>
#include <opencv/ml.h>
#include <people_msgs/PositionMeasurement.h>
#include <people_msgs/PositionMeasurementArray.h>
#include <sensor_msgs/LaserScan.h>
#include <tf/transform_listener.h>
#include <tf/message_filter.h>
#include <message_filters/subscriber.h>
#include <people_tracking_filter/tracker_kalman.h>
#include <people_tracking_filter/state_pos_vel.h>
#include <people_tracking_filter/rgb.h>
#include <visualization_msgs/Marker.h>
#include <dynamic_reconfigure/server.h>
#include <algorithm>
#include <list>
#include <set>
#include <string>
#include <vector>
static double no_observation_timeout_s = 0.5;
static double max_second_leg_age_s = 2.0;
static double max_track_jump_m = 1.0;
static double max_meas_jump_m = 0.75; // 1.0
static double leg_pair_separation_m = 1.0;
static std::string fixed_frame = "odom_combined";
static double kal_p = 4, kal_q = .002, kal_r = 10;
static bool use_filter = true;
class SavedFeature
{
public:
static int nextid;
tf::TransformListener& tfl_;
BFL::StatePosVel sys_sigma_;
estimation::TrackerKalman filter_;
std::string id_;
std::string object_id;
ros::Time time_;
ros::Time meas_time_;
double reliability, p;
tf::Stamped<tf::Point> position_;
SavedFeature* other;
float dist_to_person_;
// one leg tracker
SavedFeature(tf::Stamped<tf::Point> loc, tf::TransformListener& tfl)
: tfl_(tfl),
sys_sigma_(tf::Vector3(0.05, 0.05, 0.05), tf::Vector3(1.0, 1.0, 1.0)),
filter_("tracker_name", sys_sigma_),
reliability(-1.), p(4)
{
char id[100];
snprintf(id, sizeof(id), "legtrack%d", nextid++);
id_ = std::string(id);
object_id = "";
time_ = loc.stamp_;
meas_time_ = loc.stamp_;
other = NULL;
try
{
tfl_.transformPoint(fixed_frame, loc, loc);
}
catch (...)
{
ROS_WARN("TF exception spot 6.");
}
tf::StampedTransform pose(tf::Pose(tf::Quaternion(0.0, 0.0, 0.0, 1.0), loc), loc.stamp_, id_, loc.frame_id_);
tfl_.setTransform(pose);
BFL::StatePosVel prior_sigma(tf::Vector3(0.1, 0.1, 0.1), tf::Vector3(0.0000001, 0.0000001, 0.0000001));
filter_.initialize(loc, prior_sigma, time_.toSec());
BFL::StatePosVel est;
filter_.getEstimate(est);
updatePosition();
}
void propagate(ros::Time time)
{
time_ = time;
filter_.updatePrediction(time.toSec());
updatePosition();
}
void update(tf::Stamped<tf::Point> loc, double probability)
{
tf::StampedTransform pose(tf::Pose(tf::Quaternion(0.0, 0.0, 0.0, 1.0), loc), loc.stamp_, id_, loc.frame_id_);
tfl_.setTransform(pose);
meas_time_ = loc.stamp_;
time_ = meas_time_;
MatrixWrapper::SymmetricMatrix cov(3);
cov = 0.0;
cov(1, 1) = 0.0025;
cov(2, 2) = 0.0025;
cov(3, 3) = 0.0025;
filter_.updateCorrection(loc, cov);
updatePosition();
if (reliability < 0 || !use_filter)
{
reliability = probability;
p = kal_p;
}
else
{
p += kal_q;
double k = p / (p + kal_r);
reliability += k * (probability - reliability);
p *= (1 - k);
}
}
double getLifetime()
{
return filter_.getLifetime();
}
double getReliability()
{
return reliability;
}
private:
void updatePosition()
{
BFL::StatePosVel est;
filter_.getEstimate(est);
position_[0] = est.pos_[0];
position_[1] = est.pos_[1];
position_[2] = est.pos_[2];
position_.stamp_ = time_;
position_.frame_id_ = fixed_frame;
double nreliability = fmin(1.0, fmax(0.1, est.vel_.length() / 0.5));
// reliability = fmax(reliability, nreliability);
}
};
int SavedFeature::nextid = 0;
class MatchedFeature
{
public:
laser_processor::SampleSet* candidate_;
SavedFeature* closest_;
float distance_;
double probability_;
MatchedFeature(laser_processor::SampleSet* candidate, SavedFeature* closest, float distance, double probability)
: candidate_(candidate)
, closest_(closest)
, distance_(distance)
, probability_(probability)
{}
inline bool operator< (const MatchedFeature& b) const
{
return (distance_ < b.distance_);
}
};
int g_argc;
char** g_argv;
// actual legdetector node
class LegDetector
{
public:
ros::NodeHandle nh_;
tf::TransformListener tfl_;
laser_processor::ScanMask mask_;
int mask_count_;
// cv::ml::RTrees forest;
cv::Ptr<cv::ml::RTrees> forest;
float connected_thresh_;
int feat_count_;
char save_[100];
std::list<SavedFeature*> saved_features_;
boost::mutex saved_mutex_;
int feature_id_;
bool use_seeds_;
bool publish_legs_, publish_people_, publish_leg_markers_, publish_people_markers_;
int next_p_id_;
double leg_reliability_limit_;
int min_points_per_group;
ros::Publisher people_measurements_pub_;
ros::Publisher leg_measurements_pub_;
ros::Publisher markers_pub_;
dynamic_reconfigure::Server<leg_detector::LegDetectorConfig> server_;
message_filters::Subscriber<people_msgs::PositionMeasurement> people_sub_;
message_filters::Subscriber<sensor_msgs::LaserScan> laser_sub_;
tf::MessageFilter<people_msgs::PositionMeasurement> people_notifier_;
tf::MessageFilter<sensor_msgs::LaserScan> laser_notifier_;
explicit LegDetector(ros::NodeHandle nh) :
nh_(nh),
mask_count_(0),
feat_count_(0),
next_p_id_(0),
people_sub_(nh_, "people_tracker_filter", 10),
laser_sub_(nh_, "scan", 10),
people_notifier_(people_sub_, tfl_, fixed_frame, 10),
laser_notifier_(laser_sub_, tfl_, fixed_frame, 10)
{
if (g_argc > 1)
{
forest = cv::ml::RTrees::create();
cv::String feature_file = cv::String(g_argv[1]);
forest = cv::ml::StatModel::load<cv::ml::RTrees>(feature_file);
feat_count_ = forest->getVarCount();
printf("Loaded forest with %d features: %s\n", feat_count_, g_argv[1]);
}
else
{
printf("Please provide a trained random forests classifier as an input.\n");
ros::shutdown();
}
nh_.param<bool>("use_seeds", use_seeds_, !true);
// advertise topics
leg_measurements_pub_ = nh_.advertise<people_msgs::PositionMeasurementArray>("leg_tracker_measurements", 0);
people_measurements_pub_ = nh_.advertise<people_msgs::PositionMeasurementArray>("people_tracker_measurements", 0);
markers_pub_ = nh_.advertise<visualization_msgs::Marker>("visualization_marker", 20);
if (use_seeds_)
{
people_notifier_.registerCallback(boost::bind(&LegDetector::peopleCallback, this, _1));
people_notifier_.setTolerance(ros::Duration(0.01));
}
laser_notifier_.registerCallback(boost::bind(&LegDetector::laserCallback, this, _1));
laser_notifier_.setTolerance(ros::Duration(0.01));
dynamic_reconfigure::Server<leg_detector::LegDetectorConfig>::CallbackType f;
f = boost::bind(&LegDetector::configure, this, _1, _2);
server_.setCallback(f);
feature_id_ = 0;
}
~LegDetector()
{
}
void configure(leg_detector::LegDetectorConfig &config, uint32_t level)
{
connected_thresh_ = config.connection_threshold;
min_points_per_group = config.min_points_per_group;
leg_reliability_limit_ = config.leg_reliability_limit;
publish_legs_ = config.publish_legs;
publish_people_ = config.publish_people;
publish_leg_markers_ = config.publish_leg_markers;
publish_people_markers_ = config.publish_people_markers;
no_observation_timeout_s = config.no_observation_timeout;
max_second_leg_age_s = config.max_second_leg_age;
max_track_jump_m = config.max_track_jump;
max_meas_jump_m = config.max_meas_jump;
leg_pair_separation_m = config.leg_pair_separation;
if (fixed_frame.compare(config.fixed_frame) != 0)
{
fixed_frame = config.fixed_frame;
laser_notifier_.setTargetFrame(fixed_frame);
people_notifier_.setTargetFrame(fixed_frame);
}
kal_p = config.kalman_p;
kal_q = config.kalman_q;
kal_r = config.kalman_r;
use_filter = config.kalman_on == 1;
}
double distance(std::list<SavedFeature*>::iterator it1, std::list<SavedFeature*>::iterator it2)
{
tf::Stamped<tf::Point> one = (*it1)->position_, two = (*it2)->position_;
double dx = one[0] - two[0], dy = one[1] - two[1], dz = one[2] - two[2];
return sqrt(dx * dx + dy * dy + dz * dz);
}
// Find the tracker that is closest to this person message
// If a tracker was already assigned to a person,
// keep this assignment when the distance between them is not too large.
void peopleCallback(const people_msgs::PositionMeasurement::ConstPtr& people_meas)
{
// If there are no legs, return.
if (saved_features_.empty())
return;
tf::Point pt;
pointMsgToTF(people_meas->pos, pt);
tf::Stamped<tf::Point> person_loc(pt, people_meas->header.stamp, people_meas->header.frame_id);
person_loc[2] = 0.0; // Ignore the height of the person measurement.
// Holder for all transformed pts.
tf::Stamped<tf::Point> dest_loc(pt, people_meas->header.stamp, people_meas->header.frame_id);
boost::mutex::scoped_lock lock(saved_mutex_);
std::list<SavedFeature*>::iterator closest = saved_features_.end();
std::list<SavedFeature*>::iterator closest1 = saved_features_.end();
std::list<SavedFeature*>::iterator closest2 = saved_features_.end();
float closest_dist = max_meas_jump_m;
float closest_pair_dist = 2 * max_meas_jump_m;
std::list<SavedFeature*>::iterator begin = saved_features_.begin();
std::list<SavedFeature*>::iterator end = saved_features_.end();
std::list<SavedFeature*>::iterator it1, it2;
// If there's a pair of legs with the right label and within the max dist, return
// If there's one leg with the right label and within the max dist,
// find a partner for it from the unlabeled legs whose tracks are reasonably new.
// If no partners exist, label just the one leg.
// If there are no legs with the right label and within the max dist,
// find a pair of unlabeled legs and assign them the label.
// If all of the above cases fail,
// find a new unlabeled leg and assign the label.
// For each tracker, get the distance to this person.
for (it1 = begin; it1 != end; ++it1)
{
try
{
tfl_.transformPoint((*it1)->id_, people_meas->header.stamp,
person_loc, fixed_frame, dest_loc);
// ROS_INFO("Succesful leg transformation at spot 7");
}
catch (...)
{
ROS_WARN("TF exception spot 7.");
}
(*it1)->dist_to_person_ = dest_loc.length();
}
// Try to find one or two trackers with the same label and within the max distance of the person.
std::cout << "Looking for two legs" << std::endl;
it2 = end;
for (it1 = begin; it1 != end; ++it1)
{
// If this leg belongs to the person...
if ((*it1)->object_id == people_meas->object_id)
{
// and their distance is close enough...
if ((*it1)->dist_to_person_ < max_meas_jump_m)
{
// if this is the first leg we've found, assign it to it2. Otherwise, leave it assigned to it1 and break.
if (it2 == end)
it2 = it1;
else
break;
}
// Otherwise, remove the tracker's label, it doesn't belong to this person.
else
{
// the two trackers moved apart. This should not happen.
(*it1)->object_id = "";
}
}
}
// If we found two legs with the right label and within the max distance, all is good, return.
if (it1 != end && it2 != end)
{
std::cout << "Found matching pair. The second distance was " << (*it1)->dist_to_person_ << std::endl;
return;
}
// If we only found one close leg with the right label, let's try to find a second leg that
// * doesn't yet have a label (=valid precondition),
// * is within the max distance,
// * is less than max_second_leg_age_s old.
std::cout << "Looking for one leg plus one new leg" << std::endl;
float dist_between_legs, closest_dist_between_legs;
if (it2 != end)
{
closest_dist = max_meas_jump_m;
closest = saved_features_.end();
for (it1 = begin; it1 != end; ++it1)
{
// Skip this leg track if:
// - you're already using it.
// - it already has an id.
// - it's too old. Old unassigned trackers are unlikely to be the second leg in a pair.
// - it's too far away from the person.
if ((it1 == it2) || ((*it1)->object_id != "") || ((*it1)->getLifetime() > max_second_leg_age_s) ||
((*it1)->dist_to_person_ >= closest_dist))
continue;
// Get the distance between the two legs
try
{
tfl_.transformPoint((*it1)->id_, (*it2)->position_.stamp_, (*it2)->position_, fixed_frame, dest_loc);
}
catch (...)
{
ROS_WARN("TF exception getting distance between legs.");
}
dist_between_legs = dest_loc.length();
// If this is the closest dist (and within range), and the legs are close together and unlabeled, mark it.
if (dist_between_legs < leg_pair_separation_m)
{
closest = it1;
closest_dist = (*it1)->dist_to_person_;
closest_dist_between_legs = dist_between_legs;
}
}
// If we found a close, unlabeled leg, set it's label.
if (closest != end)
{
std::cout << "Replaced one leg with a distance of " << closest_dist
<< " and a distance between the legs of " << closest_dist_between_legs << std::endl;
(*closest)->object_id = people_meas->object_id;
}
else
{
std::cout << "Returned one matched leg only" << std::endl;
}
// Regardless of whether we found a second leg, return.
return;
}
std::cout << "Looking for a pair of new legs" << std::endl;
// If we didn't find any legs with this person's label,
// try to find two unlabeled legs that are close together and close to the tracker.
it1 = saved_features_.begin();
it2 = saved_features_.begin();
closest = saved_features_.end();
closest1 = saved_features_.end();
closest2 = saved_features_.end();
closest_dist = max_meas_jump_m;
closest_pair_dist = 2 * max_meas_jump_m;
for (; it1 != end; ++it1)
{
// Only look at trackers without ids and that are not too far away.
if ((*it1)->object_id != "" || (*it1)->dist_to_person_ >= max_meas_jump_m)
continue;
// Keep the single closest leg around in case none of the pairs work out.
if ((*it1)->dist_to_person_ < closest_dist)
{
closest_dist = (*it1)->dist_to_person_;
closest = it1;
}
// Find a second leg.
it2 = it1;
it2++;
for (; it2 != end; ++it2)
{
// Only look at trackers without ids and that are not too far away.
if ((*it2)->object_id != "" || (*it2)->dist_to_person_ >= max_meas_jump_m)
continue;
// Get the distance between the two legs
try
{
tfl_.transformPoint((*it1)->id_, (*it2)->position_.stamp_, (*it2)->position_, fixed_frame, dest_loc);
}
catch (...)
{
ROS_WARN("TF exception getting distance between legs in spot 2.");
}
dist_between_legs = dest_loc.length();
// Ensure that this pair of legs is the closest pair to the tracker,
// and that the distance between the legs isn't too large.
if ((*it1)->dist_to_person_ + (*it2)->dist_to_person_ < closest_pair_dist &&
dist_between_legs < leg_pair_separation_m)
{
closest_pair_dist = (*it1)->dist_to_person_ + (*it2)->dist_to_person_;
closest1 = it1;
closest2 = it2;
closest_dist_between_legs = dist_between_legs;
}
}
}
// Found a pair of legs.
if (closest1 != end && closest2 != end)
{
(*closest1)->object_id = people_meas->object_id;
(*closest2)->object_id = people_meas->object_id;
std::cout << "Found a completely new pair with total distance " << closest_pair_dist
<< " and a distance between the legs of " << closest_dist_between_legs << std::endl;
return;
}
std::cout << "Looking for just one leg" << std::endl;
// No pair worked, try for just one leg.
if (closest != end)
{
(*closest)->object_id = people_meas->object_id;
std::cout << "Returned one new leg only" << std::endl;
return;
}
std::cout << "Nothing matched" << std::endl;
}
void pairLegs()
{
// Deal With legs that already have ids
std::list<SavedFeature*>::iterator begin = saved_features_.begin();
std::list<SavedFeature*>::iterator end = saved_features_.end();
std::list<SavedFeature*>::iterator leg1, leg2, best, it;
for (leg1 = begin; leg1 != end; ++leg1)
{
// If this leg has no id, skip
if ((*leg1)->object_id == "")
continue;
leg2 = end;
best = end;
double closest_dist = leg_pair_separation_m;
for (it = begin; it != end; ++it)
{
if (it == leg1) continue;
if ((*it)->object_id == (*leg1)->object_id)
{
leg2 = it;
break;
}
if ((*it)->object_id != "")
continue;
double d = distance(it, leg1);
if (((*it)->getLifetime() <= max_second_leg_age_s)
&& (d < closest_dist))
{
closest_dist = d;
best = it;
}
}
if (leg2 != end)
{
double dist_between_legs = distance(leg1, leg2);
if (dist_between_legs > leg_pair_separation_m)
{
(*leg1)->object_id = "";
(*leg1)->other = NULL;
(*leg2)->object_id = "";
(*leg2)->other = NULL;
}
else
{
(*leg1)->other = *leg2;
(*leg2)->other = *leg1;
}
}
else if (best != end)
{
(*best)->object_id = (*leg1)->object_id;
(*leg1)->other = *best;
(*best)->other = *leg1;
}
}
// Attempt to pair up legs with no id
for (;;)
{
std::list<SavedFeature*>::iterator best1 = end, best2 = end;
double closest_dist = leg_pair_separation_m;
for (leg1 = begin; leg1 != end; ++leg1)
{
// If this leg has an id or low reliability, skip
if ((*leg1)->object_id != ""
|| (*leg1)->getReliability() < leg_reliability_limit_)
continue;
for (leg2 = begin; leg2 != end; ++leg2)
{
if (((*leg2)->object_id != "")
|| ((*leg2)->getReliability() < leg_reliability_limit_)
|| (leg1 == leg2)) continue;
double d = distance(leg1, leg2);
if (d < closest_dist)
{
best1 = leg1;
best2 = leg2;
}
}
}
if (best1 != end)
{
char id[100];
snprintf(id, sizeof(id), "Person%d", next_p_id_++);
(*best1)->object_id = std::string(id);
(*best2)->object_id = std::string(id);
(*best1)->other = *best2;
(*best2)->other = *best1;
}
else
{
break;
}
}
}
void laserCallback(const sensor_msgs::LaserScan::ConstPtr& scan)
{
laser_processor::ScanProcessor processor(*scan, mask_);
processor.splitConnected(connected_thresh_);
processor.removeLessThan(5);
cv::Mat tmp_mat = cv::Mat(1, feat_count_, CV_32FC1);
// if no measurement matches to a tracker in the last <no_observation_timeout> seconds: erase tracker
ros::Time purge = scan->header.stamp + ros::Duration().fromSec(-no_observation_timeout_s);
std::list<SavedFeature*>::iterator sf_iter = saved_features_.begin();
while (sf_iter != saved_features_.end())
{
if ((*sf_iter)->meas_time_ < purge)
{
if ((*sf_iter)->other)
(*sf_iter)->other->other = NULL;
delete *sf_iter;
saved_features_.erase(sf_iter++);
}
else
++sf_iter;
}
// System update of trackers, and copy updated ones in propagate list
std::list<SavedFeature*> propagated;
for (std::list<SavedFeature*>::iterator sf_iter = saved_features_.begin();
sf_iter != saved_features_.end();
sf_iter++)
{
(*sf_iter)->propagate(scan->header.stamp);
propagated.push_back(*sf_iter);
}
// Detection step: build up the set of "candidate" clusters
// For each candidate, find the closest tracker (within threshold) and add to the match list
// If no tracker is found, start a new one
std::multiset<MatchedFeature> matches;
for (std::list<laser_processor::SampleSet*>::iterator i = processor.getClusters().begin();
i != processor.getClusters().end();
i++)
{
std::vector<float> f = calcLegFeatures(*i, *scan);
memcpy(tmp_mat.data, f.data(), f.size()*sizeof(float));
float probability = 0.5 -
forest->predict(tmp_mat, cv::noArray(), cv::ml::RTrees::PREDICT_SUM) /
forest->getRoots().size();
tf::Stamped<tf::Point> loc((*i)->center(), scan->header.stamp, scan->header.frame_id);
try
{
tfl_.transformPoint(fixed_frame, loc, loc);
}
catch (...)
{
ROS_WARN("TF exception spot 3.");
}
std::list<SavedFeature*>::iterator closest = propagated.end();
float closest_dist = max_track_jump_m;
for (std::list<SavedFeature*>::iterator pf_iter = propagated.begin();
pf_iter != propagated.end();
pf_iter++)
{
// find the closest distance between candidate and trackers
float dist = loc.distance((*pf_iter)->position_);
if (dist < closest_dist)
{
closest = pf_iter;
closest_dist = dist;
}
}
// Nothing close to it, start a new track
if (closest == propagated.end())
{
std::list<SavedFeature*>::iterator new_saved =
saved_features_.insert(saved_features_.end(), new SavedFeature(loc, tfl_));
}
// Add the candidate, the tracker and the distance to a match list
else
matches.insert(MatchedFeature(*i, *closest, closest_dist, probability));
}
// loop through _sorted_ matches list
// find the match with the shortest distance for each tracker
while (matches.size() > 0)
{
std::multiset<MatchedFeature>::iterator matched_iter = matches.begin();
bool found = false;
std::list<SavedFeature*>::iterator pf_iter = propagated.begin();
while (pf_iter != propagated.end())
{
// update the tracker with this candidate
if (matched_iter->closest_ == *pf_iter)
{
// Transform candidate to fixed frame
tf::Stamped<tf::Point> loc(matched_iter->candidate_->center(), scan->header.stamp, scan->header.frame_id);
try
{
tfl_.transformPoint(fixed_frame, loc, loc);
}
catch (...)
{
ROS_WARN("TF exception spot 4.");
}
// Update the tracker with the candidate location
matched_iter->closest_->update(loc, matched_iter->probability_);
// remove this match and
matches.erase(matched_iter);
propagated.erase(pf_iter++);
found = true;
break;
}
// still looking for the tracker to update
else
{
pf_iter++;
}
}
// didn't find tracker to update, because it was deleted above
// try to assign the candidate to another tracker
if (!found)
{
tf::Stamped<tf::Point> loc(matched_iter->candidate_->center(), scan->header.stamp, scan->header.frame_id);
try
{
tfl_.transformPoint(fixed_frame, loc, loc);
}
catch (...)
{
ROS_WARN("TF exception spot 5.");
}
std::list<SavedFeature*>::iterator closest = propagated.end();
float closest_dist = max_track_jump_m;
for (std::list<SavedFeature*>::iterator remain_iter = propagated.begin();
remain_iter != propagated.end();
remain_iter++)
{
float dist = loc.distance((*remain_iter)->position_);
if (dist < closest_dist)
{
closest = remain_iter;
closest_dist = dist;
}
}
// no tracker is within a threshold of this candidate
// so create a new tracker for this candidate
if (closest == propagated.end())
std::list<SavedFeature*>::iterator new_saved =
saved_features_.insert(saved_features_.end(), new SavedFeature(loc, tfl_));
else
matches.insert(MatchedFeature(matched_iter->candidate_, *closest, closest_dist, matched_iter->probability_));
matches.erase(matched_iter);
}
}
if (!use_seeds_)
pairLegs();
// Publish Data!
int i = 0;
std::vector<people_msgs::PositionMeasurement> people;
std::vector<people_msgs::PositionMeasurement> legs;
for (std::list<SavedFeature*>::iterator sf_iter = saved_features_.begin();
sf_iter != saved_features_.end();
sf_iter++, i++)
{
// reliability
double reliability = (*sf_iter)->getReliability();
if ((*sf_iter)->getReliability() > leg_reliability_limit_
&& publish_legs_)
{
people_msgs::PositionMeasurement pos;
pos.header.stamp = scan->header.stamp;
pos.header.frame_id = fixed_frame;
pos.name = "leg_detector";
pos.object_id = (*sf_iter)->id_;
pos.pos.x = (*sf_iter)->position_[0];
pos.pos.y = (*sf_iter)->position_[1];
pos.pos.z = (*sf_iter)->position_[2];
pos.reliability = reliability;
pos.covariance[0] = pow(0.3 / reliability, 2.0);
pos.covariance[1] = 0.0;
pos.covariance[2] = 0.0;
pos.covariance[3] = 0.0;
pos.covariance[4] = pow(0.3 / reliability, 2.0);
pos.covariance[5] = 0.0;
pos.covariance[6] = 0.0;
pos.covariance[7] = 0.0;
pos.covariance[8] = 10000.0;
pos.initialization = 0;
legs.push_back(pos);
}
if (publish_leg_markers_)
{
visualization_msgs::Marker m;
m.header.stamp = (*sf_iter)->time_;
m.header.frame_id = fixed_frame;
m.ns = "LEGS";
m.id = i;
m.type = m.SPHERE;
m.pose.position.x = (*sf_iter)->position_[0];
m.pose.position.y = (*sf_iter)->position_[1];
m.pose.position.z = (*sf_iter)->position_[2];
m.scale.x = .1;
m.scale.y = .1;
m.scale.z = .1;
m.color.a = 1;
m.lifetime = ros::Duration(0.5);
if ((*sf_iter)->object_id != "")
{
m.color.r = 1;
}
else
{
m.color.b = (*sf_iter)->getReliability();
}
markers_pub_.publish(m);
}
if (publish_people_ || publish_people_markers_)
{
SavedFeature* other = (*sf_iter)->other;
if (other != NULL && other < (*sf_iter))
{
double dx = ((*sf_iter)->position_[0] + other->position_[0]) / 2,
dy = ((*sf_iter)->position_[1] + other->position_[1]) / 2,
dz = ((*sf_iter)->position_[2] + other->position_[2]) / 2;
if (publish_people_)
{
reliability = reliability * other->reliability;
people_msgs::PositionMeasurement pos;
pos.header.stamp = (*sf_iter)->time_;
pos.header.frame_id = fixed_frame;
pos.name = (*sf_iter)->object_id;;
pos.object_id = (*sf_iter)->id_ + "|" + other->id_;
pos.pos.x = dx;
pos.pos.y = dy;
pos.pos.z = dz;
pos.reliability = reliability;
pos.covariance[0] = pow(0.3 / reliability, 2.0);
pos.covariance[1] = 0.0;
pos.covariance[2] = 0.0;
pos.covariance[3] = 0.0;
pos.covariance[4] = pow(0.3 / reliability, 2.0);
pos.covariance[5] = 0.0;
pos.covariance[6] = 0.0;
pos.covariance[7] = 0.0;
pos.covariance[8] = 10000.0;
pos.initialization = 0;
people.push_back(pos);
}
if (publish_people_markers_)
{
visualization_msgs::Marker m;
m.header.stamp = (*sf_iter)->time_;
m.header.frame_id = fixed_frame;
m.ns = "PEOPLE";
m.id = i;
m.type = m.SPHERE;
m.pose.position.x = dx;
m.pose.position.y = dy;
m.pose.position.z = dz;
m.scale.x = .2;
m.scale.y = .2;
m.scale.z = .2;
m.color.a = 1;
m.color.g = 1;
m.lifetime = ros::Duration(0.5);
markers_pub_.publish(m);
}
}
}
}
people_msgs::PositionMeasurementArray array;
array.header.stamp = ros::Time::now();
if (publish_legs_)
{
array.people = legs;
leg_measurements_pub_.publish(array);
}
if (publish_people_)
{
array.people = people;
people_measurements_pub_.publish(array);
}
}
};
int main(int argc, char **argv)
{
ros::init(argc, argv, "leg_detector");
g_argc = argc;
g_argv = argv;