ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question
0

Cannot run a ROS2 node containing my custom .py scripts

asked 2022-10-18 15:37:56 -0500

junmeng gravatar image

I'm trying to port my work from ROS1 (noetic) to ROS2 (galactic). I've met some problem when I try to implement some custom algorithms (YOLOv5, which is a little complicate, contains many subfolders and scripts) to my image subscriber.
Under ROS1 since the Python node is exactly the executable python script itself, there would be no problems with the imported scripts. But ROS2, via colcon build, the node files to be executed are generated to /colcon_ws/install. Therefore there would be problems about linking our custom python modules to the ros node.

I've already tried to modify the setup.py following these blogs:
- Including a Python module in a ROS2 package
- ROS2 Python relative import of my scritps
and now my setup.py looks like:

from setuptools import setup
from setuptools import find_packages

package_name = 'yolov5_detect'
submodules = 'yolov5_detect/yolov5'

setup(
    name=package_name,
    version='0.0.0',
    # packages=[package_name, submodules],
    packages=find_packages(),
    # include_package_data=True,
    data_files=[
    ('share/ament_index/resource_index/packages',
        ['resource/' + package_name]),
    ('share/' + package_name, ['package.xml']),
    ],
    install_requires=['setuptools'],
    zip_safe=True,
    maintainer='jun',
    maintainer_email='mengjun6025@163.com',
    description='TODO: Package description',
    license='TODO: License declaration',
    tests_require=['pytest'],
    entry_points={
        'console_scripts': [
            'webcam_yolo_pub=yolov5_detect.webcam_yolo_pub:main',
            'webcam_yolo_sub=yolov5_detect.webcam_yolo_sub:main'
        ],
    },
)

the folder structure:

yolov5_detect
└── yolov5_detect
    ├── image_utils.py
    ├── __init__.py
    ├── webcam_yolo_pub.py
    ├── webcam_yolo_sub.py
    └── yolov5
       └── \utils
       └── \models
├── MANIFEST.in
├── package.xml
├── resource
│   └── yolov5_detect
├── setup.cfg
├── setup.py

and in /install the folder yolov5 is also successfully built. Still, when I run the subscriber node, it complains:

$ ~/ros2_guyue/colcon_ws$ ros2 run yolov5_detect webcam_yolo_sub
Traceback (most recent call last):
  File "/home/jun/ros2_guyue/colcon_ws/install/yolov5_detect/lib/yolov5_detect/webcam_yolo_sub", line 33, in <module>
    sys.exit(load_entry_point('yolov5-detect==0.0.0', 'console_scripts', 'webcam_yolo_sub')())
  File "/home/jun/ros2_guyue/colcon_ws/install/yolov5_detect/lib/yolov5_detect/webcam_yolo_sub", line 25, in importlib_load_entry_point
    return next(matches).load()
  File "/usr/lib/python3.8/importlib/metadata.py", line 77, in load
    module = import_module(match.group('module'))
  File "/usr/lib/python3.8/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
  File "<frozen importlib._bootstrap>", line 991, in _find_and_load
  File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 848, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "/home/jun/ros2_guyue/colcon_ws/install/yolov5_detect/lib/python3.8/site-packages/yolov5_detect/webcam_yolo_sub.py", line 12, in <module>
    from yolov5.detect_ros import Yolov5Detector
ModuleNotFoundError: No module named 'yolov5'

the "import" part of my ros node script:

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import rclpy
from rclpy.node import Node

from sensor_msgs.msg import Image
from cv_bridge import CvBridge
import cv2
import numpy as np

from yolov5.detect_ros import Yolov5Detector
from image_utils import *

the "import" part of the custom class script detect_ros.py:

from utils.plots import Annotator, colors, save_one_box
from utils.general import (check_img_size, non_max_suppression, scale_coords, LOGGER)
from utils.torch_utils import select_device, time_sync
from models.common import DetectMultiBackend
import os
...
edit retag flag offensive close merge delete

1 Answer

Sort by » oldest newest most voted
2

answered 2022-10-20 15:18:14 -0500

junmeng gravatar image

I've solved this with the following steps:
- 1) modify setup.py to ensure my yolov5 folder also be compiled into /install
- 2) modify from xxx import xxx in ALL the relevant scripts in yolov5, add '<package_name>.<submodule_name>. ahead of folder name, like:

from utils.plots import Annotator, colors, save_one_box

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

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

With this the node can just struggle to run. It's not very elegant. I'm looking for a more efficient way to do so.

edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2022-10-18 15:37:56 -0500

Seen: 189 times

Last updated: Oct 20 '22