Apollo 10.0
自动驾驶开放平台
select.cc
浏览该文件的文档.
1/******************************************************************************
2 * Copyright 2018 The Apollo Authors. All Rights Reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the License);
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an AS IS BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *****************************************************************************/
17
18#include "cyber/common/log.h"
19
20namespace apollo {
21namespace perception {
22namespace trafficlight {
23
24bool Select::Init(int rows, int cols) {
25 if (rows < 0 || cols < 0) {
26 return false;
27 }
28
29 munkres_.costs()->Reserve(rows, cols);
30
31 return true;
32}
33
35 float sigma1, float sigma2) {
36 return std::exp(-0.5 * (static_cast<float>((p1.x - p2.x) * (p1.x - p2.x)) /
37 (sigma1 * sigma1) +
38 (static_cast<float>((p1.y - p2.y) * (p1.y - p2.y)) /
39 (sigma2 * sigma2))));
40}
41
43 const std::vector<base::TrafficLightPtr> &refined_bboxes,
44 std::vector<base::TrafficLightPtr> *hdmap_bboxes) {
45 std::vector<std::pair<size_t, size_t>> assignments;
46 munkres_.costs()->Resize(hdmap_bboxes->size(), refined_bboxes.size());
47
48 for (size_t row = 0; row < hdmap_bboxes->size(); ++row) {
49 auto center_hd = (*hdmap_bboxes)[row]->region.detection_roi.Center();
50 if ((*hdmap_bboxes)[row]->region.outside_image) {
51 AINFO << "projection_roi outside image, set score to 0.";
52 for (size_t col = 0; col < refined_bboxes.size(); ++col) {
53 (*munkres_.costs())(row, col) = 0.0;
54 }
55 continue;
56 }
57 for (size_t col = 0; col < refined_bboxes.size(); ++col) {
58 float gaussian_score = 100.0f;
59 auto center_refine = refined_bboxes[col]->region.detection_roi.Center();
60 // use gaussian score as metrics of distance and width
61 double distance_score = Calc2dGaussianScore(
62 center_hd, center_refine, gaussian_score, gaussian_score);
63
64 double max_score = 0.9;
65 auto detect_score = refined_bboxes[col]->region.detect_score;
66 double detection_score =
67 detect_score > max_score ? max_score : detect_score;
68
69 double distance_weight = 0.7;
70 double detection_weight = 1 - distance_weight;
71 (*munkres_.costs())(row, col) =
72 static_cast<float>(detection_weight * detection_score +
73 distance_weight * distance_score);
74 const auto &crop_roi = (*hdmap_bboxes)[row]->region.crop_roi;
75 const auto &detection_roi = refined_bboxes[col]->region.detection_roi;
76 if ((detection_roi & crop_roi) != detection_roi) {
77 AINFO << "detection_roi outside crop_roi, set score to 0."
78 << " detection_roi: " << detection_roi.x << " " << detection_roi.y
79 << " " << detection_roi.width << " " << detection_roi.height
80 << " crop_roi: " << crop_roi.x << " " << crop_roi.y << " "
81 << crop_roi.width << " " << crop_roi.height;
82 (*munkres_.costs())(row, col) = 0.0;
83 }
84 AINFO << "score " << (*munkres_.costs())(row, col);
85 }
86 }
87
88 munkres_.Maximize(&assignments);
89
90 for (size_t i = 0; i < hdmap_bboxes->size(); ++i) {
91 (*hdmap_bboxes)[i]->region.is_selected = false;
92 (*hdmap_bboxes)[i]->region.is_detected = false;
93 }
94
95 for (size_t i = 0; i < assignments.size(); ++i) {
96 if (static_cast<size_t>(assignments[i].first) >= hdmap_bboxes->size() ||
97 static_cast<size_t>(
98 assignments[i].second >= refined_bboxes.size() ||
99 (*hdmap_bboxes)[assignments[i].first]->region.is_selected ||
100 refined_bboxes[assignments[i].second]->region.is_selected)) {
101 } else {
102 auto &refined_bbox_region = refined_bboxes[assignments[i].second]->region;
103 auto &hdmap_bbox_region = (*hdmap_bboxes)[assignments[i].first]->region;
104 refined_bbox_region.is_selected = true;
105 hdmap_bbox_region.is_selected = true;
106
107 const auto &crop_roi = hdmap_bbox_region.crop_roi;
108 const auto &detection_roi = refined_bbox_region.detection_roi;
109 bool outside_crop_roi = ((crop_roi & detection_roi) != detection_roi);
110 if (hdmap_bbox_region.outside_image || outside_crop_roi) {
111 hdmap_bbox_region.is_detected = false;
112 } else {
113 hdmap_bbox_region.detection_roi = refined_bbox_region.detection_roi;
114 hdmap_bbox_region.detect_class_id = refined_bbox_region.detect_class_id;
115 hdmap_bbox_region.detect_score = refined_bbox_region.detect_score;
116 hdmap_bbox_region.is_detected = refined_bbox_region.is_detected;
117 hdmap_bbox_region.is_selected = refined_bbox_region.is_selected;
118 }
119 }
120 }
121
122 for (size_t i = 0; i < hdmap_bboxes->size(); ++i) {
123 AINFO << "hdmap_bboxes-" << i << ":"
124 << " projection_roi: "
125 << (*hdmap_bboxes)[i]->region.projection_roi.ToStr()
126 << " detection_roi: "
127 << (*hdmap_bboxes)[i]->region.detection_roi.ToStr();
128 }
129}
130
131} // namespace trafficlight
132} // namespace perception
133} // namespace apollo
void Maximize(std::vector< std::pair< size_t, size_t > > *assignments)
void Resize(const size_t resize_height, const size_t resize_width)
void Reserve(const size_t reserve_height, const size_t reserve_width)
void SelectTrafficLights(const std::vector< base::TrafficLightPtr > &refined_bboxes, std::vector< base::TrafficLightPtr > *hdmap_bboxes)
Choose traffic lights using Gaussian calculation scores.
Definition select.cc:42
double Calc2dGaussianScore(base::Point2DI p1, base::Point2DI p2, float sigma1, float sigma2)
Calculate 2d Gaussian score.
Definition select.cc:34
bool Init(int rows, int cols)
Initialize select object parameters.
Definition select.cc:24
#define AINFO
Definition log.h:42
class register implement
Definition arena_queue.h:37