ROS2 how to make a node read files

asked 2021-05-11 05:02:51 -0500

Raoryn gravatar image

hi, i've made a ros2 node that outputs a msg to another node. the problem comes when i make that node file import a python script to run OpenCV with Yolo. i can import the python file all good but when the script comes to

net = cv2.dnn.readNet("yolov3_training_last.weights", "yolov3_testing.cfg")

the script stops because it cannot map to the yolov3_training_last.weight and yolov3_testing.cfg. Package name: yeti error: Failed to parse NetParameter File: /home/user/dev_ws/srs/yeti/yetiyolov3_testing.

for some reason it wont give a / for last folder into the file it wants, as the folder structure is yeti/yeti/yolov3 files. How does ros2 eloquent find files? how can i get a ros2 node import a python script that uses files in the folder i use to build the package?

edit retag flag offensive close merge delete


When you build a ROS2 package using colcon build an install/ directory will be generated with your package and its files. Python nodes, YAML parameter files, etc. are copy/pasted into install/ during the build process. If you're trying to reference files relative to the node's python file then all of those paths will be messed-up after the build process.

Assuming you're running Ubuntu or something similar you can find the copies of those files in the install/ directory by running:

find ./install/ -type d -name *.py

You should get something like install/<pkg_name>/lib/python3.8/site-packages/<pkg_name>/

Spectre gravatar image Spectre  ( 2021-05-12 02:54:27 -0500 )edit

Miscellaneous files won't be copied during build process automatically, unless you add them to "data_files=[]" in the file. You can read about this here:

As for linking to files in the src folder, it seems that it's a bad idea, see William's comment ( Jun 13 '18 ) here:

There is however a way you could override the build to read all files from the src folder instead from the install folder like this:

colcon build --symlink-install --packages-select your-package-name


But this is probably intended just for developing purpose.

mihail1989 gravatar image mihail1989  ( 2022-01-01 14:34:26 -0500 )edit