Skip to content
22 changes: 8 additions & 14 deletions src/detection/src/detection_node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,6 @@ double angle(Point2f a, Point2f b);
vector<vector<Point>> getContours(Mat& frame, const string& color);
vector<Point2f> calculateCenters(Mat& frame, const string& color, bool draw = false, bool debug = false);

Mat applyCanny(Mat& frame) {
Mat edges;
GaussianBlur(frame, frame, Size(5, 5), 1.5);
// Canny with thresholds 100 and 200
Canny(frame, edges, 100, 200);
Mat edgesColor;
cvtColor(edges, edgesColor, COLOR_GRAY2BGR);
return edgesColor;
}

class CVNode : public rclcpp::Node {
public:
CVNode() : Node("CVNode"), writer_initialized(false), last_frame_time(this->now()) {
Expand Down Expand Up @@ -59,15 +49,17 @@ class CVNode : public rclcpp::Node {
bool flag_write_video_;
std::string output_video_path_;

int frame_number = 0;

void topic_callback(const sensor_msgs::msg::CompressedImage &msg) {
try {

frame_number ++;

cv_bridge::CvImagePtr cv_ptr = cv_bridge::toCvCopy(msg, sensor_msgs::image_encodings::BGR8);
Mat frame = cv_ptr->image;

Mat edges = applyCanny(frame);

vector<Point2f> centers = calculateCenters(frame, "blue", flag_write_video_, flag_debug_);
vector<Point2f> centers = calculateCenters(frame, "red", flag_write_video_, flag_debug_);

Detection2DArray detections_msg;
detections_msg.header = msg.header;
Expand All @@ -76,12 +68,14 @@ class CVNode : public rclcpp::Node {
Detection2D detection;
detection.bbox.center.position.x = center.x;
detection.bbox.center.position.y = center.y;
RCLCPP_INFO(this->get_logger(), "Frame Number: %d, Detection - x: %.2f, y: %.2f", frame_number, center.x, center.y);
detections_msg.detections.push_back(detection);
}

detections_publisher_->publish(detections_msg);

RCLCPP_INFO(this->get_logger(), "Published %zu detections", detections_msg.detections.size());
// RCLCPP_INFO(this->get_logger(), "Frame Number: %d, Published %zu detections", frame_number, detections_msg.detections.size());


// Update the last received frame timestamp
last_frame_time = this->now();
Expand Down
43 changes: 43 additions & 0 deletions src/tracking/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
cmake_minimum_required(VERSION 3.8)
project(tracking)

if(CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID MATCHES "Clang")
add_compile_options(-Wall -Wextra -Wpedantic)
endif()

# find dependencies
find_package(ament_cmake REQUIRED)
find_package(rclcpp REQUIRED)
find_package(vision_msgs REQUIRED)
find_package(geometry_msgs REQUIRED)
find_package(cv_bridge REQUIRED)
find_package(OpenCV REQUIRED)
find_package(message_filters REQUIRED)

# uncomment the following section in order to fill in
# further dependencies manually.
# find_package(<dependency> REQUIRED)

add_executable(tracking_node src/tracking_node.cpp)
ament_target_dependencies(tracking_node rclcpp vision_msgs geometry_msgs cv_bridge OpenCV message_filters)
target_include_directories(tracking_node PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:include>)
target_compile_features(tracking_node PUBLIC c_std_99 cxx_std_17) # Require C99 and C++17

install(TARGETS tracking_node
DESTINATION lib/${PROJECT_NAME})

if(BUILD_TESTING)
find_package(ament_lint_auto REQUIRED)
# the following line skips the linter which checks for copyrights
# comment the line when a copyright and license is added to all source files
set(ament_cmake_copyright_FOUND TRUE)
# the following line skips cpplint (only works in a git repo)
# comment the line when this package is in a git repo and when
# a copyright and license is added to all source files
set(ament_cmake_cpplint_FOUND TRUE)
ament_lint_auto_find_test_dependencies()
endif()

ament_package()
24 changes: 24 additions & 0 deletions src/tracking/package.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?xml version="1.0"?>
<?xml-model href="http://download.ros.org/schema/package_format3.xsd" schematypens="http://www.w3.org/2001/XMLSchema"?>
<package format="3">
<name>tracking</name>
<version>0.0.0</version>
<description>TODO: Package description</description>
<maintainer email="[email protected]">endian</maintainer>
<license>TODO: License declaration</license>

<buildtool_depend>ament_cmake</buildtool_depend>
<depend>rclcpp</depend>
<depend>vision_msgs</depend>
<depend>geometry_msgs</depend>
<depend>cv_bridge</depend>
<depend>opencv2</depend>
<depend>message_filters</depend>

<test_depend>ament_lint_auto</test_depend>
<test_depend>ament_lint_common</test_depend>

<export>
<build_type>ament_cmake</build_type>
</export>
</package>
Loading