Skip to content

Commit fe1dbfe

Browse files
ktro2828vividf
authored andcommitted
fix(autoware_traffic_light_visualization): fix to visualize correct color and shapes (autowarefoundation#8428)
fix(autoware_traffic_light_visualization): fix vialization to draw correct shapes Signed-off-by: ktro2828 <kotaro.uetake@tier4.jp> Co-authored-by: Yi-Hsiang Fang (Vivid) <146902905+vividf@users.noreply.github.com>
1 parent fdd665d commit fe1dbfe

File tree

2 files changed

+64
-21
lines changed
  • perception/traffic_light_visualization
    • include/traffic_light_visualization/traffic_light_roi_visualizer
    • src/traffic_light_roi_visualizer

2 files changed

+64
-21
lines changed

perception/traffic_light_visualization/include/traffic_light_visualization/traffic_light_roi_visualizer/nodelet.hpp

+58-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,9 @@
3232
#include <map>
3333
#include <memory>
3434
#include <mutex>
35+
#include <sstream>
3536
#include <string>
37+
#include <vector>
3638

3739
namespace traffic_light
3840
{
@@ -42,7 +44,16 @@ struct ClassificationResult
4244
std::string label;
4345
};
4446

45-
class TrafficLightRoiVisualizerNodelet : public rclcpp::Node
47+
/**
48+
* @brief A struct to represent parsed traffic light shape information.
49+
*/
50+
struct TrafficLightShapeInfo
51+
{
52+
cv::Scalar color; //!< Color associated with "circle".
53+
std::vector<std::string> shapes; //!< Shape names.
54+
};
55+
56+
class TrafficLightRoiVisualizerNode : public rclcpp::Node
4657
{
4758
public:
4859
explicit TrafficLightRoiVisualizerNodelet(const rclcpp::NodeOptions & options);
@@ -74,13 +85,59 @@ class TrafficLightRoiVisualizerNodelet : public rclcpp::Node
7485
{tier4_perception_msgs::msg::TrafficLightElement::RIGHT_ARROW, "right"},
7586
{tier4_perception_msgs::msg::TrafficLightElement::UP_ARROW, "straight"},
7687
{tier4_perception_msgs::msg::TrafficLightElement::DOWN_ARROW, "down"},
88+
{tier4_perception_msgs::msg::TrafficLightElement::UP_LEFT_ARROW, "straight_left"},
89+
{tier4_perception_msgs::msg::TrafficLightElement::UP_RIGHT_ARROW, "straight_right"},
7790
{tier4_perception_msgs::msg::TrafficLightElement::DOWN_LEFT_ARROW, "down_left"},
7891
{tier4_perception_msgs::msg::TrafficLightElement::DOWN_RIGHT_ARROW, "down_right"},
7992
{tier4_perception_msgs::msg::TrafficLightElement::CROSS, "cross"},
8093
// other
8194
{tier4_perception_msgs::msg::TrafficLightElement::UNKNOWN, "unknown"},
8295
};
8396

97+
/**
98+
* @brief Return RGB color from color string associated with "circle".
99+
* @param color Color string.
100+
* @return RGB color.
101+
*/
102+
static cv::Scalar strToColor(const std::string & color)
103+
{
104+
if (color == "red") {
105+
return {254, 149, 149};
106+
} else if (color == "yellow") {
107+
return {254, 250, 149};
108+
} else if (color == "green") {
109+
return {149, 254, 161};
110+
} else {
111+
return {250, 250, 250};
112+
}
113+
}
114+
115+
/**
116+
* @brief Extract color and shape names from label.
117+
* @param label String formatted as `<Color0>-<Shape0>,<Color1>-<Shape1>,...,<ColorN>-<ShapeN>`.
118+
* @return Extracted information includes a color associated with "circle" and shape names.
119+
*/
120+
static TrafficLightShapeInfo extractShapeInfo(const std::string & label)
121+
{
122+
cv::Scalar color{255, 255, 255};
123+
std::vector<std::string> shapes;
124+
125+
std::stringstream ss(label);
126+
std::string segment;
127+
while (std::getline(ss, segment, ',')) {
128+
size_t hyphen_pos = segment.find('-');
129+
if (hyphen_pos != std::string::npos) {
130+
auto shape = segment.substr(hyphen_pos + 1);
131+
if (shape == "circle") {
132+
const auto color_str = segment.substr(0, hyphen_pos);
133+
color = strToColor(color_str);
134+
}
135+
shapes.emplace_back(shape);
136+
}
137+
}
138+
return {color, shapes};
139+
}
140+
84141
bool createRect(
85142
cv::Mat & image, const tier4_perception_msgs::msg::TrafficLightRoi & tl_roi,
86143
const cv::Scalar & color);

perception/traffic_light_visualization/src/traffic_light_roi_visualizer/nodelet.cpp

+6-20
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919

2020
#include <memory>
2121
#include <string>
22-
#include <utility>
2322

2423
namespace traffic_light
2524
{
@@ -91,31 +90,18 @@ bool TrafficLightRoiVisualizerNodelet::createRect(
9190
cv::Mat & image, const tier4_perception_msgs::msg::TrafficLightRoi & tl_roi,
9291
const ClassificationResult & result)
9392
{
94-
cv::Scalar color;
95-
if (result.label.find("red") != std::string::npos) {
96-
color = cv::Scalar{255, 0, 0};
97-
} else if (result.label.find("yellow") != std::string::npos) {
98-
color = cv::Scalar{0, 255, 0};
99-
} else if (result.label.find("green") != std::string::npos) {
100-
color = cv::Scalar{0, 0, 255};
101-
} else {
102-
color = cv::Scalar{255, 255, 255};
103-
}
93+
const auto info = extractShapeInfo(result.label);
10494

10595
cv::rectangle(
10696
image, cv::Point(tl_roi.roi.x_offset, tl_roi.roi.y_offset),
10797
cv::Point(tl_roi.roi.x_offset + tl_roi.roi.width, tl_roi.roi.y_offset + tl_roi.roi.height),
108-
color, 3);
98+
info.color, 2);
10999

110-
int offset = 40;
111-
cv::putText(
112-
image, std::to_string(result.prob),
113-
cv::Point(tl_roi.roi.x_offset, tl_roi.roi.y_offset - (offset * 0)), cv::FONT_HERSHEY_COMPLEX,
114-
1.1, color, 3);
100+
constexpr int shape_img_size = 16;
101+
const auto position = cv::Point(tl_roi.roi.x_offset, tl_roi.roi.y_offset);
115102

116-
cv::putText(
117-
image, result.label, cv::Point(tl_roi.roi.x_offset, tl_roi.roi.y_offset - (offset * 1)),
118-
cv::FONT_HERSHEY_COMPLEX, 1.1, color, 2);
103+
visualization::drawTrafficLightShape(
104+
image, info.shapes, shape_img_size, position, info.color, result.prob);
119105

120106
return true;
121107
}

0 commit comments

Comments
 (0)