21#include "opencv2/opencv.hpp"
40void CaddnObstacleDetector::InitImageSize(
41 const caddn::ModelParam &model_param) {
43 auto resize = model_param.resize();
44 if (resize.width() == 0 && resize.height() == 0) {
48 width_ = resize.width();
49 height_ = resize.height();
52 AINFO <<
"height=" << height_ <<
", "
53 <<
"width=" << width_;
56void CaddnObstacleDetector::InitParam(
const caddn::ModelParam &model_param) {
58 score_threshold_ = model_param.score_threshold();
61bool CaddnObstacleDetector::InitTypes(
const caddn::ModelParam &model_param) {
62 for (
const auto &class_name : model_param.info().class_names()) {
66 AERROR <<
"Unsupported subtype type!" << class_name;
75 std::string config_file =
78 AERROR <<
"Read model param failed!";
82 InitImageSize(model_param_);
83 InitParam(model_param_);
84 InitTypes(model_param_);
86 const auto &model_info = model_param_.
info();
87 std::string model_path =
GetModelPath(model_info.name());
89 AERROR <<
"Init network failed!";
95bool CaddnObstacleDetector::Preprocess(
const base::Image8U *image,
97 cv::Mat img = cv::Mat(image->
rows(), image->
cols(), CV_8UC3);
98 memcpy(img.data, image->
cpu_data(), image->
total() *
sizeof(uint8_t));
101 cv::resize(img, img, cv::Size(width_, height_));
104 img.convertTo(img, CV_32F, 1.0 / 255, 0);
105 std::vector<float> mean_values{0, 0, 0};
106 std::vector<float> std_values{0.229, 0.224, 0.225};
108 std::vector<cv::Mat> rgbChannels(3);
109 cv::split(img, rgbChannels);
110 for (
int i = 0; i < 3; ++i) {
111 rgbChannels[i].convertTo(rgbChannels[i], CV_32FC1, 1 / std_values[i],
112 (0.0 - mean_values[i]) / std_values[i]);
114 cv::merge(rgbChannels, img);
119 int chs = img.channels();
122 input_blob->Reshape({1, chs, rows, cols});
123 float *input_data = input_blob->mutable_cpu_data();
124 for (
int i = 0; i < chs; ++i) {
126 img, cv::Mat(rows, cols, CV_32FC1, input_data + i * rows * cols), i);
132 if (frame ==
nullptr) {
137 auto model_inputs = model_param_.
info().inputs();
138 auto input_image_blob =
net_->get_blob(model_inputs[0].name());
139 auto input_cam2img_blob =
net_->get_blob(model_inputs[1].name());
140 auto input_lidar2cam_blob =
net_->get_blob(model_inputs[2].name());
142 const auto &camera_k_matrix = options_.
intrinsic;
143 float *input_cam_intrinsic = input_cam2img_blob->mutable_cpu_data();
144 for (
size_t i = 0; i < 3; ++i) {
145 for (
size_t j = 0; j < 4; ++j) {
147 input_cam_intrinsic[i * 4 + j] = 0.0;
149 input_cam_intrinsic[i * 4 + j] = camera_k_matrix(i, j);
154 float *input_lidar2cam_data = input_lidar2cam_blob->mutable_cpu_data();
155 for (
int i = 0; i < 16; ++i) {
156 input_lidar2cam_data[i] = lidar_to_cam_[i];
161 std::shared_ptr<base::Image8U> image = std::make_shared<base::Image8U>();
164 Preprocess(image.get(), input_image_blob);
170 auto model_outputs = model_param_.
info().outputs();
171 auto out_detections =
net_->get_blob(model_outputs[0].name());
172 auto out_labels =
net_->get_blob(model_outputs[1].name());
173 auto out_scores =
net_->get_blob(model_outputs[2].name());
180 out_detections, out_labels, out_scores);
#define REGISTER_OBSTACLE_DETECTOR(name)
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 Detect(onboard::CameraFrame *frame) override
Main part to detect obstacle
bool Init(const ObstacleDetectorInitOptions &options=ObstacleDetectorInitOptions()) override
Init function for CaddnObstacleDetector
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,...
std::shared_ptr< Blob< Dtype > > BlobPtr
const std::map< std::string, base::ObjectSubType > kKITTIName2SubTypeMap
void GetCaddnObjects(std::vector< base::ObjectPtr > *objects, const caddn::ModelParam &model_param, const std::vector< base::ObjectSubType > &types, const base::BlobPtr< float > &boxes, const base::BlobPtr< float > &labels, const base::BlobPtr< float > &scores)
Get the Caddn Objects objects from Blob
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::ModelInfo info
std::vector< base::ObjectPtr > detected_objects
std::shared_ptr< camera::DataProvider > data_provider