背景 #
在使用 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然后知道
python的include路径, 可以使用以下命令获得: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_bridgecatkin build cv_bridge
-
使用
-
当前终端临时使用
source install/setup.bash --extend
-
开机自动使用
可以添加到
.bashrc里面source ~/cvbridge_build_ws/install/setup.bash --extend
-