18#include <opencv2/opencv.hpp>
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_ <<
", ";
56 auto resize = model_param.
resize();
57 if (resize.width() == 0 && resize.height() == 0) {
61 width_ = resize.width();
62 height_ = resize.height();
65 AINFO <<
"height=" << height_ <<
", "
66 <<
"width=" << width_;
83 std::string config_file =
86 AERROR <<
"Read model param failed!";
95 const auto &model_info = model_param_.
info();
96 std::string model_path =
GetModelPath(model_info.name());
99 AERROR <<
"Init network failed!";
108 cv::Mat img = cv::Mat(image->
rows(), image->
cols(), CV_8UC3);
113 cv::resize(img, img, cv::Size(width_, height_));
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};
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]);
126 cv::merge(rgbChannels, img);
131 int chs = img.channels();
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) {
138 img, cv::Mat(rows, cols, CV_32FC1, input_data + i * rows * cols), i);
144 if (frame ==
nullptr) {
148 if (cudaSetDevice(options_.
gpu_id) != cudaSuccess) {
149 AERROR <<
"Failed to set device to " << options_.
gpu_id;
156 AWARN <<
"Initialized image size not equal to runtime!";
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());
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);
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";
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_);
191 image_ = std::make_shared<base::Image8U>();
205 auto model_outputs = model_param_.
info().outputs();
206 auto detection_blob =
net_->get_blob(model_outputs[0].name());
226 obj->camera_supplement.alpha /= ori_cycle_;
#define REGISTER_OBSTACLE_DETECTOR(name)
A wrapper around SyncedMemory holders serving as the basic computational unit for images,...
A wrapper around Blob holders serving as the basic computational unit for images.
const uint8_t * cpu_data() const
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
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,...
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)
std::string GetModelPath(const std::string &model_name)
Get the model path by model name, search from APOLLO_MODEL_PATH
Eigen::Matrix3f intrinsic
optional common::Resize resize
optional OffsetRatio offset_ratio
optional MinDims min_dims
optional float confidence_threshold
optional common::ModelInfo info
optional float border_ratio
std::vector< base::ObjectPtr > detected_objects
std::shared_ptr< base::Blob< float > > feature_blob
std::shared_ptr< camera::DataProvider > data_provider