【问题标题】:Unable to use cv_bridge with ROS Kinetic and Python3无法将 cv_bridge 与 ROS Kinetic 和 Python3 一起使用
【发布时间】:2018-08-19 15:59:49
【问题描述】:

我在 Ubuntu 14.04 上有一个使用 ROS indigo 和 python3 的计算机视觉项目,然后我不得不在 Ubuntu 16.04 上使用 ROS 动力学。在这里我遇到了多个问题:

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。

【问题讨论】:

    标签: python-3.x opencv cmake ros


    【解决方案1】:

    你是对的,你应该用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
    

    参数到 cmake。 或者,如果您使用 catkin 构建包,则可以执行以下步骤:

    # `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
    

    $ 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
    >>> 
    

    如果遇到下一个错误

    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)
    

    这是因为CMake试图找到libboost_python3.so库,但是在ubuntu中是libboost_python-py35.so(/usr/lib/x86_64-linux-gnu/libboost_python-py35.so),所以你应该换行

    find_package(Boost REQUIRED python3)
    

    find_package(Boost REQUIRED python-py35)
    

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

    【讨论】:

    • 工作就像一个魅力!
    • 非常感谢,这适用于 Python 3.6,在 virtualenv 下
    • Python 3.8 失败。当我尝试时,我得到了 1 个包成功和 3 个包被跳过/列入黑名单。看着日志,它的目标是 3.6m,不知道在哪里。我使用您的设置将所有提到的 Python 实例替换为我的 Python3.8 的路径。
    【解决方案2】:

    我的系统中安装了 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
    

    解决方案: 我做了几件事,问题得到了解决。

    • 由于使用 anaconda,可执行文件和包含目录应该是您当前虚拟环境中的目录。 -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
    • 我在尝试 catkin build cv_bridge 时遇到了构建问题。如果您遇到类似问题,请删除除 src 之外的所有文件夹。然后从终端运行 catkin_make(确保你在 catkin_workspace 中)。
    • 获取当前工作空间source devel/setup.bash

    现在 import cv2 from cv_bridge.boost.cv_bridge_boost import getCvType

    应该可以正常工作。

    【讨论】:

      猜你喜欢
      • 2019-12-01
      • 2020-03-17
      • 1970-01-01
      • 2019-01-06
      • 2017-08-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-09-04
      相关资源
      最近更新 更多