Apollo 10.0
自动驾驶开放平台
camera_detection_multi_stage_component.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
22
23namespace apollo {
24namespace perception {
25namespace camera {
26
27bool CameraDetectionMultiStageComponent::InitObstacleDetector(
28 const CameraDetectionMultiStage& detection_param) {
29 ObstacleDetectorInitOptions init_options;
30 // Init conf file
31 auto plugin_param = detection_param.plugin_param();
32 init_options.config_path = plugin_param.config_path();
33 init_options.config_file = plugin_param.config_file();
34 init_options.gpu_id = detection_param.gpu_id();
35 timestamp_offset_ = detection_param.timestamp_offset();
36
37 // Init camera params
38 std::string camera_name = detection_param.camera_name();
40 algorithm::SensorManager::Instance()->GetUndistortCameraModel(
41 camera_name);
42 ACHECK(model) << "Can't find " << camera_name
43 << " in data/conf/sensor_meta.pb.txt";
44 auto pinhole = static_cast<base::PinholeCameraModel*>(model.get());
45 init_options.intrinsic = pinhole->get_intrinsic_params();
46 camera_k_matrix_ = init_options.intrinsic;
47 init_options.image_height = model->get_height();
48 init_options.image_width = model->get_width();
49 image_height_ = model->get_height();
50 image_width_ = model->get_width();
51
52 // Init detector
53 detector_.reset(
54 BaseObstacleDetectorRegisterer::GetInstanceByName(plugin_param.name()));
55 detector_->Init(init_options);
56 return true;
57}
58
59bool CameraDetectionMultiStageComponent::InitCameraFrame(
60 const CameraDetectionMultiStage& detection_param) {
61 DataProvider::InitOptions init_options;
62 init_options.image_height = image_height_;
63 init_options.image_width = image_width_;
64 init_options.do_undistortion = detection_param.enable_undistortion();
65 init_options.sensor_name = detection_param.camera_name();
66 init_options.device_id = detection_param.gpu_id();
67 AINFO << "init_options.device_id: " << init_options.device_id
68 << " camera_name: " << init_options.sensor_name;
69
70 data_provider_ = std::make_shared<camera::DataProvider>();
71 data_provider_->Init(init_options);
72
73 return true;
74}
75
76bool CameraDetectionMultiStageComponent::InitTransformWrapper(
77 const CameraDetectionMultiStage& detection_param) {
78 trans_wrapper_.reset(new onboard::TransformWrapper());
79 // tf_camera_frame_id
80 trans_wrapper_->Init(detection_param.camera_name());
81 return true;
82}
83
85 CameraDetectionMultiStage detection_param;
86 if (!GetProtoConfig(&detection_param)) {
87 AERROR << "Load camera detection 3d component config failed!";
88 return false;
89 }
90
91 InitObstacleDetector(detection_param);
92
93 InitCameraFrame(detection_param);
94
95 InitTransformWrapper(detection_param);
96
97 writer_ = node_->CreateWriter<onboard::CameraFrame>(
98 detection_param.channel().output_obstacles_channel_name());
99 return true;
100}
101
103 const std::shared_ptr<apollo::drivers::Image>& msg) {
105 std::shared_ptr<onboard::CameraFrame> out_message(new (std::nothrow)
107 bool status = InternalProc(msg, out_message);
108 if (status) {
109 writer_->Write(out_message);
110 AINFO << "Send camera detection 2d output message.";
111 }
112
113 return status;
114}
115
116bool CameraDetectionMultiStageComponent::InternalProc(
117 const std::shared_ptr<apollo::drivers::Image>& msg,
118 const std::shared_ptr<onboard::CameraFrame>& out_message) {
119 out_message->data_provider = data_provider_;
120 // Fill image
121 // todo(daohu527): need use real memory size
122 out_message->data_provider->FillImageData(
123 image_height_, image_width_,
124 reinterpret_cast<const uint8_t*>(msg->data().data()), msg->encoding());
125
126 out_message->camera_k_matrix = camera_k_matrix_;
127
128 const double msg_timestamp = msg->measurement_time() + timestamp_offset_;
129 out_message->timestamp = msg_timestamp;
130
131 // Get sensor to world pose from TF
132 Eigen::Affine3d camera2world;
133 if (!trans_wrapper_->GetSensor2worldTrans(msg_timestamp, &camera2world)) {
134 const std::string err_str =
135 absl::StrCat("failed to get camera to world pose, ts: ", msg_timestamp,
136 " frame_id: ", msg->frame_id());
137 AERROR << err_str;
138 return false;
139 }
140
141 out_message->frame_id = frame_id_;
142 ++frame_id_;
143
144 out_message->camera2world_pose = camera2world;
145
146 // Detect
147 PERF_BLOCK("camera_2d_detector")
148 detector_->Detect(out_message.get());
150 return true;
151}
152
153} // namespace camera
154} // namespace perception
155} // namespace apollo
bool GetProtoConfig(T *config) const
std::shared_ptr< Node > node_
bool Proc(const std::shared_ptr< drivers::Image > &msg) override
Process of camera detection 2d compoment
#define ACHECK(cond)
Definition log.h:80
#define AERROR
Definition log.h:44
#define AINFO
Definition log.h:42
std::shared_ptr< BaseCameraModel > BaseCameraModelPtr
Definition camera.h:75
class register implement
Definition arena_queue.h:37
#define PERF_FUNCTION(...)
Definition profiler.h:54
#define PERF_BLOCK_END
Definition profiler.h:53
#define PERF_BLOCK(...)
Definition profiler.h:52