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: update trafficlight roi styling #6985

Merged
merged 22 commits into from
Jun 28, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
32b4289
traffic light shape drawing header file
KhalilSelyan May 11, 2024
f77e862
shape cv arrowline/circle/cross
KhalilSelyan May 11, 2024
8406008
add shape_draw to cmakelists
KhalilSelyan May 11, 2024
4d0397a
fix colors, proba position/precision, text into shapes
KhalilSelyan May 11, 2024
06af046
add copyright
KhalilSelyan May 12, 2024
1cead03
add shape icons
KhalilSelyan May 13, 2024
5b641b8
add images directory to installation
KhalilSelyan May 13, 2024
8b33341
add general draw function using images
KhalilSelyan May 13, 2024
9fc1daa
refactor unnecessary code
KhalilSelyan May 13, 2024
abbfb90
style(pre-commit): autofix
pre-commit-ci[bot] May 13, 2024
a813a7f
add shapename extractor function, less thick rect
KhalilSelyan May 13, 2024
4f1836b
better contained shape/prob
KhalilSelyan May 13, 2024
c352fff
Merge branch 'main' into style/trafficlight-roi-styling
kminoda May 17, 2024
634da98
Merge branch 'main' into style/trafficlight-roi-styling
KhalilSelyan May 27, 2024
b6ba188
refactor(traffic_light_roi_visualizer): update color values for traff…
KhalilSelyan May 27, 2024
2ac8b56
refactor(traffic_light_roi_visualizer): adjust shape dimensions and f…
KhalilSelyan May 27, 2024
32fcce8
Merge branch 'main' into style/trafficlight-roi-styling
KhalilSelyan May 31, 2024
4534835
add unknown symbol
KhalilSelyan May 31, 2024
2cdee33
feat: add scale_factor parameter to drawShape function
KhalilSelyan May 31, 2024
7080bb2
feat: add scale_factor parameter to drawShape function and no text on…
KhalilSelyan May 31, 2024
dc98ada
refactor: adjust scale_factor parameter in drawShape function
KhalilSelyan May 31, 2024
e8085e5
Merge branch 'main' into style/trafficlight-roi-styling
kminoda Jun 28, 2024
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
1 change: 1 addition & 0 deletions perception/traffic_light_visualization/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ find_package(OpenCV REQUIRED)

ament_auto_add_library(traffic_light_roi_visualizer_nodelet SHARED
src/traffic_light_roi_visualizer/nodelet.cpp
src/traffic_light_roi_visualizer/shape_draw.cpp
)

