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

feat: add support of overwriting signals if harsh backlight is detected #5852

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -42,87 +42,98 @@
add_launch_arg("input/image", "/sensing/camera/traffic_light/image_raw")
add_launch_arg("output/rois", "/perception/traffic_light_recognition/rois")
add_launch_arg(
"output/traffic_signals", "/perception/traffic_light_recognition/traffic_signals"
"output/traffic_signals",
"/perception/traffic_light_recognition/traffic_signals",
)

# traffic_light_fine_detector
add_launch_arg(
"fine_detector_model_path",
os.path.join(fine_detector_share_dir, "data", "tlr_yolox_s.onnx"),
)
add_launch_arg(
"fine_detector_label_path",
os.path.join(fine_detector_share_dir, "data", "tlr_labels.txt"),
)
add_launch_arg("fine_detector_precision", "fp16")
add_launch_arg("fine_detector_score_thresh", "0.3")
add_launch_arg("fine_detector_nms_thresh", "0.65")

add_launch_arg("approximate_sync", "False")

# traffic_light_classifier
add_launch_arg("classifier_type", "1")
add_launch_arg(
"classifier_model_path",
os.path.join(classifier_share_dir, "data", "traffic_light_classifier_efficientNet_b1.onnx"),
os.path.join(
classifier_share_dir,
"data",
"traffic_light_classifier_efficientNet_b1.onnx",
),
)
add_launch_arg(
"classifier_label_path", os.path.join(classifier_share_dir, "data", "lamp_labels.txt")
"classifier_label_path",
os.path.join(classifier_share_dir, "data", "lamp_labels.txt"),
)
add_launch_arg("classifier_precision", "fp16")
add_launch_arg("classifier_mean", "[123.675, 116.28, 103.53]")
add_launch_arg("classifier_std", "[58.395, 57.12, 57.375]")
add_launch_arg("backlight_threshold", "0.85")

add_launch_arg("use_intra_process", "False")
add_launch_arg("use_multithread", "False")

def create_parameter_dict(*args):
result = {}
for x in args:
result[x] = LaunchConfiguration(x)
return result

