Apollo 10.0
自动驾驶开放平台
bev_obstacle_detector.cc
浏览该文件的文档.
1/******************************************************************************
2 * Copyright 2022 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 <map>
19
20#include "cyber/common/file.h"
21#include "cyber/common/log.h"
26
27namespace apollo {
28namespace perception {
29namespace camera {
30
31const std::map<std::string, base::ObjectSubType> kNuScenesName2SubTypeMap = {
34 {"construction_vehicle", base::ObjectSubType::CAR},
36 {"trailer", base::ObjectSubType::CAR},
40 {"pedestrian", base::ObjectSubType::PEDESTRIAN},
41 {"traffic_cone", base::ObjectSubType::TRAFFICCONE},
42 {"MAX_OBJECT_TYPE", base::ObjectSubType::MAX_OBJECT_TYPE},
43};
44
45void BEVObstacleDetector::InitImageSize(const petr::ModelParam &model_param) {
46 // resize
47 auto resize = model_param.resize();
48 if (resize.width() == 0 && resize.height() == 0) {
49 width_ = options_.image_width * resize.fx();
50 height_ = options_.image_height * resize.fy();
51 } else {
52 width_ = resize.width();
53 height_ = resize.height();
54 }
55
56 AINFO << "height=" << height_ << ", "
57 << "width=" << width_;
58}
59
60bool BEVObstacleDetector::InitTypes(const petr::ModelParam &model_param) {
61 for (const auto &class_name : model_param.info().class_names()) {
62 if (kNuScenesName2SubTypeMap.find(class_name) !=
64 types_.push_back(kNuScenesName2SubTypeMap.at(class_name));
65 } else {
66 AERROR << "Unsupported subtype type!" << class_name;
67 return false;
68 }
69 }
70 return true;
71}
72
74 options_ = options;
75
76 std::string config_file =
77 GetConfigFile(options_.config_path, options_.config_file);
78 if (!cyber::common::GetProtoFromFile(config_file, &model_param_)) {
79 AERROR << "Read model param failed!";
80 return false;
81 }
82
83 InitTypes(model_param_);
84 InitImageSize(model_param_);
85
86 const auto &model_info = model_param_.info();
87 std::string model_path = GetModelPath(model_info.name());
88 if (!InitNetwork(model_param_.info(), model_path)) {
89 AERROR << "Init network failed!";
90 return false;
91 }
92 return true;
93}
94
95void BEVObstacleDetector::Mat2Vec(const cv::Mat &im, float *data) {
96 ACHECK(nullptr != data);
97 int rh = im.rows;
98 int rw = im.cols;
99 int rc = im.channels();
100
101 for (int i = 0; i < rc; ++i) {
102 cv::extractChannel(im, cv::Mat(rh, rw, CV_32FC1, data + i * rh * rw), i);
103 }
104}
105
106bool BEVObstacleDetector::ImagePreprocess(const CameraFrame *frame,
107 base::BlobPtr<float> input_img_blob) {
108 float *img_data_ptr = input_img_blob->mutable_cpu_data();
109
110 DataProvider::ImageOptions image_options;
111 image_options.target_color = base::Color::RGB;
112
113 float scale = 1.0f;
114 int camera_num = 6;
115 base::Image8U image;
116 std::vector<float> images_data;
117 for (int i = 0; i < camera_num; ++i) {
118 frame->data_provider[i]->GetImage(image_options, &image);
119 cv::Mat img(image.rows(), image.cols(), CV_8UC3, cv::Scalar(0, 0, 0));
120 memcpy(img.data, image.cpu_data(), image.total() * sizeof(uint8_t));
121
122 cv::Mat img_r;
123 Resize(&img, &img_r, width_, height_);
124
125 common::Crop crop = model_param_.crop();
126 cv::Mat img_c;
127 Crop(&img_r, &img_c, crop.x(), crop.y(), crop.width(), crop.height());
128
129 common::Normalize normalize = model_param_.normalize();
130 std::vector<float> mean{normalize.mean().begin(), normalize.mean().end()};
131 std::vector<float> std{normalize.std().begin(), normalize.std().end()};
132 Normalize(mean, std, scale, &img_c);
133
134 std::vector<float> image_data(1 * 3 * img_height_crop_ * img_width_crop_,
135 0.0f);
136 Mat2Vec(img_c, image_data.data());
137
138 images_data.insert(images_data.end(), image_data.begin(), image_data.end());
139 }
140 memcpy(img_data_ptr, images_data.data(), images_data.size() * sizeof(float));
141
142 return true;
143}
144
145bool BEVObstacleDetector::ImageExtrinsicPreprocess(
146 base::BlobPtr<float> input_img2lidar_blob) {
147 float *img2lidar_data_ptr = input_img2lidar_blob->mutable_cpu_data();
148 memcpy(img2lidar_data_ptr, k_data_.data(), k_data_.size() * sizeof(float));
149 return true;
150}
151
153 if (frame == nullptr) {
154 return false;
155 }
156
157 // Inputs
158 auto model_inputs = model_param_.info().inputs();
159 auto input_img_blob = net_->get_blob(model_inputs[0].name());
160 auto input_img2lidar_blob = net_->get_blob(model_inputs[1].name());
161
162 ImagePreprocess(frame, input_img_blob);
163 ImageExtrinsicPreprocess(input_img2lidar_blob);
164
165 // Net forward
166 net_->Infer();
167
168 // Outputs
169 auto model_outputs = model_param_.info().outputs();
170 auto output_bbox_blob = net_->get_blob(model_outputs[0].name());
171 auto output_score_blob = net_->get_blob(model_outputs[1].name());
172 auto output_label_blob = net_->get_blob(model_outputs[2].name());
173
174 float threshold = model_param_.score_threshold();
175 GetObjects(output_bbox_blob, output_label_blob, output_score_blob, types_,
176 threshold, &frame->detected_objects);
177
178 Nuscenes2Apollo(&frame->detected_objects);
179
180 return true;
181}
182
183/*
184bbox_nuscenes to bbox_apollo: Rotate 90 degrees counterclockwise about the
185z-axis
186*/
187// bbox: x, y, z, w, l, h, yaw, vx, vy
188bool BEVObstacleDetector::Nuscenes2Apollo(
189 std::vector<base::ObjectPtr> *objects) {
190 ACHECK(objects != nullptr);
191 for (auto &obj : *objects) {
192 obj->theta -= M_PI / 2;
193 obj->direction[0] = cosf(obj->theta);
194 obj->direction[1] = sinf(obj->theta);
195 obj->direction[2] = 0;
196
197 Eigen::AngleAxisd rotation_vector(-M_PI / 2, Eigen::Vector3d(0, 0, 1));
198 obj->center = rotation_vector.matrix() * obj->center;
199 obj->camera_supplement.local_center[0] = static_cast<float>(obj->center[0]);
200 obj->camera_supplement.local_center[1] = static_cast<float>(obj->center[1]);
201 obj->camera_supplement.local_center[2] = static_cast<float>(obj->center[2]);
202 }
203 return true;
204}
205
207
208} // namespace camera
209} // namespace perception
210} // namespace apollo
#define REGISTER_OBSTACLE_DETECTOR(name)
bool Detect(CameraFrame *frame) override
Get obstacles detection result
bool Init(const ObstacleDetectorInitOptions &options=ObstacleDetectorInitOptions()) override
Init ObstacleDetector constructor.
virtual bool InitNetwork(const common::ModelInfo &model_info, const std::string &model_root)
Interface for network initialization
std::shared_ptr< inference::Inference > net_
#define ACHECK(cond)
Definition log.h:80
#define AERROR
Definition log.h:44
#define AINFO
Definition log.h:42
bool GetProtoFromFile(const std::string &file_name, google::protobuf::Message *message)
Parses the content of the file specified by the file_name as a representation of protobufs,...
Definition file.cc:132
void Resize(cv::Mat *img, cv::Mat *img_n, int width, int height)
Image resize function
Definition preprocess.cc:24
void Normalize(const std::vector< float > &mean, const std::vector< float > &std, float scale, cv::Mat *img)
Image normalize function
Definition preprocess.cc:33
const std::map< std::string, base::ObjectSubType > kNuScenesName2SubTypeMap
void GetObjects(const base::BlobPtr< float > &box3ds, const base::BlobPtr< float > &labels, const base::BlobPtr< float > &scores, const std::vector< base::ObjectSubType > &types, float score_threshold, std::vector< base::ObjectPtr > *objects)
Get the Objects objects from blob
void Crop(cv::Mat *img, cv::Mat *img_n, int x, int y, int width, int height)
Image crop function
Definition preprocess.cc:28
std::string GetConfigFile(const std::string &config_path, const std::string &config_file)
Definition util.cc:80
std::string GetModelPath(const std::string &model_name)
Get the model path by model name, search from APOLLO_MODEL_PATH
Definition util.cc:44
class register implement
Definition arena_queue.h:37
Definition future.h:29
std::vector< base::ObjectPtr > detected_objects