无法在ROS Kinetic和Python3中使用cv_bridge

Ego*_*gor 6 opencv cmake python-3.x ros

我在具有ROS indigo和python3的Ubuntu 14.04上有一个计算机视觉项目,然后不得不在具有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
Run Code Online (Sandbox Code Playgroud)

我发现只是重命名cv2.so

cd /opt/ros/kinetic/lib/python2.7/dist-packages/
sudo mv cv2.so cv2_ros.so
Run Code Online (Sandbox Code Playgroud)

然后我能够导入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)
Run Code Online (Sandbox Code Playgroud)

我相信问题出在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。

hex*_*ons 8

没错,您应该使用python3构建cv_bridge。

你可以通过传递

-DPYTHON_EXECUTABLE=/usr/bin/python3 -DPYTHON_INCLUDE_DIR=/usr/include/python3.5m -DPYTHON_LIBRARY=/usr/lib/x86_64-linux-gnu/libpython3.5m.so
Run Code Online (Sandbox Code Playgroud)

args到cmake。或者,如果您使用柳絮来构建软件包,则可以执行以下步骤:

# `python-catkin-tools` is needed for catkin tool
# `python3-dev` and `python3-catkin-pkg-modules` is needed to build cv_bridge
# `python3-numpy` and `python3-yaml` is cv_bridge dependencies
# `ros-kinetic-cv-bridge` is needed to install a lot of cv_bridge deps. Probaply you already have it installed.
sudo apt-get install python-catkin-tools python3-dev python3-catkin-pkg-modules python3-numpy python3-yaml ros-kinetic-cv-bridge
# Create catkin workspace
mkdir catkin_workspace
cd catkin_workspace
catkin init
# Instruct catkin to set cmake variables
catkin config -DPYTHON_EXECUTABLE=/usr/bin/python3 -DPYTHON_INCLUDE_DIR=/usr/include/python3.5m -DPYTHON_LIBRARY=/usr/lib/x86_64-linux-gnu/libpython3.5m.so
# Instruct catkin to install built packages into install place. It is $CATKIN_WORKSPACE/install folder
catkin config --install
# Clone cv_bridge src
git clone https://github.com/ros-perception/vision_opencv.git src/vision_opencv
# Find version of cv_bridge in your repository
apt-cache show ros-kinetic-cv-bridge | grep Version
    Version: 1.12.8-0xenial-20180416-143935-0800
# Checkout right version in git repo. In our case it is 1.12.8
cd src/vision_opencv/
git checkout 1.12.8
cd ../../
# Build
catkin build cv_bridge
# Extend environment with new package
source install/setup.bash --extend
Run Code Online (Sandbox Code Playgroud)

$ python3

Python 3.5.2 (default, Nov 23 2017, 16:37:01) 
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from cv_bridge.boost.cv_bridge_boost import getCvType
>>> 
Run Code Online (Sandbox Code Playgroud)

如果遇到下一个错误

CMake Error at /usr/share/cmake-3.5/Modules/FindBoost.cmake:1677 (message):
  Unable to find the requested Boost libraries.

  Boost version: 1.58.0

  Boost include path: /usr/include

  Could not find the following Boost libraries:

          boost_python3

  No Boost libraries were found.  You may need to set BOOST_LIBRARYDIR to the
  directory containing Boost libraries or BOOST_ROOT to the location of
  Boost.
Call Stack (most recent call first):
  CMakeLists.txt:11 (find_package)
Run Code Online (Sandbox Code Playgroud)

这是因为CMake试图找到libboost_python3.so库,但是在ubuntu中它是libboost_python-py35.so/usr/lib/x86_64-linux-gnu/libboost_python-py35.so),因此您应该更改行

find_package(Boost REQUIRED python3)
Run Code Online (Sandbox Code Playgroud)

find_package(Boost REQUIRED python-py35)
Run Code Online (Sandbox Code Playgroud)

在文件src/vision_opencv/cv_bridge/CMakeLists.txt和重建包中。