Apollo 11.0
自动驾驶开放平台
smoke_obstacle_detector.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 *****************************************************************************/
17
18#include <opencv2/opencv.hpp>
19
20#include "cyber/common/file.h"
21#include "cyber/common/log.h"
26
27namespace apollo {
28namespace perception {
29namespace camera {
30
32 const smoke::ModelParam &model_param) {
33 // offset
34 auto offset_ratio = model_param.offset_ratio();
35 int image_width = options_.image_width;
36 int image_height = options_.image_height;
37 offset_y_ = static_cast<int>(image_height * offset_ratio.y());
38 AINFO << "image_width=" << image_width << ", "
39 << "image_height=" << image_height << ", "
40 << "offset_y=" << offset_y_ << ", ";
41}
42
44 types_.push_back(base::ObjectSubType::CAR);
45 types_.push_back(base::ObjectSubType::CYCLIST);
46 types_.push_back(base::ObjectSubType::PEDESTRIAN);
47 types_.push_back(base::ObjectSubType::VAN);
48 types_.push_back(base::ObjectSubType::TRUCK);
49 types_.push_back(base::ObjectSubType::BUS);
50 types_.push_back(base::ObjectSubType::TRAFFICCONE);
51}
52
54 const smoke::ModelParam &model_param) {
55 // resize
56 auto resize = model_param.resize();
57 if (resize.width() == 0 && resize.height() == 0) {
58 width_ = options_.image_width * resize.fx();
59 height_ = options_.image_height * resize.fy();
60 } else {
61 width_ = resize.width();
62 height_ = resize.height();
63 }
64
65 AINFO << "height=" << height_ << ", "
66 << "width=" << width_;
67}
68
70 // min 2d height
71 min_dims_ = model_param.min_dims();
72 // confidence threshold
73 confidence_threshold_ = model_param.confidence_threshold();
74 // ori cycle
75 ori_cycle_ = model_param.ori_cycle();
76 // border ratio
77 border_ratio_ = model_param.border_ratio();
78}
79
81 options_ = options;
82
83 std::string config_file =
84 GetConfigFile(options_.config_path, options_.config_file);
85 if (!cyber::common::GetProtoFromFile(config_file, &model_param_)) {
86 AERROR << "Read model param failed!";
87 return false;
88 }
89
90 InitImageOffset(model_param_);
91 InitImageSize(model_param_);
92 InitParam(model_param_);
94
95 const auto &model_info = model_param_.info();
96 std::string model_path = GetModelPath(model_info.name());
97
98 if (!InitNetwork(model_param_.info(), model_path)) {
99 AERROR << "Init network failed!";
100 return false;
101 }
102
103 return true;
104}
105
107 const base::Image8U *image, std::shared_ptr<base::Blob<float>> input_blob) {
108 cv::Mat img = cv::Mat(image->rows(), image->cols(), CV_8UC3);
109 memcpy(img.data, image->cpu_data(),
110 image->rows() * image->cols() * image->channels() * sizeof(uint8_t));
111
112 // resize
113 cv::resize(img, img, cv::Size(width_, height_));
114
115 // mean and std
116 img.convertTo(img, CV_32F, 1.0 / 255, 0);
117 std::vector<float> mean_values{0, 0, 0};
118 std::vector<float> std_values{0.229, 0.224, 0.225};
119
120 std::vector<cv::Mat> rgbChannels(3);
121 cv::split(img, rgbChannels);
122 for (int i = 0; i < 3; ++i) {
123 rgbChannels[i].convertTo(rgbChannels[i], CV_32FC1, 1 / std_values[i],
124 (0.0 - mean_values[i]) / std_values[i]);
125 }
126 cv::merge(rgbChannels, img);
127
128 // from hwc to chw
129 int rows = img.rows;
130 int cols = img.cols;
131 int chs = img.channels();
132
133 // fill input_blob
134 input_blob->Reshape({1, chs, rows, cols});
135 float *input_data = input_blob->mutable_cpu_data();
136 for (int i = 0; i < chs; ++i) {
137 cv::extractChannel(
138 img, cv::Mat(rows, cols, CV_32FC1, input_data + i * rows * cols), i);
139 }
140 return true;
141}
142
144 if (frame == nullptr) {
145 return false;
146 }
147
148 if (cudaSetDevice(options_.gpu_id) != cudaSuccess) {
149 AERROR << "Failed to set device to " << options_.gpu_id;
150 return false;
151 }
152
153 // Check init height == runtime height
154 if (options_.image_height != frame->data_provider->src_height() ||
155 options_.image_width != frame->data_provider->src_width()) {
156 AWARN << "Initialized image size not equal to runtime!";
157 }
158
159 auto model_inputs = model_param_.info().inputs();
160 auto input_blob = net_->get_blob(model_inputs[0].name());
161 auto input_k_blob = net_->get_blob(model_inputs[1].name());
162 auto input_ratio_blob = net_->get_blob(model_inputs[2].name());
163
164 // Input k matrix
165 const auto &camera_k_matrix = options_.intrinsic.inverse();
166 float *k_data = input_k_blob->mutable_cpu_data();
167 for (size_t i = 0; i < 3; ++i) {
168 for (size_t j = 0; j < 3; ++j) {
169 k_data[i * 3 + j] = camera_k_matrix(i, j);
170 }
171 }
172 AINFO << "Camera k matrix input to obstacle postprocessor: \n"
173 << k_data[0] << ", " << k_data[1] << ", " << k_data[2] << "\n"
174 << k_data[3] << ", " << k_data[4] << ", " << k_data[5] << "\n"
175 << k_data[6] << ", " << k_data[7] << ", " << k_data[8] << "\n";
176
177 // Input ratio
178 float *ratio_data = input_ratio_blob->mutable_cpu_data();
179 ratio_data[0] = 4.f * static_cast<float>(frame->data_provider->src_width()) /
180 static_cast<float>(width_);
181 ratio_data[1] = 4.f * static_cast<float>(frame->data_provider->src_height()) /
182 static_cast<float>(height_);
183
184 // Input image
185 DataProvider::ImageOptions image_options;
186 image_options.target_color = base::Color::RGB;
187 image_options.crop_roi = base::RectI(0, offset_y_, options_.image_width,
188 options_.image_height - offset_y_);
189 image_options.do_crop = true;
190 // todo(daohu527) copy assignment
191 image_ = std::make_shared<base::Image8U>();
192 frame->data_provider->GetImage(image_options, image_.get());
193
194 // Preprocess
195 PERF_BLOCK("camera_3d_detector_preprocess")
196 Preprocess(image_.get(), input_blob);
198
199 // Net forward
200 PERF_BLOCK("camera_3d_detector_infer")
201 net_->Infer();
203
204 // Output
205 auto model_outputs = model_param_.info().outputs();
206 auto detection_blob = net_->get_blob(model_outputs[0].name());
207
208 PERF_BLOCK("camera_3d_detector_get_obj")
209 GetObjectsCpu(detection_blob, types_, model_param_, &frame->detected_objects,
210 frame->data_provider->src_width(),
211 frame->data_provider->src_height() - offset_y_);
213
214 FilterByMinDims(min_dims_, &frame->detected_objects);
215
216 RecoverBBox(frame->data_provider->src_width(),
217 frame->data_provider->src_height() - offset_y_, offset_y_,
218 &frame->detected_objects);
219
220 // appearance features for tracking
221 frame->feature_blob = net_->get_blob(model_outputs[1].name());
222
223 // post processing
224 for (auto &obj : frame->detected_objects) {
225 // recover alpha
226 obj->camera_supplement.alpha /= ori_cycle_;
227 }
228
229 return true;
230}
231
233
234} // namespace camera
235} // namespace perception
236} // namespace apollo
#define REGISTER_OBSTACLE_DETECTOR(name)
A wrapper around SyncedMemory holders serving as the basic computational unit for images,...
Definition blob.h:88
A wrapper around Blob holders serving as the basic computational unit for images.
Definition image_8u.h:44
const uint8_t * cpu_data() const
Definition image_8u.h:99
virtual bool InitNetwork(const common::ModelInfo &model_info, const std::string &model_root)
Interface for network initialization
std::shared_ptr< inference::Inference > net_
bool Preprocess(const base::Image8U *image, std::shared_ptr< base::Blob< float > > input_blob)
void InitParam(const smoke::ModelParam &model_param)
bool Detect(onboard::CameraFrame *frame) override
Main part to detect obstacle
void InitImageSize(const smoke::ModelParam &model_param)
void InitImageOffset(const smoke::ModelParam &model_param)
bool Init(const ObstacleDetectorInitOptions &options) override
Init function for SmokeObstacleDetector
#define AERROR
Definition log.h:44
#define AINFO
Definition log.h:42
#define AWARN
Definition log.h:43
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
Rect< int > RectI
Definition box.h:159
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)
void RecoverBBox(int roi_w, int roi_h, int offset_y, std::vector< base::ObjectPtr > *objects)
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
#define PERF_BLOCK_END
Definition profiler.h:53
#define PERF_BLOCK(...)
Definition profiler.h:52
std::shared_ptr< camera::DataProvider > data_provider