Contents
Wanted to compile ROS packages with Python 3 on ROS Melodic, only realizing that catkin_make only compiles Python 2 scripts instead after a while? No worries! In this tutorial, we’ll be looking exactly into that.
Background
This tutorial is tested on Jetson Nano with JetPack 4.3 L4T 32.4.2 (
Ubuntu 18.04
) withROS Melodic
& customOpen CV 4.3.0
installation.
As I was writing a ROS package that utililses the Sensor Image transport and OpenCV4, everything was going well from building to compilation of the code. The issue arrises when I started to run the node itself, which gives the following error:
Assertion failed (tlsSlots.size() > slotIdx) in releaseSlot
Searching online, I found out that others had a similar issue when there are 2 conflicting versions of OpenCV being used.
Therefore, I tried compiling the open_vision package (noetic-branch
) within the catkin workspace itself and modifying the CMakeLists.txt
accordingly, thinking it would work. However, some error arises again:
In file included from /usr/include/python2.7/numpy/ndarrayobject.h:27:0, from /home/jnano/catkin_ws/src/vision_opencv/cv_bridge/src/module.hpp:26, from /home/jnano/catkin_ws/src/vision_opencv/cv_bridge/src/module.cpp:35: /home/jnano/catkin_ws/src/vision_opencv/cv_bridge/src/module.hpp: In function ‘void* do_numpy_import()’: /usr/include/python2.7/numpy/__multiarray_api.h:1537:144: error: return-statement with no value, in function returning ‘void*’ [-fpermissive] #define import_array() {if (_import_array() < 0) {PyErr_Print(); PyErr_SetString(PyExc_ImportError, "numpy.core.multiarray failed to import"); return NUMPY_IMPORT_ARRAY_RETVAL; } } ^ /usr/include/python2.7/numpy/__multiarray_api.h:1537:144: note: in definition of macro ‘import_array’ #define import_array() {if (_import_array() < 0) {PyErr_Print(); PyErr_SetString(PyExc_ImportError, "numpy.core.multiarray failed to import"); return NUMPY_IMPORT_ARRAY_RETVAL; } } ^~~~~~
And then it dawned on me: the fact I was trying to compile the package with Python 2 instead of 3. However, if I wanted to stick to compiling the melodic branch with OpenCV 4, another arises as the melodic branch is based on OpenCV 3.
Since ROS does not support Python 3 by default, we would have to compile cv_bridge via another method if want to use OpenCV 4 with Python 3.
Fun fact: ROS2 supports Python 3 by default.
Furthermore, as the support of Python 2.7 ended last year (December 2019), it is recommended to discontinue the use of Python 2.7 as it would not be updated anymore and move on to Python 3.
Therefore, this tutorial would be covering how to compile a Python 3 ROS package with ROS Melodic, specifically cv_bridge
. With that, let’s get started!
Dependencies
We’ll first download the python build tools:
$ sudo apt-get install python3-pip python-catkin-tools python3-dev python3-numpy $ sudo pip3 install rospkg catkin_pkg
Workspace
Next, create a separate workspace to compile the bridge_cv
ROS package. We would be naming the directory cvbridge_build_ws
.
$ mkdir -p ~/cvbridge_build_ws/src $ cd ~/cvbridge_build_ws/src
After the workspace has been setup, we’ll clone the open_vision repository into ~/cvbridge_build_ws/src
:
$ git clone -b noetic https://github.com/ros-perception/vision_opencv.git
We would be downloading the noetic branch
instead of the melodic branch
as it supports for both OpenCV 4 and Python 3.If you try compiling immediately, you would get this error:
Therefore, we need to make a slight change to the cv_bridge
CMakeLists.txt
file before proceeding as Ubuntu 18.04 is not be able to recognise python37 by default.
Open CMakeLists.txt
with you favorite text editor and make the following modification at Line 11, changing:
find_package(Boost REQUIRED python37)
to
find_package(Boost REQUIRED python3)
Save and exit the text editor.
Compilation
After the dependencies and workspace have been setup, we would proceed to configure the workspace for compilation:
$ cd ~/cvbridge_build_ws $ catkin config -DPYTHON_EXECUTABLE=/usr/bin/python3 -DPYTHON_INCLUDE_DIR=/usr/include/python3.6m -DPYTHON_LIBRARY=/usr/lib/aarch64-linux-gnu/libpython3.6m.so $ catkin config --install
Note: Change the python path accordingly, you could check out the location via Python 3.
$ python3 >>> import sys >>> print(sys.executable) #Print python3 executable path >>> print(sys.path) #Print python3 library pathTo find where the include files are:
$ python3-config --includes
After the configuration is completed, build the package:
$ catkin build cv_bridge
To use the package, you could source it via:
$ source install/setup.bash --extend
And Viola! You have “compiled” the cv_bridge package via Python 3.
You could add it to .bashrc if you use the ROS package frequently.
References
- https://wiki.ros.org/UsingPython3
- https://medium.com/@beta_b0t/how-to-setup-ros-with-python-3-44a69ca36674
- https://answers.ros.org/question/350904/cv_bridge-throws-boost-import-error-in-python-3-and-ros-melodic/
- https://stackoverflow.com/questions/49221565/unable-to-use-cv-bridge-with-ros-kinetic-and-python3
- https://stackoverflow.com/questions/35071192/how-to-find-out-where-the-python-include-directory-is
- https://stackoverflow.com/questions/6767283/find-where-python-is-installed-if-it-isnt-default-dir#6767329
Hi, i have a question, were do i make the first directory???
in the catkin_ws??
Hi Marco, the first directory would be created in the home directory (~/ path), not in the catkin_ws directory.
when i finish all this process, how can i catkin_make in ros, in the catkin_ws???
Yes, it would be in catkin_ws.
how can you run this when the install space is missing:
source install/setup.bash –extend
Hi, have you configured and build the workspace before sourcing `setup.bash`? The setup script would only be available after building the workspace.
Can’t I use catkin_make instead of build command?
Hi, we are building only the cv_bridge package, hence
catkin build
is used.In general,
catkin build
could be used from any directory in the workspace and can be used to compile a specific set of packages, with more flexibility in terms of package compilation. Whereas forcatkin_make
, it only works in the top level directory of the workspace and compiles all the packages in the workspace. The packages would also be compiled in parallel, which makes the whole compilation time shorter.Really thank you for the wonderful instructions. By the way, i am trying to do this in ubuntu 16.04 and i know that 16.04 uses python3.5 instead of 3.6. Would it be okay just by changing all the 3.6 to 3.5 in the commands?
Hi jeongho, it should work by changing the version number.
Hello, I got an error
“TypeError: signal handler must be signal.SIG_IGN, signal.SIG_DFL, or a callable object”
when i “$ catkin build cv_bridge” I have tried some solution but still the same.
i’m using Jetson Xavier JP4.4, python3.6 and ROS Melodic
Hi Ihsan, have you tried compiling the package with other versions of Python3?
Hi, after building and sourcing, ROS cannot find the other package i was working on, located in another catkin workspace.
Was i supposed to do all these steps in that workspace?
Hi Andrei, the other catkin workspace have to be sourced again after the cv_bridge is sourced.
This article was incredibly helpful. Thanks a lot!
I just wanted to add that if you are using Ubuntu the DPYTHON_LIBRARY flag should begin with
/usr/lib/x86_64-linux-gnu/…
no big deal but hopefully that helps someone
happy coding
I want to thank you for this amazing post. My comrade and I went insane solving this problem. Now it works like a charm. May Odin bless your soul.
Hi,
After going through all processes, there is no error, but when I test run with
” roslaunch opencv_tests pong.launch” got error ” cannot import name CvBridge”.
______________________________________________________
auto-starting new master
process[master]: started with pid [2041]
ROS_MASTER_URI=http://localhost:11311
setting /run_id to b3b7fb1e-738d-11eb-951a-00044bec5662
process[rosout-1]: started with pid [2093]
started core service [/rosout]
process[image_view_pong-2]: started with pid [2096]
process[png-3]: started with pid [2101]
[ INFO] [1613833802.649536066]: Initializing nodelet with 4 worker threads.
[ INFO] [1613833803.033829504]: Using transport “raw”
Unable to init server: Could not connect: Connection refused
(images:2096): Gtk-WARNING **: 22:10:03.042: cannot open display:
Traceback (most recent call last):
File “/home/pongrut/catkin_ws/src/vision_opencv/opencv_tests/nodes/source.py”, line 43, in
from cv_bridge import CvBridge
ImportError: cannot import name CvBridge
[image_view_pong-2] process has died [pid 2096, exit code -11, cmd /opt/ros/melodic/lib/image_view/image_view image:=/opencv_tests/images __name:=image_view_pong __log:=/home/pon grut/.ros/log/b3b7fb1e-738d-11eb-951a-00044bec5662/image_view_pong-2.log].
log file: /home/pongrut/.ros/log/b3b7fb1e-738d-11eb-951a-00044bec5662/image_view_pong-2*.log
[png-3] process has died [pid 2101, exit code 1, cmd /home/pongrut/catkin_ws/src/vision_opencv/opencv_tests/nodes/source.py __name:=png __log:=/home/pongrut/.ros/log/b3b7fb1e-738 d-11eb-951a-00044bec5662/png-3.log].
log file: /home/pongrut/.ros/log/b3b7fb1e-738d-11eb-951a-00044bec5662/png-3*.log
Hi, did you source the workspace with
--extend
?I am getting this error after following last line of code “catkin build cv_bridge”. Everything else worked fine for me.
The error is
“””
Errors << cv_bridge:make /home/vikram/cvbridge_build_ws/logs/cv_bridge/build.make.000.log
make[2]: *** No rule to make target '/usr/lib/aarch64-linux-gnu/libpython3.6m.so', needed by '/home/vikram/cvbridge_build_ws/devel/.private/cv_bridge/lib/python3/dist-packages/cv_bridge/boost/cv_bridge_boost.so'. Stop.
make[2]: *** Waiting for unfinished jobs….
make[1]: *** [src/CMakeFiles/cv_bridge_boost.dir/all] Error 2
make: *** [all] Error 2
cd /home/vikram/cvbridge_build_ws/build/cv_bridge; catkin build –get-env cv_bridge | catkin env -si /usr/bin/make –jobserver-fds=6,7 -j; cd –
"""
Hi Vikram, what is the version of your Python3 installation?