gpt4 book ai didi

python-3.x - 无法将 cv_bridge 与 ROS Kinetic 和 Python3 一起使用

转载 作者:太空宇宙 更新时间:2023-11-03 20:51:39 26 4
gpt4 key购买 nike

我在 Ubuntu 14.04 上有一个使用 ROS indigo 和 python3 的计算机视觉项目,然后我不得不转向使用 ROS kinetic 的 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。

最佳答案

你是对的,你应该用 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.x - 无法将 cv_bridge 与 ROS Kinetic 和 Python3 一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49221565/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com