无法在ROS Kinetic和Python3中使用cv_bridge

9

我是一位有用的助手,可以为您进行翻译。

我原先在Ubuntu 14.04上使用ROS Indigo和Python3开发计算机视觉项目,但后来需要转移到Ubuntu 16.04上使用ROS Kinetic。在此过程中,我遇到了多个问题:

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

1) 我成功导入了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。
3个回答

19

你是正确的,你应该使用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 中进行修改并重新构建包。


运行得非常顺利! - Ambareesh
非常感谢,这在Python 3.6下,在虚拟环境中运行。 - Dumisani Kunene
Python 3.8出现故障。当我尝试时,我得到1个包成功和3个包被跳过/列入黑名单。查看日志,它在某个地方针对3.6m,不确定在哪里。我使用了你的设置,用我的Python3.8路径替换所有Python实例提到的内容。 - Jibril

1

我在系统中安装了anaconda3,并且在使用cv_bridge时遇到了类似的问题。当我尝试使用catkin build cv_bridge进行构建时,它会报错。我使用来自conda的python3.7。我的catkin配置命令是:

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,可执行文件和include目录应该是当前虚拟环境内的文件。 -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
  • 当我尝试构建cv_bridge时,出现了构建问题。如果您遇到类似的问题,请删除除src以外的所有文件夹。然后从终端运行catkin_make(确保您在catkin_workspace内)。
  • 源代码当前工作区source devel/setup.bash

现在,import cv2from cv_bridge.boost.cv_bridge_boost import getCvType应该可以正常工作,不会出现错误。


0

我的基本环境:Ubuntu 18.04和ROS Melodic。

我正在conda的虚拟环境(Python3.7)中编译cv_bridge,并且我遇到了与@Jibril相同的问题。当我运行catkin build cv_bridge时,我得到1个成功的软件包和3个被跳过/列入黑名单的软件包。

我使用的命令是

# 1
catkin init

# 2
catkin config \
-DPYTHON_EXECUTABLE=/home/zed/anaconda3/envs/yolov5/bin/python3.7 \
-DPYTHON_INCLUDE_DIR=/home/zed/anaconda3/envs/yolov5/include/python3.7m \
-DPYTHON_LIBRARY=/home/zed/anaconda3/envs/yolov5/lib/libpython3.7m.so

# 3
catkin config --install

# 4
catkin build cv_bridge

然后我在{ws}/log文件夹中查看日志文件,找到了关于--install-layout参数的错误信息。 参考issue中的解决方案,我在原始命令中添加了-DSETUPTOOLS_DEB_LAYOUT=OFF,最终成功编译完成。
我修改后的命令是:
# 1
catkin init

# 2
catkin config \
-DPYTHON_EXECUTABLE=/home/zed/anaconda3/envs/yolov5/bin/python3.7 \
-DPYTHON_INCLUDE_DIR=/home/zed/anaconda3/envs/yolov5/include/python3.7m \
-DPYTHON_LIBRARY=/home/zed/anaconda3/envs/yolov5/lib/libpython3.7m.so \
-DSETUPTOOLS_DEB_LAYOUT=OFF

# 3
catkin config --install

# 4
catkin build cv_bridge

当你需要在另一个ROS工作区中使用已编译的cv_bridge时,你只需执行 source {PATH_cv_bridge_ws}/install/setup.bash --extend,然后执行 source {PATH_current_ws}/devel/setup.bash

如果上述解决方案对你不起作用,你也可以尝试一些在这个issue中提出的cv_bridge替代方案!


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接