container = ComposableNodeContainer(
name="traffic_light_node_container",
namespace="",
package="rclcpp_components",
executable=LaunchConfiguration("container_executable"),
composable_node_descriptions=[
ComposableNode(
package="traffic_light_classifier",
plugin="traffic_light::TrafficLightClassifierNodelet",
name="traffic_light_classifier",
namespace="classification",
parameters=[
create_parameter_dict(
"approximate_sync",
"classifier_type",
"classifier_model_path",
"classifier_label_path",
"classifier_precision",
"classifier_mean",
"classifier_std",
"backlight_threshold",
)
],
remappings=[
("~/input/image", LaunchConfiguration("input/image")),
("~/input/rois", LaunchConfiguration("output/rois")),
("~/output/traffic_signals", "classified/traffic_signals"),
],
extra_arguments=[
{"use_intra_process_comms": LaunchConfiguration("use_intra_process")}
],
),
ComposableNode(
package="traffic_light_visualization",
plugin="traffic_light::TrafficLightRoiVisualizerNodelet",
name="traffic_light_roi_visualizer",
parameters=[create_parameter_dict("enable_fine_detection")],
remappings=[
("~/input/image", LaunchConfiguration("input/image")),
("~/input/rois", LaunchConfiguration("output/rois")),
("~/input/rough/rois", "detection/rough/rois"),
("~/input/traffic_signals", LaunchConfiguration("output/traffic_signals")),
(
"~/input/traffic_signals",
LaunchConfiguration("output/traffic_signals"),
),

Check warning on line 136 in launch/tier4_perception_launch/launch/traffic_light_recognition/traffic_light_node_container.launch.py

View check run for this annotation

CodeScene Delta Analysis / CodeScene Cloud Delta Analysis (main)

❌ Getting worse: Large Method

generate_launch_description increases from 169 to 180 lines of code, threshold = 70. Large functions with many lines of code are generally harder to understand and lower the code health. Avoid adding more lines to this function.
("~/output/image", "debug/rois"),
("~/output/image/compressed", "debug/rois/compressed"),
("~/output/image/compressedDepth", "debug/rois/compressedDepth"),
Expand Down
9 changes: 5 additions & 4 deletions perception/traffic_light_classifier/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,11 @@ These colors and shapes are assigned to the message as follows:

### Node Parameters

| Name | Type | Description |
| ----------------- | ---- | ------------------------------------------- |
| `classifier_type` | int | if the value is `1`, cnn_classifier is used |
| `data_path` | str | packages data and artifacts directory path |
| Name | Type | Description |
| --------------------- | ----- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `classifier_type` | int | if the value is `1`, cnn_classifier is used |
| `data_path` | str | packages data and artifacts directory path |
| `backlight_threshold` | float | If the intensity get grater than this overwrite with UNKNOWN in corresponding RoI. Note that, if the value is much higher, the node only overwrites in the harsher backlight situations. Therefore, If you wouldn't like to use this feature set this value to `1.0`. The value can be `[0.0, 1.0]`. The confidence of overwritten signal is set to `0.0`. |

### Core Parameters

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,9 @@ class TrafficLightClassifierNodelet : public rclcpp::Node
rclcpp::Publisher<tier4_perception_msgs::msg::TrafficSignalArray>::SharedPtr
traffic_signal_array_pub_;
std::shared_ptr<ClassifierInterface> classifier_ptr_;

double backlight_threshold_;
bool is_harsh_backlight(const cv::Mat & img) const;
};

} // namespace traffic_light
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
<arg name="classifier_type" default="0" unless="$(var use_gpu)"/>

<arg name="build_only" default="false" description="shutdown node after TensorRT engine file is built"/>
<arg name="backlight_threshold" default="0.85" description="overwrite signals if the RoI image intensity is greater than this value"/>
miursh marked this conversation as resolved.
Show resolved Hide resolved

<node pkg="traffic_light_classifier" exec="traffic_light_classifier_node" name="traffic_light_classifier">
<remap from="~/input/image" to="$(var input/image)"/>
Expand All @@ -24,5 +25,6 @@
<param name="classifier_model_path" value="$(var classifier_model_path)"/>
<param name="classifier_precision" value="$(var classifier_precision)"/>
<param name="build_only" value="$(var build_only)"/>
<param name="backlight_threshold" value="$(var backlight_threshold)"/>
</node>
</launch>
30 changes: 29 additions & 1 deletion perception/traffic_light_classifier/src/nodelet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
using std::placeholders::_1;
using std::placeholders::_2;
is_approximate_sync_ = this->declare_parameter("approximate_sync", false);
backlight_threshold_ = this->declare_parameter<double>("backlight_threshold");

Check warning on line 29 in perception/traffic_light_classifier/src/nodelet.cpp

View check run for this annotation

Codecov / codecov/patch

perception/traffic_light_classifier/src/nodelet.cpp#L29

Added line #L29 was not covered by tests

if (is_approximate_sync_) {
approximate_sync_.reset(new ApproximateSync(ApproximateSyncPolicy(10), image_sub_, roi_sub_));
approximate_sync_->registerCallback(
Expand Down Expand Up @@ -94,19 +96,45 @@
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++) {
output_msg.signals[i].traffic_light_id = input_rois_msg->rois.at(i).traffic_light_id;
const sensor_msgs::msg::RegionOfInterest & roi = input_rois_msg->rois.at(i).roi;
images.emplace_back(cv_ptr->image, cv::Rect(roi.x_offset, roi.y_offset, roi.width, roi.height));
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);

Check warning on line 105 in perception/traffic_light_classifier/src/nodelet.cpp

View check run for this annotation

Codecov / codecov/patch

perception/traffic_light_classifier/src/nodelet.cpp#L103-L105

Added lines #L103 - L105 were not covered by tests
}
images.emplace_back(roi_img);

Check warning on line 107 in perception/traffic_light_classifier/src/nodelet.cpp

View check run for this annotation

Codecov / codecov/patch

perception/traffic_light_classifier/src/nodelet.cpp#L107

Added line #L107 was not covered by tests
}
if (!classifier_ptr_->getTrafficSignals(images, output_msg)) {
RCLCPP_ERROR(this->get_logger(), "failed classify image, abort callback");
return;
}

for (const auto & idx : backlight_indices) {
auto & elements = output_msg.signals.at(idx).elements;
for (auto & element : elements) {
element.color = tier4_perception_msgs::msg::TrafficLightElement::UNKNOWN;
element.shape = tier4_perception_msgs::msg::TrafficLightElement::UNKNOWN;
element.confidence = 0.0;

Check warning on line 119 in perception/traffic_light_classifier/src/nodelet.cpp

View check run for this annotation

Codecov / codecov/patch

perception/traffic_light_classifier/src/nodelet.cpp#L114-L119

Added lines #L114 - L119 were not covered by tests
}
miursh marked this conversation as resolved.
Show resolved Hide resolved
}

Check warning on line 122 in perception/traffic_light_classifier/src/nodelet.cpp

View check run for this annotation

CodeScene Delta Analysis / CodeScene Cloud Delta Analysis (main)

❌ New issue: Bumpy Road Ahead

TrafficLightClassifierNodelet::imageRoiCallback has 2 blocks with nested conditional logic. Any nesting of 2 or deeper is considered. Threshold is one single, nested 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.
output_msg.header = input_image_msg->header;
traffic_signal_array_pub_->publish(output_msg);
}

bool TrafficLightClassifierNodelet::is_harsh_backlight(const cv::Mat & img) const

Check warning on line 127 in perception/traffic_light_classifier/src/nodelet.cpp

View check run for this annotation

Codecov / codecov/patch

perception/traffic_light_classifier/src/nodelet.cpp#L127

Added line #L127 was not covered by tests
{
cv::Mat y_cr_cb;
cv::cvtColor(img, y_cr_cb, cv::COLOR_RGB2YCrCb);

Check warning on line 130 in perception/traffic_light_classifier/src/nodelet.cpp

View check run for this annotation

Codecov / codecov/patch

perception/traffic_light_classifier/src/nodelet.cpp#L129-L130

Added lines #L129 - L130 were not covered by tests

const cv::Scalar mean_values = cv::mean(y_cr_cb);
const double intensity = (mean_values[0] - 112.5) / 112.5;

Check warning on line 133 in perception/traffic_light_classifier/src/nodelet.cpp

View check run for this annotation

Codecov / codecov/patch

perception/traffic_light_classifier/src/nodelet.cpp#L132-L133

Added lines #L132 - L133 were not covered by tests

return backlight_threshold_ <= intensity;
}

Check warning on line 136 in perception/traffic_light_classifier/src/nodelet.cpp

View check run for this annotation

Codecov / codecov/patch

perception/traffic_light_classifier/src/nodelet.cpp#L135-L136

Added lines #L135 - L136 were not covered by tests

} // namespace traffic_light

#include <rclcpp_components/register_node_macro.hpp>
Expand Down
Loading