cmake 无法将cv_bridge与ROS动力学和Python3配合使用

enxuqcxy  于 2023-02-23  发布在  Python
关注(0)|答案(3)|浏览(154)

我在Ubuntu 14.04上有一个使用ROS靛蓝和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

然后我就可以导入并使用cv 2了
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。

tjrkku2a

tjrkku2a1#

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

args到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中并重建程序包。

7cjasjjr

7cjasjjr2#

我在系统中安装了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 cv2from cv_bridge.boost.cv_bridge_boost import getCvType
应正常工作。

ppcbkaq5

ppcbkaq53#

我的基本环境:Ubuntu 18.04和ROS旋律。
我在**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

如果上面的解决方案不适合你,你也可以尝试一些cv_bridge的替代方案,在这个issue!!

相关问题