Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(traffic_light_classifier): fix zero size roi bug #7608

Original file line number Diff line number Diff line change
Expand Up @@ -84,52 +84,63 @@
if (classifier_ptr_.use_count() == 0) {
return;
}
if (input_rois_msg->rois.empty()) {
return;
}

cv_bridge::CvImagePtr cv_ptr;
try {
cv_ptr = cv_bridge::toCvCopy(input_image_msg, sensor_msgs::image_encodings::RGB8);
} catch (cv_bridge::Exception & e) {
RCLCPP_ERROR(
this->get_logger(), "Could not convert from '%s' to 'rgb8'.",
input_image_msg->encoding.c_str());
}

tier4_perception_msgs::msg::TrafficLightArray output_msg;

output_msg.signals.resize(input_rois_msg->rois.size());

std::vector<cv::Mat> images;
std::vector<size_t> backlight_indices;
for (size_t i = 0; i < input_rois_msg->rois.size(); i++) {
// skip if the roi is not detected
if (input_rois_msg->rois.at(i).roi.height == 0) {
break;
const auto & input_roi = input_rois_msg->rois.at(i);

// ignore if the roi is not the type to be classified
if (input_roi.traffic_light_type != classify_traffic_light_type_) {
continue;

Check warning on line 110 in perception/autoware_traffic_light_classifier/src/traffic_light_classifier_node.cpp

View check run for this annotation

Codecov / codecov/patch

perception/autoware_traffic_light_classifier/src/traffic_light_classifier_node.cpp#L110

Added line #L110 was not covered by tests
}
if (input_rois_msg->rois.at(i).traffic_light_type != classify_traffic_light_type_) {
// skip if the roi size is zero
if (input_roi.roi.height == 0 || input_roi.roi.width == 0) {
continue;
}
output_msg.signals[images.size()].traffic_light_id =
input_rois_msg->rois.at(i).traffic_light_id;
output_msg.signals[images.size()].traffic_light_type =
input_rois_msg->rois.at(i).traffic_light_type;
const sensor_msgs::msg::RegionOfInterest & roi = input_rois_msg->rois.at(i).roi;

// set traffic light id and type
output_msg.signals[images.size()].traffic_light_id = input_roi.traffic_light_id;
output_msg.signals[images.size()].traffic_light_type = input_roi.traffic_light_type;

Check warning on line 119 in perception/autoware_traffic_light_classifier/src/traffic_light_classifier_node.cpp

View check run for this annotation

Codecov / codecov/patch

perception/autoware_traffic_light_classifier/src/traffic_light_classifier_node.cpp#L118-L119

Added lines #L118 - L119 were not covered by tests
yukkysaito marked this conversation as resolved.
Show resolved Hide resolved

const sensor_msgs::msg::RegionOfInterest & roi = input_roi.roi;
auto roi_img = cv_ptr->image(cv::Rect(roi.x_offset, roi.y_offset, roi.width, roi.height));
if (is_harsh_backlight(roi_img)) {
backlight_indices.emplace_back(i);
technolojin marked this conversation as resolved.
Show resolved Hide resolved
}
images.emplace_back(roi_img);
}

// classify the images
output_msg.signals.resize(images.size());
if (!classifier_ptr_->getTrafficSignals(images, output_msg)) {
RCLCPP_ERROR(this->get_logger(), "failed classify image, abort callback");
return;
if (!images.empty()) {
if (!classifier_ptr_->getTrafficSignals(images, output_msg)) {
RCLCPP_ERROR(this->get_logger(), "failed classify image, abort callback");

Check warning on line 133 in perception/autoware_traffic_light_classifier/src/traffic_light_classifier_node.cpp

View check run for this annotation

Codecov / codecov/patch

perception/autoware_traffic_light_classifier/src/traffic_light_classifier_node.cpp#L133

Added line #L133 was not covered by tests
return;
}
}

// append the undetected rois as unknown
for (const auto & input_roi : input_rois_msg->rois) {
if (input_roi.roi.height == 0 && input_roi.traffic_light_type == classify_traffic_light_type_) {
// if the type is the target type but the roi size is zero, the roi is undetected
if (
(input_roi.roi.height == 0 || input_roi.roi.width == 0) &&

Check warning on line 142 in perception/autoware_traffic_light_classifier/src/traffic_light_classifier_node.cpp

View check run for this annotation

CodeScene Delta Analysis / CodeScene Cloud Delta Analysis (main)

❌ New issue: Complex Conditional

TrafficLightClassifierNodelet::imageRoiCallback has 1 complex conditionals with 2 branches, threshold = 2. A complex conditional is an expression inside a branch (e.g. if, for, while) which consists of multiple, logical operators such as AND/OR. The more logical operators in an expression, the more severe the code smell.
input_roi.traffic_light_type == classify_traffic_light_type_) {

Check warning on line 143 in perception/autoware_traffic_light_classifier/src/traffic_light_classifier_node.cpp

View check run for this annotation

CodeScene Delta Analysis / CodeScene Cloud Delta Analysis (main)

❌ Getting worse: Complex Method

TrafficLightClassifierNodelet::imageRoiCallback increases in cyclomatic complexity from 14 to 19, threshold = 9. This function has many conditional statements (e.g. if, for, while), leading to lower code health. Avoid adding more conditionals and code to it without refactoring.

Check notice on line 143 in perception/autoware_traffic_light_classifier/src/traffic_light_classifier_node.cpp

View check run for this annotation

CodeScene Delta Analysis / CodeScene Cloud Delta Analysis (main)

ℹ Getting worse: Bumpy Road Ahead

TrafficLightClassifierNodelet::imageRoiCallback increases from 3 to 4 logical blocks with deeply nested code, threshold is one single block per function. The Bumpy Road code smell is a function that contains multiple chunks of nested conditional logic. The deeper the nesting and the more bumps, the lower the code health.

Check warning on line 143 in perception/autoware_traffic_light_classifier/src/traffic_light_classifier_node.cpp

View check run for this annotation

Codecov / codecov/patch

perception/autoware_traffic_light_classifier/src/traffic_light_classifier_node.cpp#L141-L143

Added lines #L141 - L143 were not covered by tests
tier4_perception_msgs::msg::TrafficLight tlr_sig;
tlr_sig.traffic_light_id = input_roi.traffic_light_id;
tlr_sig.traffic_light_type = input_roi.traffic_light_type;
Expand Down
Loading