Skip to content

Commit 26b2226

Browse files
technolojinshmpwk
authored andcommitted
fix(traffic_light_classifier): fix zero size roi bug (autowarefoundation#7608)
* fix: continue to process when input roi size is zero Signed-off-by: Taekjin LEE <taekjin.lee@tier4.jp> * fix: consider when roi size is zero, rois is empty fix Signed-off-by: Taekjin LEE <taekjin.lee@tier4.jp> * fix: use emplace_back instead of push_back for adding images and backlight indices The code changes in `traffic_light_classifier_node.cpp` modify the way images and backlight indices are added to the respective vectors. Instead of using `push_back`, the code now uses `emplace_back`. This change improves performance and ensures proper object construction. Signed-off-by: Taekjin LEE <taekjin.lee@tier4.jp> * refactor: bring back for loop skim and output_msg filling Signed-off-by: Taekjin LEE <taekjin.lee@tier4.jp> * chore: refactor code to handle empty input ROIs in traffic_light_classifier_node.cpp Signed-off-by: Taekjin LEE <taekjin.lee@tier4.jp> * refactor: using index instead of vector length Signed-off-by: Taekjin LEE <taekjin.lee@tier4.jp> --------- Signed-off-by: Taekjin LEE <taekjin.lee@tier4.jp>
1 parent 637f58f commit 26b2226

File tree

1 file changed

+27
-16
lines changed

1 file changed

+27
-16
lines changed

perception/traffic_light_classifier/src/nodelet.cpp

+27-16
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,9 @@ void TrafficLightClassifierNodelet::imageRoiCallback(
8484
if (classifier_ptr_.use_count() == 0) {
8585
return;
8686
}
87+
if (input_rois_msg->rois.empty()) {
88+
return;
89+
}
8790

8891
cv_bridge::CvImagePtr cv_ptr;
8992
try {
@@ -95,41 +98,49 @@ void TrafficLightClassifierNodelet::imageRoiCallback(
9598
}
9699

97100
tier4_perception_msgs::msg::TrafficLightArray output_msg;
98-
99101
output_msg.signals.resize(input_rois_msg->rois.size());
100102

101103
std::vector<cv::Mat> images;
102104
std::vector<size_t> backlight_indices;
103-
for (size_t i = 0; i < input_rois_msg->rois.size(); i++) {
104-
// skip if the roi is not detected
105-
if (input_rois_msg->rois.at(i).roi.height == 0) {
106-
break;
105+
size_t idx_valid_roi = 0;
106+
for (const auto & input_roi : input_rois_msg->rois) {
107+
// ignore if the roi is not the type to be classified
108+
if (input_roi.traffic_light_type != classify_traffic_light_type_) {
109+
continue;
107110
}
108-
if (input_rois_msg->rois.at(i).traffic_light_type != classify_traffic_light_type_) {
111+
// skip if the roi size is zero
112+
if (input_roi.roi.height == 0 || input_roi.roi.width == 0) {
109113
continue;
110114
}
111-
output_msg.signals[images.size()].traffic_light_id =
112-
input_rois_msg->rois.at(i).traffic_light_id;
113-
output_msg.signals[images.size()].traffic_light_type =
114-
input_rois_msg->rois.at(i).traffic_light_type;
115-
const sensor_msgs::msg::RegionOfInterest & roi = input_rois_msg->rois.at(i).roi;
116115

116+
// set traffic light id and type
117+
output_msg.signals[idx_valid_roi].traffic_light_id = input_roi.traffic_light_id;
118+
output_msg.signals[idx_valid_roi].traffic_light_type = input_roi.traffic_light_type;
119+
120+
const sensor_msgs::msg::RegionOfInterest & roi = input_roi.roi;
117121
auto roi_img = cv_ptr->image(cv::Rect(roi.x_offset, roi.y_offset, roi.width, roi.height));
118122
if (is_harsh_backlight(roi_img)) {
119-
backlight_indices.emplace_back(i);
123+
backlight_indices.emplace_back(idx_valid_roi);
120124
}
121125
images.emplace_back(roi_img);
126+
idx_valid_roi++;
122127
}
123128

129+
// classify the images
124130
output_msg.signals.resize(images.size());
125-
if (!classifier_ptr_->getTrafficSignals(images, output_msg)) {
126-
RCLCPP_ERROR(this->get_logger(), "failed classify image, abort callback");
127-
return;
131+
if (!images.empty()) {
132+
if (!classifier_ptr_->getTrafficSignals(images, output_msg)) {
133+
RCLCPP_ERROR(this->get_logger(), "failed classify image, abort callback");
134+
return;
135+
}
128136
}
129137

130138
// append the undetected rois as unknown
131139
for (const auto & input_roi : input_rois_msg->rois) {
132-
if (input_roi.roi.height == 0 && input_roi.traffic_light_type == classify_traffic_light_type_) {
140+
// if the type is the target type but the roi size is zero, the roi is undetected
141+
if (
142+
(input_roi.roi.height == 0 || input_roi.roi.width == 0) &&
143+
input_roi.traffic_light_type == classify_traffic_light_type_) {
133144
tier4_perception_msgs::msg::TrafficLight tlr_sig;
134145
tlr_sig.traffic_light_id = input_roi.traffic_light_id;
135146
tlr_sig.traffic_light_type = input_roi.traffic_light_type;

0 commit comments

Comments
 (0)