This repository contains ros2 packages.
This project implements real-time object detection and tracking using ROS2 (Humble). The system captures images from a live camera or video, processes them through a YOLO V8 model, and tracks objects using a Centroid Tracking Algorithm.
This project consists of the following ROS2 nodes:
- Publishes:
/camera/image_raw - Streams images from a live camera or video file.
- Subscribes:
/camera/image_raw - Publishes:
/object_detection - Runs YOLOv8 to detect objects and outputs bounding boxes.
- Subscribes:
/object_detection - Publishes:
/object_tracking - Tracks objects across frames using Centroid Tracking.
- Subscribes:
/camera/image_raw,/object_detection,/object_tracking - Publishes:
/visualization/image - Overlays detections and tracking results on images.
Real-Time-Object-Tracking-ROS2/src/
│── object_tracking_viz/
│ ├── config/ # YAML configuration files
│ │ ├── params.yaml # ROS2 parameters (camera, video file, tracking settings)
│ ├── include/object_tracking_viz/ # Cpp header files
│ │ ├── header.cpp
│ ├── launch/ # ROS2 launch files
│ │ ├── app.launch.py # Launch all nodes
│ ├── media/ # Sample video files for testing
│ │ ├── test1.mp4
│ │ ├── test2.mp4
│ │ ├── test3.mp4
│ ├── object_tracking_viz/ # Python packages
│ │ ├── __init__.py
│ ├── scripts/ # Python scripts
│ │ ├── image_subscriber.py # Subscribes images and detect objects using YOLOv8
│ │ ├── object_visualization.py # Overlays tracking & detection results
│ ├── src/ # C++ source files
│ │ ├── image_publisher.cpp # Publishes images from camera or video
│ │ ├── object_tracking.cpp # Tracks objects using Centroid Tracking
│ ├── CMakeLists.txt # ROS2 build configuration
│ ├── package.xml # Package dependencies
│── .gitignore
│── README.md # Project documentation
Clone this repository into your ROS2 workspace.
Install the ROS2 Hunble distribution:
https://docs.ros.org/en/humble/Installation.htmlThis project requires ROS2 Humble, along with additional system and Python dependencies.
Install the required system packages:
sudo apt update && sudo apt install -y \
python3-opencv \
ros-humble-cv-bridge \
ros-humble-image-transport \
ros-humble-rclcpp \
ros-humble-std-msgs \
ros-humble-ament-index-cpp \
libeigen3-devInstall the required Python packages:
pip install opencv-python numpy ultralytics nlohmann-jsoncd ~/Real-Time-Object-Tracking-ROS2
colcon build --packages-select object_tracking_viz
source install/setup.bashModify config/params.yaml to adjust the camera usage, video file name, and tracking settings
Use the launch file to start all ROS2 nodes:
ros2 launch object_tracking_viz app.launch.pyOR, manually run each node:
ros2 run object_tracking_viz image_publisher
ros2 run object_tracking_viz image_subscriber
ros2 run object_tracking_viz object_tracking
ros2 run object_tracking_viz object_visualizationBelow is the rqt_graph showing the ROS2 nodes and their connections.
Here are some helpful resources related to using WSL2, USB support in WSL2, Gazebo Simulation, and OpenCV VideoIO:
- Microsoft Docs - Connect USB in WSL
- WSL2 Linux Kernel (GitHub)
- VideoIO v4l2 /dev/video0 select timeout issue (OpenCV Forum)
- Package setup for both Cpp and Python
- Launch file for multiple nodes
- YOLOv8 on ROS2
- Multi-threaded execution
- Yaml parameters
- Custom msgs
Contributions are welcome! Please fork the repository and create a pull request.
Developed by Shivani Khandelwal.
Connect with me here - Profile
For questions or improvements, feel free to reach out at shivanikhandelwal487@gmail.com.