target_link_libraries(traffic_light_roi_visualizer_nodelet
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// Copyright 2024 The Autoware Contributors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#pragma once
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/opencv.hpp>

#include <cv_bridge/cv_bridge.h>
#include <opencv2/imgproc/imgproc_c.h>

#include <functional>
#include <map>

struct DrawFunctionParams
{
cv::Mat & image;
cv::Point position;
cv::Scalar color;
int size;
};

using DrawFunction = std::function<void(const DrawFunctionParams & params)>;

void drawCircle(const DrawFunctionParams & params);
void drawLeftArrow(const DrawFunctionParams & params);
void drawRightArrow(const DrawFunctionParams & params);
void drawStraightArrow(const DrawFunctionParams & params);
void drawDownArrow(const DrawFunctionParams & params);
void drawDownLeftArrow(const DrawFunctionParams & params);
void drawDownRightArrow(const DrawFunctionParams & params);
void drawCross(const DrawFunctionParams & params);
void drawTrafficLightShape(
cv::Mat & image, const std::string & shape, const cv::Point & position, const cv::Scalar & color,
int size);
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2020 Tier IV, Inc.

Check notice on line 1 in perception/traffic_light_visualization/src/traffic_light_roi_visualizer/nodelet.cpp

View check run for this annotation

CodeScene Delta Analysis / CodeScene Cloud Delta Analysis (main)

ℹ Getting worse: Overall Code Complexity

The mean cyclomatic complexity increases from 4.25 to 4.50, threshold = 4. This file has many conditional statements (e.g. if, for, while) across its implementation, leading to lower code health. Avoid adding more conditionals.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand All @@ -14,6 +14,8 @@

#include "traffic_light_visualization/traffic_light_roi_visualizer/nodelet.hpp" // NOLINT(whitespace/line_length)

#include "traffic_light_visualization/traffic_light_roi_visualizer/shape_draw.hpp" // NOLINT(whitespace/line_length)

#include <rclcpp/rclcpp.hpp>
#include <rclcpp_components/register_node_macro.hpp>

Expand Down Expand Up @@ -95,9 +97,9 @@
if (result.label.find("red") != std::string::npos) {
color = cv::Scalar{255, 0, 0};
} else if (result.label.find("yellow") != std::string::npos) {
color = cv::Scalar{0, 255, 0};
color = cv::Scalar{255, 255, 0};
} else if (result.label.find("green") != std::string::npos) {
color = cv::Scalar{0, 0, 255};
color = cv::Scalar{0, 255, 0};
} else {
color = cv::Scalar{255, 255, 255};
}
KhalilSelyan marked this conversation as resolved.
Show resolved Hide resolved
Expand All @@ -107,15 +109,29 @@
cv::Point(tl_roi.roi.x_offset + tl_roi.roi.width, tl_roi.roi.y_offset + tl_roi.roi.height),
color, 3);

int offset = 40;
int y_offset = 10;
int x_offset = 15;
cv::putText(
image, std::to_string(result.prob),
cv::Point(tl_roi.roi.x_offset, tl_roi.roi.y_offset - (offset * 0)), cv::FONT_HERSHEY_COMPLEX,
1.1, color, 3);

cv::putText(
image, result.label, cv::Point(tl_roi.roi.x_offset, tl_roi.roi.y_offset - (offset * 1)),
cv::FONT_HERSHEY_COMPLEX, 1.1, color, 2);
image, std::to_string(static_cast<int>(round(result.prob * 100))),
cv::Point(
tl_roi.roi.x_offset + tl_roi.roi.width + (x_offset * 0.5),
tl_roi.roi.y_offset + (y_offset * 0.75)),
cv::FONT_HERSHEY_COMPLEX, 0.5, color, 2);

std::string shape_name = result.label.find('-') != std::string::npos
? result.label.substr(result.label.find('-') + 1)
: "unknown";

if (shape_name != "unknown") {
drawTrafficLightShape(
image, shape_name, cv::Point(tl_roi.roi.x_offset + (x_offset * 1), tl_roi.roi.y_offset),
color, 16);
} else {
cv::putText(
image, shape_name,
cv::Point(tl_roi.roi.x_offset + (x_offset * 0), tl_roi.roi.y_offset - (y_offset * 0.75)),
cv::FONT_HERSHEY_COMPLEX, 0.5, color, 2);
}

return true;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
// Copyright 2024 The Autoware Contributors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "traffic_light_visualization/traffic_light_roi_visualizer/shape_draw.hpp"

#include "opencv2/core/types.hpp"

void drawCircle(const DrawFunctionParams & params)
{
int y_offset = params.size / 2 + 10;
cv::circle(
params.image, cv::Point(params.position.x, params.position.y - y_offset), params.size / 2.0,
params.color, -1); // Filled circle
}

void drawLeftArrow(const DrawFunctionParams & params)
{
int y_offset = params.size / 2 + 10;
// Start from the right, go to the left (start x > end x)
cv::arrowedLine(
params.image, cv::Point(params.position.x, params.position.y - y_offset), // Start point
cv::Point(
params.position.x - params.size, params.position.y - y_offset), // End point to the left
params.color, 2, 8, 0, 0.3);
}

