45void BEVObstacleDetector::InitImageSize(
const petr::ModelParam &model_param) {
47 auto resize = model_param.resize();
48 if (resize.width() == 0 && resize.height() == 0) {
52 width_ = resize.width();
53 height_ = resize.height();
56 AINFO <<
"height=" << height_ <<
", "
57 <<
"width=" << width_;
60bool BEVObstacleDetector::InitTypes(
const petr::ModelParam &model_param) {
61 for (
const auto &class_name : model_param.info().class_names()) {
66 AERROR <<
"Unsupported subtype type!" << class_name;
76 std::string config_file =
79 AERROR <<
"Read model param failed!";
83 InitTypes(model_param_);
84 InitImageSize(model_param_);
86 const auto &model_info = model_param_.
info();
87 std::string model_path =
GetModelPath(model_info.name());
89 AERROR <<
"Init network failed!";
95void BEVObstacleDetector::Mat2Vec(
const cv::Mat &im,
float *data) {
99 int rc = im.channels();
101 for (
int i = 0; i < rc; ++i) {
102 cv::extractChannel(im, cv::Mat(rh, rw, CV_32FC1, data + i * rh * rw), i);
106bool BEVObstacleDetector::ImagePreprocess(
const CameraFrame *frame,
107 base::BlobPtr<float> input_img_blob) {
108 float *img_data_ptr = input_img_blob->mutable_cpu_data();
110 DataProvider::ImageOptions image_options;
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));
123 Resize(&img, &img_r, width_, height_);
125 common::Crop crop = model_param_.
crop();
127 Crop(&img_r, &img_c, crop.x(), crop.y(), crop.width(), crop.height());
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()};
134 std::vector<float> image_data(1 * 3 * img_height_crop_ * img_width_crop_,
136 Mat2Vec(img_c, image_data.data());
138 images_data.insert(images_data.end(), image_data.begin(), image_data.end());
140 memcpy(img_data_ptr, images_data.data(), images_data.size() *
sizeof(
float));
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));
153 if (frame ==
nullptr) {
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());
162 ImagePreprocess(frame, input_img_blob);
163 ImageExtrinsicPreprocess(input_img2lidar_blob);
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());
175 GetObjects(output_bbox_blob, output_label_blob, output_score_blob, types_,
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;
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]);
#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_
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 Resize(cv::Mat *img, cv::Mat *img_n, int width, int height)
Image resize function
void Normalize(const std::vector< float > &mean, const std::vector< float > &std, float scale, cv::Mat *img)
Image normalize function
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
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
std::vector< base::ObjectPtr > detected_objects
optional common::ModelInfo info
optional common::Normalize normalize
optional common::Crop crop
optional float score_threshold