跳过正文
Background Image

在 Ros melodic 版本上安装 cv bridge with python3

··304 字·1 分钟
lizqwerscott
作者
lizqwerscott

背景
#

在使用 ROS Melodic 的时候, cv_bridge 默认是使用 python2.7. 在 python3 中使用 cv_bridge 会出现错误.

解决办法
#

自己编译 python3 版本的 cv_bridge

依赖
#

使用以下命令安装需要的依赖

sudo apt-get install python3-pip python-catkin-tools python3-dev python3-numpy
sudo pip3 install rospkg catkin_pkg

编译
#

  • 需要创建一个工作空间

    mkdir -p ~/cvbridge_build_ws/src
    cd ~/cvbridge_build_ws/src
    
  • 下载 noetic 版本的 cv_bridge

    使用这个 github 仓库的 noetic 分支, 将代码放到 cvbridge_build_ws/src 目录下

    • git clone

      git clone -b noetic https://github.com/ros-perception/vision_opencv.git
      
  • 修改需要编译的 Python 版本

    首先需要确保你 Ubuntu 系统的 python 默认是 python3, 修改 vision_opencv/cv_bridge/CMakeLists.txt 将:

    find_package(Boost REQUIRED python37)
    

    修改为:

    find_package(Boost REQUIRED python3)
    
  • 编译

    首先需要知道你的 python3 路径在那里, 可以使用以下命令获得:

    where python3
    

    或者

    which python3
    

    然后知道 pythoninclude 路径, 可以使用以下命令获得:

    import sys
    print(sys.executable) #Print python3 executable path
    print(sys.path) #Print python3 library path
    

    或者使用

    python3-config --includes
    

    然后根据以下获得的变量执行下列命令:

    cd ~/cvbridge_build_ws
    catkin config -DPYTHON_EXECUTABLE=/usr/bin/python3 -DPYTHON_INCLUDE_DIR=/usr/include/python3.6m -DPYTHON_LIBRARY=/usr/lib/aarch64-linux-gnu/libpython3.6m.so
    catkin config --install
    

    编译 cv_bridge

    catkin build cv_bridge
    
  • 使用

    • 当前终端临时使用

      source install/setup.bash --extend
      
    • 开机自动使用

      可以添加到 .bashrc 里面

      source ~/cvbridge_build_ws/install/setup.bash --extend