Apollo 10.0
自动驾驶开放平台
postprocess.cc
浏览该文件的文档.
1/******************************************************************************
2 * Copyright 2023 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 *****************************************************************************/
16
18
19#include "cyber/common/log.h"
21
22namespace apollo {
23namespace perception {
24namespace camera {
25
26void FilterByMinDims(const smoke::MinDims &min_dims,
27 std::vector<base::ObjectPtr> *objects) {
28 std::size_t valid_index = 0, cur_index = 0;
29 while (cur_index < objects->size()) {
30 const auto& obj = objects->at(cur_index);
31 float height =
32 obj->camera_supplement.box.ymax - obj->camera_supplement.box.ymin;
33 if (height >= min_dims.min_2d_height() &&
34 obj->size[2] >= min_dims.min_3d_height() &&
35 obj->size[1] >= min_dims.min_3d_width() &&
36 obj->size[0] >= min_dims.min_3d_length()) {
37 if (valid_index != cur_index)
38 objects->at(valid_index) = objects->at(cur_index);
39 ++valid_index;
40 }
41 ++cur_index;
42 }
43
44 objects->resize(valid_index);
45 AINFO << valid_index << " of " << cur_index << " obstacles kept";
46}
47
48void RecoverBBox(int roi_w, int roi_h, int offset_y,
49 std::vector<base::ObjectPtr> *objects) {
50 for (auto &obj : *objects) {
51 float xmin = obj->camera_supplement.box.xmin;
52 float ymin = obj->camera_supplement.box.ymin;
53 float xmax = obj->camera_supplement.box.xmax;
54 float ymax = obj->camera_supplement.box.ymax;
55
56 float x = xmin * roi_w;
57 float w = (xmax - xmin) * roi_w;
58 float y = ymin * roi_h + offset_y;
59 float h = (ymax - ymin) * roi_h;
60 base::RectF rect_det(x, y, w, h);
61 base::RectF rect_img(0, 0, roi_w, roi_h + offset_y);
62 obj->camera_supplement.box = rect_det & rect_img;
63
64 constexpr double eps = 1e-2;
65 // Truncation assignment based on bbox positions
66 if ((ymin < eps) || (ymax >= (1.0 - eps))) {
67 obj->camera_supplement.truncated_vertical = 0.5;
68 } else {
69 obj->camera_supplement.truncated_vertical = 0.0;
70 }
71
72 if ((xmin < eps) || (xmax >= (1.0 - eps))) {
73 obj->camera_supplement.truncated_horizontal = 0.5;
74 } else {
75 obj->camera_supplement.truncated_horizontal = 0.0;
76 }
77 }
78}
79
80void FillBBox(base::ObjectPtr obj, const float *bbox, int width, int height) {
81 obj->camera_supplement.box.xmin = bbox[2] / width;
82 obj->camera_supplement.box.ymin = bbox[3] / height;
83 obj->camera_supplement.box.xmax = bbox[4] / width;
84 obj->camera_supplement.box.ymax = bbox[5] / height;
85}
86
87void FillBBox3d(base::ObjectPtr obj, const float *bbox) {
88 obj->camera_supplement.alpha = bbox[1];
89 // todo: why obj size not put in camera_supplement
90 obj->size[2] = bbox[6];
91 obj->size[1] = bbox[7];
92 obj->size[0] = bbox[8];
93
94 obj->camera_supplement.local_center[0] = bbox[9];
95 obj->camera_supplement.local_center[1] = bbox[10];
96 obj->camera_supplement.local_center[2] = bbox[11];
97}
98
100 int cls, const std::vector<base::ObjectSubType> &types) {
101 if (cls < 0 || cls >= static_cast<int>(types.size())) {
103 }
104
105 return types[cls];
106}
107
108void GetObjectsCpu(const std::shared_ptr<base::Blob<float>> &output_blob,
109 const std::vector<base::ObjectSubType> &types,
110 const smoke::ModelParam &model_param,
111 std::vector<base::ObjectPtr> *objects,
112 int width, int height) {
113 const float* detect_result = output_blob->cpu_data();
114 objects->clear();
115 auto shape = output_blob->shape();
116
117 int len_pred = shape[1];
118 int total = shape[0] * shape[1];
119 int step = 0;
120 while (step < total) {
121 const float* bbox = detect_result + step;
122 step += len_pred;
123 float score = bbox[13];
124 if (score < model_param.confidence_threshold()) {
125 continue;
126 }
127
128 base::ObjectPtr obj = std::make_shared<base::Object>();
129 int label = static_cast<int>(bbox[0]);
130 obj->sub_type = GetSubtype(label, types);
131 obj->type = base::kSubType2TypeMap.at(obj->sub_type);
132 obj->type_probs.assign(
133 static_cast<int>(base::ObjectType::MAX_OBJECT_TYPE), 0);
134 obj->sub_type_probs.assign(
135 static_cast<int>(base::ObjectSubType::MAX_OBJECT_TYPE), 0);
136 obj->type_probs[static_cast<int>(obj->type)] = score;
137 obj->sub_type_probs[static_cast<int>(obj->sub_type)] = score;
138 obj->confidence = score;
139
140 FillBBox(obj, bbox, width, height);
141 FillBBox3d(obj, bbox);
142 objects->push_back(obj);
143 }
144}
145
146} // namespace camera
147} // namespace perception
148} // namespace apollo
A wrapper around SyncedMemory holders serving as the basic computational unit for images,...
Definition blob.h:88
#define AINFO
Definition log.h:42
Rect< float > RectF
Definition box.h:160
const std::map< ObjectSubType, ObjectType > kSubType2TypeMap
ObjectSubType mapping
std::shared_ptr< Object > ObjectPtr
Definition object.h:127
void FillBBox3d(const float *bbox, base::ObjectPtr obj)
Fill the 3d bbox to object
void GetObjectsCpu(const std::shared_ptr< base::Blob< float > > &output_blob, const std::vector< base::ObjectSubType > &types, const smoke::ModelParam &model_param, std::vector< base::ObjectPtr > *objects, int width, int height)
void FilterByMinDims(const smoke::MinDims &min_dims, std::vector< base::ObjectPtr > *objects)
base::ObjectSubType GetSubtype(int cls, const std::vector< base::ObjectSubType > &types)
Get the Subtype
void FillBBox(base::ObjectPtr obj, const float *bbox, int width, int height)
void RecoverBBox(int roi_w, int roi_h, int offset_y, std::vector< base::ObjectPtr > *objects)
class register implement
Definition arena_queue.h:37