void drawRightArrow(const DrawFunctionParams & params)
{
int y_offset = params.size / 2 + 10;
// Start from the left, go to the right (start x < end x)
cv::arrowedLine(
params.image, cv::Point(params.position.x, params.position.y - y_offset), // Start point
cv::Point(
params.position.x + params.size, params.position.y - y_offset), // End point to the right
params.color, 2, 8, 0, 0.3);
}

void drawStraightArrow(const DrawFunctionParams & params)
{
int y_offset = params.size / 2 + 10; // This adjusts the base position upwards
// Start point is lower, and it goes upwards to a higher (smaller y-value) point
cv::arrowedLine(
params.image, cv::Point(params.position.x, params.position.y - y_offset), // Start point
cv::Point(params.position.x, params.position.y - params.size - y_offset), // End point
params.color, 2, 8, 0, 0.3);
}
void drawDownArrow(const DrawFunctionParams & params)
{
int y_offset = params.size / 2 + 10; // This adjusts the base position upwards
// Start point is higher, and it goes downwards to a lower (larger y-value) point
cv::arrowedLine(
params.image, cv::Point(params.position.x, params.position.y - y_offset), // Start point
cv::Point(params.position.x, params.position.y + params.size - y_offset), // End point
params.color, 2, 8, 0, 0.3);
}

void drawDownLeftArrow(const DrawFunctionParams & params)
{
int y_offset = params.size / 2 + 15;
// Down-left arrow
cv::arrowedLine(
params.image, cv::Point(params.position.x, params.position.y - y_offset),
cv::Point(params.position.x - params.size, params.position.y + params.size - y_offset),
params.color, 2, 8, 0, 0.3);
}

void drawDownRightArrow(const DrawFunctionParams & params)
{
int y_offset = params.size / 2 + 15;
// Down-right arrow
cv::arrowedLine(
params.image, cv::Point(params.position.x, params.position.y - y_offset),
cv::Point(params.position.x + params.size, params.position.y + params.size - y_offset),
params.color, 2, 8, 0, 0.3);
}

void drawCross(const DrawFunctionParams & params)
{
int y_offset = params.size / 2 + 10;
// Cross (two lines intersecting)
int half_size = params.size / 2;
cv::line(
params.image,
cv::Point(params.position.x - half_size, params.position.y - half_size - y_offset),
cv::Point(params.position.x + half_size, params.position.y + half_size - y_offset),
params.color, 2);
cv::line(
params.image,
cv::Point(params.position.x + half_size, params.position.y - half_size - y_offset),
cv::Point(params.position.x - half_size, params.position.y + half_size - y_offset),
params.color, 2);
}

void drawTrafficLightShape(
cv::Mat & image, const std::string & shape, const cv::Point & position, const cv::Scalar & color,
int size)
{
static std::map<std::string, DrawFunction> shapeToFunction = {
{"circle", drawCircle},
xmfcx marked this conversation as resolved.
Show resolved Hide resolved
{"left", drawLeftArrow},
{"right", drawRightArrow},
{"straight", drawStraightArrow},
{"down", drawDownArrow},
{"down_left", drawDownLeftArrow},
{"down_right", drawDownRightArrow},
{"cross", drawCross}};
auto it = shapeToFunction.find(shape);
if (it != shapeToFunction.end()) {
DrawFunctionParams params{image, position, color, size};
it->second(params);
} else {
std::cerr << "Unknown shape: " << shape << std::endl;
}
}

Check warning on line 124 in perception/traffic_light_visualization/src/traffic_light_roi_visualizer/shape_draw.cpp

View check run for this annotation

CodeScene Delta Analysis / CodeScene Cloud Delta Analysis (main)

❌ New issue: Excess Number of Function Arguments

drawTrafficLightShape has 5 arguments, threshold = 4. This function has too many arguments, indicating a lack of encapsulation. Avoid adding more arguments.
Loading