Git Product home page Git Product logo

ros2_yolov5_webcam's Introduction

YOLOv5 on ROS2

Pre-requisites

  • ROS2 (DISTRO: galactic) installed and sourced

  • YOLOv5 requirements satisfied

  • ROS usb_cam installed

    sudo apt-get install ros-{ROS2_DISTRO}-usb-cam

Run

  • source ros2

    source /opt/ros/galactic/setup.bash

    use command echo ${ROS_DISTRO} to make sure your current Terminal is sourced to a ROS2 distro, e.g. galactic

  • compile the workspace first

    cd ros2_yolov5_webcam/colcon_ws
    colcon build
  • copy the folder /data in

    /src/yolov5_detect/yolov5/

    to

    /install/yolov5_detect/lib/python3.8/site-packages/yolov5/
  • make sure to set device-id as 0 in webcam_yolo_pub.py:

    self.cap = cv2.VideoCapture(0)
  • launch the nodes

    # stay in the /colcon_ws directory
    source install/setup.sh
    ros2 launch yolov5_detect yolo_webcam_detect.launch.py
  • to friends who comes from the SONY setup project: make sure to set device-id as 2 in webcam_yolo_pub.py:

    self.cap = cv2.VideoCapture(2)

    to whom who has interest in using an external SONY camera as a substitution of web-camera:
    SONY a7r4 setup for Ubuntu

    # stay in the /colcon_ws directory
    source install/setup.sh
    ros2 launch yolov5_detect yolo_sony_detect.launch.py

below are some notes during development...

Implement YOLOv5 algorithm to our ROS node

To learning how to build a ros node containing a custom submodule, check these blogs on ROS answer:

How did I do:

the following guide just works, but not so elegant...*

0) re-write a class for YOLOv5 detector detect_ros.py

Capsule the detect as a class Yolov5Detector(), which would be easier to be implemented in ros node script.
The yolov5 folder is placed directly in our package folder, parallel with sub-package folder:

colcon_ws
├── src
│   ├── yolov5_detect #------------------------ROS package folder
│   │   ├── package.xml
│   │   ├── setup.py
│   │   ├── setup.cfg
│   │   ├── yolov5_detect #----------------ROS package sub-folder
│   │   │   ├── __init__.py
│   │   │   ├── webcam_yolo_sub.py #------------- ROS node script
│   │   │   └── ...
│   │   │
│   │   └── yolov5 #-------------------------the yolov5 submodule
│   │       └── ...
│   │
│   ├── install
│   ├── build
│   └── log
│
└── README.md

1) modify the setup.py

Declare the submodule names in setup.py to ensure them to be compiled together into /install after colcon build:

from setuptools import setup
from setuptools import find_packages

package_name = 'yolov5_detect'
submodules = 'yolov5_detect/yolov5'  # declare the submodule to be build

setup(
    name=package_name,
    version='0.0.0',
    packages=find_packages(),
    include_package_data=True,
    data_files=[
        ('share/ament_index/resource_index/packages',
            ['resource/' + package_name]),
        ('share/' + package_name, ['package.xml']),
        ################## MODIFIED ##################
        (os.path.join('share', package_name, 'launch'),
         glob(os.path.join('launch', '*.launch.py'))),
        (os.path.join('share', package_name, 'yolov5'),
         glob(os.path.join('yolov5', '*.*'))),
        ################## MODIFIED ##################
        # this part helps copy the necessary files into /install directory
    ],
    install_requires=['setuptools'],
    zip_safe=True,
    maintainer='jun',
    maintainer_email='[email protected]',
    description='TODO: Package description',
    license='TODO: License declaration',
    tests_require=['pytest'],
    entry_points={
        'console_scripts': [  # define the entry_points as usual
            'webcam_yolo_pub=yolov5_detect.webcam_yolo_pub:main',
            'webcam_yolo_sub=yolov5_detect.webcam_yolo_sub:main'
        ],
    },
)

2) modify the import part in ALL relevant scripts

To connect the scripts from yolov5's subfolders like utils and models correctly, we need to switch the relative path to absolute path, i.e. add yolov5. ahead of the folder names in ALL relevant scripts:

from utils.plots import Annotator, colors, save_one_box

# --> MODIFY: add '<submodule_name>. ahead

from yolov5.utils.plots import Annotator, colors, save_one_box

Here I managed to do it via running the node again and again and allocate ALL the relevant scripts in the error messages.

3) copy the coco128.yaml to the /install space

copy the folder /data containing .yaml files in

/src/yolov5_detect/yolov5/

to

/install/yolov5_detect/lib/python3.8/site-packages/yolov5/

ros2_yolov5_webcam's People

Contributors

junmeng6025 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.