"cv_bridge" throws boost import error in Python 3 and ROS Melodic
I'm running a script that uses cv_bridge with OpenCV 4, on Python 3, on ROS Melodic, on Raspbian, on a Raspberry Pi 4, and I'm running into errors trying to dynamically import cv_bridge.boost
and failing.
I built ROS from source following these instructions, and installed opencv with sudo pip3 install opencv-contrib-python==4.1.0.25
. I've had to do a bit of a hack to get it to import properly (I run sys.path.append('/usr/lib/python2.7/dist-packages')
before I import cv2
) but beyond these hacks, OpenCV 4 seems to be playing nice with Python 3, and Python 3 seems to be playing nice with ROS.
I from cv_bridge import CvBridge, CvBridgeError
without issue, then run:
self.bridge = CvBridge()
image_opencv = self.bridge.imgmsg_to_cv2(data, desired_encoding="passthrough")
on a helper class, where data (as the function suggests) is a standard ROS Image message. (In my case, I'm getting these messages from the cv_camera topic /cv_camera/image_raw
)
when I try to run that, I get this error:
[ERROR] [1588106367.612936]: bad callback: <bound method tpu_detector.callback of <__main__.tpu_detector object at 0xa7061df0>>
Traceback (most recent call last):
File "/opt/ros/melodic/lib/python2.7/dist-packages/rospy/topics.py", line 750, in _invoke_callback
cb(msg)
File "/home/pi/catkin_ws/src/edge_tpu/scripts/detect_ros.py", line 91, in callback
data, desired_encoding="passthrough")
File "/home/pi/catkin_build_ws/install/lib/python2.7/dist-packages/cv_bridge/core.py", line 163, in imgmsg_to_cv2
dtype, n_channels = self.encoding_to_dtype_with_channels(img_msg.encoding)
File "/home/pi/catkin_build_ws/install/lib/python2.7/dist-packages/cv_bridge/core.py", line 99, in encoding_to_dtype_with_channels
return self.cvtype2_to_dtype_with_channels(self.encoding_to_cvtype2(encoding))
File "/home/pi/catkin_build_ws/install/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)
every time an image message arrives. Which, to put it lightly, really sucks. I've googled the snot out of it, and come up with a few pages, all of which tell me to build cv_bridge for Python 3 - but I already have. Multiple times. Following the instructions in this Medium post line by line, the error persists. The error traceback above prooves that this custom-built package is being imported like it's supposed to.
It's worth noting that I had to use catkin_make
to build cv_bridge properly; catkin build cv_bridge
(with all the extra flags of https://stackoverflow.com/questions/4..., plus an extra -DPYTHON3=1
I added to placate NumPy) fails for no apparent reason (I've pasted the output into this gist if anyone believes it to be related to the issue)
I have been stuck on this issue for a multitude of hours and have gotten no closer to a solution - any help will be much appreciated!
Hey did you find the solution? I'm getting the same error. If you did can you help me pls?
@Onurcan Thanks for the reminder! See my new answer on this thread.
While this isn't a specific answer to your problem, you are going to have a lot less headaches using python2 and opencv 3 when working with ROS melodic. If you want to stick with python 3 and opencv 4, depending on your Raspbian version, you can install ROS noetic instead since they officially ported to python 3.