It was when I was messing around with ROS. Since RGB data is sent from the input node of the camera, I wanted to receive it at another node and was trying to use cv_bridge. It looks like below.
from cv_bridge import CvBridge def prediction(msg): bridge = CvBridge() img = bridge.imgmsg_to_cv2(msg, "bgr8")
However, when I built it and ran it, I got the following error.
File "/opt/ros/melodic/lib/python2.7/dist-packages/cv_bridge/core.py", line 91, in encoding_to_cvtype2 from cv_bridge.boost.cv_bridge_boost import getCvType ImportError: dynamic module does not define module export function (PyInit_cv_bridge_boost)
When I looked it up, it seemed that the cause was that cv_bridge was built with python2. Since my execution environment was python3 (virtualenv), I had to build cv_bridge from the source code.
At first, I tried it by referring to the method of here. If my local python3 is the runtime environment, it might work, but it didn't work in my environment using virtualenv. I will share it for the time being.
$ cd catkin_ws $ catkin config -DPYTHON_EXECUTABLE=/usr/bin/python3 -DPYTHON_INCLUDE_DIR=/usr/include/python3.5m -DPYTHON_LIBRARY=/usr/lib/aarch64-linux-gnu/libpython3.5m.so $ catkin config --install $ git clone https://github.com/ros-perception/vision_opencv.git src/vision_opencv $ apt-cache show ros-melodic-cv-bridge | grep Version $ cd src/vision_opencv/ #I think it was probably the lower version. apt-It came out in the cache show. $ git checkout 1.13.0 $ cd ../../ $ catkin build cv_bridge $ source install/setup.bash --extend
In my environment using virtualenv, cv_bridge is built successfully, but when I build other nodes of my own with catkin_ws together after building, the following error occurs.
RLException: [sample.launch] is neither a launch file in package [sample_proc] nor is [sample_proc] a launch file name
I'm quite addicted to the swamp from here. Share how you have solved it to reduce casualties.
The repository environment of here used cv_bridge with python3 on virtualenv like myself, so I got a hint from that. There are two important things. One is to build cv_bridge in a different workspace than your own. The other is to remove the python2.7 path before the line importing cv_bridge in my script. The specific procedure is as follows.
$ sudo apt install python-catkin-tools python3-dev python3-catkin-pkg-modules python3-numpy python3-yaml ros-melodic-cv-bridge $ mkdir -p cv_bridge_ws/src $ git clone https://github.com/ros-perception/vision_opencv.git src/vision_opencv $ apt-cache show ros-melodic-cv-bridge | grep Version $ cd src/vision_opencv/ # apt-The version value changes depending on the result of cache show $ git checkout 1.13.0 $ cd ../../ #Depending on the environment, DPYTHON_LIBRARY aarch64-linux-The gnu part changes $ catkin config -DCMAKE_BUILD_TYPE=Release -DPYTHON_EXECUTABLE=/usr/bin/python3 -DPYTHON_INCLUDE_DIR=/usr/include/python3.6m -DPYTHON_LIBRARY=/usr/lib/aarch64-linux-gnu/libpython3.6m.so $ catkin build $ source devel/setup.bash --extend $ cd ../catkin_ws #Build your own node $ catkin build $ source devel/setup.bash
Also change your own script.
import sys sys.path.remove('/opt/ros/melodic/lib/python2.7/dist-packages') from cv_bridge import CvBridge def prediction(msg): bridge = CvBridge() img = bridge.imgmsg_to_cv2(msg, "bgr8")
This will cause python3's cv_bridge to be called at runtime instead of python2's cv_bridge.
I don't like melodic anymore (laughs). I want to use noetic or ROS2.