我在Ubuntu 14.04上使用ROS indigo和python3进行了一个计算机视觉项目,然后我不得不使用ROS动能在Ubuntu 16.04上移动。在这里,我遇到了多个问题:
1)我安装了opencv,但无法在python3中导入它,错误信息为:
Traceback (most recent call last):
File "test.py", line 2, in <module>
import cv2
ImportError: /opt/ros/kinetic/lib/python2.7/dist-packages/cv2.so: undefined symbol: PyCObject_Type
我想到的只是重命名cv2.so
cd /opt/ros/kinetic/lib/python2.7/dist-packages/
sudo mv cv2.so cv2_ros.so
然后我能够导入cv2并使用它
2)然后我无法导入rospy,但安装python3-catkin-pkg-modules和python3-rospkg-modules解决了这个问题
3)最后我遇到了cv_bridge的问题,它说:
[ERROR] [1520780674.845066]: bad callback: <bound method ViewsBuffer.update of <__main__.ViewsBuffer object at 0x7f5f45a07f28>>
Traceback (most recent call last):
File "/opt/ros/kinetic/lib/python2.7/dist-packages/rospy/topics.py", line 750, in _invoke_callback
cb(msg)
File "test.py", line 48, in update
im = self.bridge.imgmsg_to_cv2(im, "bgr8")
File "/opt/ros/kinetic/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 "/opt/ros/kinetic/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 "/opt/ros/kinetic/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)
我认为问题出在cv_bridge_boost.so文件中。 我也尝试从https://github.com/ros-perception/vision_opencv构建cv_bridge,但它会自动为python2.7构建,我试图稍微修改一下CMakeLists.txt以指定python3,但我对CMakeLists没有多少经验所以它没有用。我还尝试将cv_bridge模块复制到我的项目文件夹,但它没有太大变化,它仍然指向cv_bridge_boost.so文件。 另外要提到的是cv_bridge与python2.7一起使用,但我的项目需要python3.5。
答案 0 :(得分:1)
我的系统中安装了anaconda3,使用cv_bridge时也遇到类似的问题。当我尝试使用catkin build cv_bridge
进行构建时,会引发错误。我使用来自conda的python3.7。我的catkin cofig命令是:
catkin config -DPYTHON_EXECUTABLE=/home/akashbaskaran/anaconda3/bin/python3 -DPYTHON_INCLUDE_DIR=/home/akashbaskaran/anaconda3/include/python3.7m -DPYTHON_LIBRARY=/home/akashbaskaran/anaconda3/lib/libpython3.7m.so
解决方案: 我做了几件事,问题就解决了。
-DPYTHON_EXECUTABLE=/home/akashbaskaran/anaconda3/envs/tf/bin/python3.6 -DPYTHON_INCLUDE_DIR=/home/akashbaskaran/anaconda3/envs/tf/include/python3.6m -DPYTHON_LIBRARY=/home/akashbaskaran/anaconda3/envs/tf/lib/libpython3.6m.so
e source devel/setup.bash
现在
import cv2
from cv_bridge.boost.cv_bridge_boost import getCvType
应该正常工作。