python3环境下编译cv_bridge
前言
之前使用ROS的时候只用了C++,没有遇到cv_bridge这个坑,最近需要用到一个分割的网络与ROS桥接,为此使用Anaconda配置了一个Python3.8的环境,运行cv_bridge时出现报错,如下图所示:
一、下载cv_bridge包并编译
# `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 可以通过此路径查看自己的Python版本并做更改
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
extend参数的作用是让这次的路径配置不影响之前配置好的路径
还需要把cv2.so 文件重命名为cv2_ros.so文件:
cd /opt/ros/kinetic/lib/python2.7/dist-packages/
sudo mv cv2.so cv2_ros.so
二、测试&debug
打开终端,更新环境变量,进入python3环境,输入测试代码:
source ~/ros-py3/install/setup.bash --extend
$python3
>>> 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)。
解决办法:打开cv_bridge包中的CMakeLists.txt文件:
gedit ~/ros-py3/src/vision_opencv/cv_bridge/CMakeLists.txt
把find_package(Boost REQUIRED python3)
改为
find_package(Boost REQUIRED python-py35)
保存后关闭,重新编译即可:
cd ros-py3
# Build
catkin build cv_bridge
# Extend environment with new package
source install/setup.bash --extend
总结
一定要记得更新环境变量,否则会报错:
source install/setup.bash --extend
在其他目录下使用cv_bridge时使用绝对路径 :
source ~/ros-py3/install/setup.bash --extend