24namespace trafficlight {
29 if (!projection_.
Init(projection_init_option)) {
30 AERROR <<
"init multi_camera_projection failed.";
35 lights_on_image_array_.resize(num_cameras_);
36 lights_outside_image_array_.resize(num_cameras_);
39 AINFO <<
"preprocessor init succeed";
46 std::vector<base::TrafficLightPtr> *lights) {
48 selected_camera_name_.first = timestamp;
49 selected_camera_name_.second = GetMaxFocalLenWorkingCameraName();
51 AINFO <<
"TLPreprocessor Got signal number: " << lights->size()
52 <<
", ts: " << timestamp;
53 if (lights->empty()) {
54 AINFO <<
"No signals, select camera with max focal length: "
55 << selected_camera_name_.second;
59 if (!ProjectLightsAndSelectCamera(pose, option,
60 &(selected_camera_name_.second), lights)) {
61 AERROR <<
"project_lights_and_select_camera failed, ts: " << timestamp;
64 AINFO <<
"selected_camera_id: " << selected_camera_name_.second;
70 const std::string &cam_name) {
71 const double &proj_ts = selected_camera_name_.first;
72 const std::string &proj_camera_name = selected_camera_name_.second;
73 AINFO <<
"ready to sync information";
76 AERROR <<
"sync_image failed, "
77 <<
"get invalid camera_name: " << cam_name;
81 AINFO <<
"Enter TLPreprocessor::sync_image. proj_ts: " << proj_ts
82 <<
" proj_camera_name: " << proj_camera_name
83 <<
" image_ts: " << image_timestamp
84 <<
" image_camera_name: " << cam_name;
85 if (image_timestamp < last_pub_img_ts_) {
86 AWARN <<
"TLPreprocessor reject the image pub ts:" << image_timestamp
87 <<
" which is earlier than last output ts:" << last_pub_img_ts_
88 <<
", image_camera_name: " << cam_name;
92 if (proj_camera_name != cam_name) {
93 AWARN <<
"sync_image failed - find close enough projection,"
94 <<
"but camera_name not match.";
97 AINFO <<
"sync_image succeeded.";
98 last_pub_img_ts_ = image_timestamp;
104 const std::string &camera_name,
105 std::vector<base::TrafficLightPtr> *lights) {
106 lights_on_image_.clear();
107 lights_outside_image_.clear();
109 AINFO <<
"clear lights_outside_image_ " << lights_outside_image_.size();
111 if (lights->empty()) {
112 AINFO <<
"No lights to be projected";
116 if (!ProjectLights(pose, camera_name, lights, &lights_on_image_,
117 &lights_outside_image_)) {
118 AERROR <<
"update_lights_projection project lights on " << camera_name
123 if (lights_outside_image_.size() > 0) {
124 AERROR <<
"update_lights_projection failed,"
125 <<
"lights_outside_image->size() " << lights_outside_image_.size()
130 auto min_focal_len_working_camera = GetMinFocalLenWorkingCameraName();
131 if (camera_name == min_focal_len_working_camera) {
132 return lights_on_image_.size() > 0;
139 AINFO <<
"update_lights_projection light project out of image region. "
140 <<
"camera_name: " << camera_name;
145 AINFO <<
"UpdateLightsProjection success";
151 if (!projection_.
HasCamera(camera_name)) {
152 AERROR <<
"SetCameraWorkingFlag failed, "
153 <<
"get invalid camera_name: " << camera_name;
156 camera_is_working_flags_[camera_name] = is_working;
157 AINFO <<
"SetCameraWorkingFlag succeeded, camera_name: " << camera_name
158 <<
", flag: " << camera_is_working_flags_[camera_name];
163 bool *is_working)
const {
164 if (!projection_.
HasCamera(camera_name)) {
165 AERROR <<
"GetCameraWorkingFlag failed, "
166 <<
"get invalid camera_name: " << camera_name;
170 if (camera_is_working_flags_.find(camera_name) ==
171 camera_is_working_flags_.end() ||
172 !camera_is_working_flags_.at(camera_name)) {
180void TLPreprocessor::SelectCamera(
181 std::vector<base::TrafficLightPtrs> *lights_on_image_array,
182 std::vector<base::TrafficLightPtrs> *lights_outside_image_array,
185 auto min_focal_len_working_camera = GetMinFocalLenWorkingCameraName();
186 AINFO <<
"working camera with minimum focal length: "
187 << min_focal_len_working_camera;
190 for (
size_t cam_id = 0; cam_id < lights_on_image_array->size(); ++cam_id) {
191 const auto &camera_name = camera_names[cam_id];
192 bool is_working =
false;
195 AINFO <<
"camera " << camera_name <<
"is not working";
200 if (camera_name != min_focal_len_working_camera) {
202 if (lights_outside_image_array->at(cam_id).size() > 0) {
203 AINFO <<
"light project out of image, "
204 <<
"camera_name: " << camera_name
205 <<
" lights_outside_image_array->at(cam_id).size(): "
206 << lights_outside_image_array->at(cam_id).size();
209 auto lights = lights_on_image_array->at(cam_id);
210 for (
const auto light : lights) {
217 AINFO <<
"light project out of image region, "
218 <<
"camera_name: " << camera_name <<
" border_size: "
225 ok = (lights_on_image_array->at(cam_id).size() > 0);
229 *selected_camera_name = camera_name;
233 AINFO <<
"select_camera selection: " << *selected_camera_name;
236bool TLPreprocessor::ProjectLights(
237 const camera::CarPose &pose,
const std::string &camera_name,
238 std::vector<base::TrafficLightPtr> *lights,
241 if (lights->empty()) {
242 AINFO <<
"project_lights get empty signals.";
245 if (!projection_.
HasCamera(camera_name)) {
246 AERROR <<
"project_lights get invalid camera_name: " << camera_name;
251 bool is_working =
false;
253 AWARN <<
"TLPreprocessor::project_lights not project lights, "
254 <<
"camera is not working, camera_name: " << camera_name;
258 for (
size_t i = 0; i < lights->size(); ++i) {
260 auto light = lights->at(i);
261 if (!projection_.
Project(pose, ProjectOption(camera_name), light.get())) {
262 light->region.outside_image =
true;
263 *light_proj = *light;
264 lights_outside_image->push_back(light_proj);
266 light->region.outside_image =
false;
267 *light_proj = *light;
268 lights_on_image->push_back(light_proj);
274bool TLPreprocessor::ProjectLightsAndSelectCamera(
275 const camera::CarPose &pose,
const TLPreprocessorOption &option,
276 std::string *selected_camera_name,
277 std::vector<base::TrafficLightPtr> *lights) {
278 if (selected_camera_name ==
nullptr) {
279 AERROR <<
"selected_camera_name is not available";
282 if (lights ==
nullptr) {
283 AERROR <<
"lights is not available";
287 for (
auto &light_ptrs : lights_on_image_array_) {
290 for (
auto &light_ptrs : lights_outside_image_array_) {
296 for (
size_t cam_id = 0; cam_id < num_cameras_; ++cam_id) {
297 const std::string &camera_name = camera_names[cam_id];
298 if (!ProjectLights(pose, camera_name, lights,
299 &(lights_on_image_array_[cam_id]),
300 &(lights_outside_image_array_[cam_id]))) {
301 AERROR <<
"select_camera_by_lights_projection project lights on "
302 << camera_name <<
" image failed";
307 projections_outside_all_images_ = !lights->empty();
308 for (
size_t cam_id = 0; cam_id < num_cameras_; ++cam_id) {
309 projections_outside_all_images_ =
310 projections_outside_all_images_ &&
311 (lights_on_image_array_[cam_id].size() < lights->size());
313 if (projections_outside_all_images_) {
314 AWARN <<
"lights projections outside all images";
318 SelectCamera(&lights_on_image_array_, &lights_outside_image_array_, option,
319 selected_camera_name);
325 return projections_outside_all_images_;
330std::string TLPreprocessor::GetMinFocalLenWorkingCameraName()
const {
332 for (
auto itr = camera_names.crbegin(); itr != camera_names.crend(); ++itr) {
333 bool is_working =
false;
338 AWARN <<
"No working camera, return empty camera_name";
342std::string TLPreprocessor::GetMaxFocalLenWorkingCameraName()
const {
344 for (
const auto &camera_name : camera_names) {
345 bool is_working =
false;
350 AWARN <<
"No working camera, return empty camera_name";
#define PERCEPTION_REGISTER_TLPREPROCESSOR(name)
double getTimestamp() const
bool Project(const camera::CarPose &pose, const ProjectOption &option, base::TrafficLight *light) const
Boundary projection based on vehicle position and traffic lights.
bool Init(const MultiCamerasInitOption &options)
multicamera project initialization parameters.
bool HasCamera(const std::string &camera_name) const
Determine whether the camera exists.
int getImageWidth(const std::string &camera_name) const
Get the image width object.
int getImageHeight(const std::string &camera_name) const
Get the image height object.
const std::vector< std::string > & getCameraNamesByDescendingFocalLen() const
Get the camera names by descending focal len object.
bool GetAlllightsOutsideFlag() const
Get all projections outside the image.
bool Init(const TrafficLightPreprocessorInitOptions &options) override
Traffic light preprocessing initialization parameters.
bool SyncInformation(const double ts, const std::string &camera_name)
Determine whether the selected camera is available and synchronize timestamp information.
bool SetCameraWorkingFlag(const std::string &camera_name, bool is_working) override
Set the camera working flag object.
bool GetCameraWorkingFlag(const std::string &camera_name, bool *is_working) const override
Get the camera working flag object.
std::string Name() const override
returns the name of the preprocessing plugin.
bool UpdateCameraSelection(const camera::CarPose &pose, const TLPreprocessorOption &option, std::vector< base::TrafficLightPtr > *lights) override
Select the appropriate camera for preprocessing according to the position of the vehicle and the posi...
bool UpdateLightsProjection(const camera::CarPose &pose, const TLPreprocessorOption &option, const std::string &camera_name, std::vector< base::TrafficLightPtr > *lights) override
Update the traffic light projection status and determine whether the projection exceeds the boundary ...
std::shared_ptr< TrafficLight > TrafficLightPtr
std::vector< TrafficLightPtr > TrafficLightPtrs
bool OutOfValidRegion(const base::BBox2D< T > box, const T width, const T height, const T border_size=0)
std::vector< std::string > camera_names
std::map< std::string, int > * image_borders_size
float sync_interval_seconds
std::vector< std::string > camera_names