Ubuntu 18.04 ROS Melodic中调用支持Python3的cv_bridge

Ubuntu 18.04 ROS Melodic中调用支持Python3的cv_bridge

  • 0.背景
  • 1. 编译自己的cv_bridge功能包(Python 3.7.11)
  • 2. 更新当前shell的环境变量
  • 3. 附录
      • Why use source xxx --extend(为什么使用source xxx --extend)

0.背景

这段时间搞了VINS-Mono和深度学习的结合,需要调用cv_bridge进行通信,而ROS Melodic默认安装的cv_bridge是支持python2.7的,而现在的深度学习代码都是用Python3写的,所以就出现了问题。python3中直接调用cv_bridge的话会有报错:

ImportError: dynamic module does not define module export function (PyInit_cv_bridge_boost)

不过幸运的是这个问题可以自己动手解决,cv_bridge其实是ROS的一个功能包,所以我们可以基于源码编译自己的cv_bridge。在需要的时候覆盖掉ROS自带的进行调用。

1. 编译自己的cv_bridge功能包(Python 3.7.11)

# 安装需要的库
sudo apt-get install python-catkin-tools python3-dev python3-catkin-pkg-modules python3-numpy python3-yaml ros-melodic-cv-bridge

# 创建一个目录作为工作空间
mkdir -p cv-bridge-py3.7/src

# 配置工作空间
catkin config -DPYTHON_EXECUTABLE=/home/qyz/anaconda3/envs/pytorch/bin/python3 -DPYTHON_INCLUDE_DIR=/home/qyz/anaconda3/envs/pytorch/include/python3.7m -DPYTHON_LIBRARY=/home/qyz/anaconda3/envs/pytorch/lib/libpython3.7m.so -DCMAKE_BUILD_TYPE=Release -DSETUPTOOLS_DEB_LAYOUT=OFF 

catkin config --install


# Clone cv_bridge src
git clone https://github.com/ros-perception/vision_opencv.git src/vision_opencv

# 根据返回的版本,然后切换到指定版本分支上
apt-cache show ros-melodic-cv-bridge | grep Version
# Version: 1.13.0-0bionic.20210505.032238
# Checkout right version in git repo. In our case it is 1.13.0
cd src/vision_opencv/
git checkout 1.13.0
cd ../../
# Build, 编译的时候如果提示没有cmake,可以通过:python3 -m pip install cmake
catkin build cv_bridge

这里还有一点要很需要注意的是build cv_bridge会用到C++的opencv库,../vision_opencv/cv_bridge/CMakeLists.txt中有相关的find_package命令。而安装ROS会自带OpenCV,它通常和自己默认的安装目录不一样,会出现在

  • /usr/include
  • /usr/libx86_64-linux-gnu/

这时如果其他基于C++的功能包(比如VINS-Mono)和cv_bridge-py37有交互的话,就很有可能因为他们编译时所基于的C++的OpenCV版本不一致而报错。C++对于库的版本很敏感,根本比不了python中conda提供的环境隔离方便。所以最好的办法就是维护自己环境的干净和一致性,环境中的C++库都只有一个版本。

2. 更新当前shell的环境变量

编译通过后我们就可以在需要时调用,基于ROS的工作空间覆盖机制,我们source了以后就会有限搜索新的路径,从而覆盖掉/opt/ros/melodic/share 里的cv_bridge.

# Extend environment with new package
source install/setup.bash --extend  # 或者使用绝对路径

打印环境变量验证

echo $ROS_PACKAGE_PATH
  • source前: /opt/ros/melodic/share
  • source后: /home/qyz/catkin_ws/cv-bridge-py3.7/install/share:/opt/ros/melodic/share

这时在当前环境中就可以正常使用python3解释器跑py脚本了

3. 附录

Why use source xxx --extend(为什么使用source xxx --extend)

The source command generally overwrites one another in the sense that if you have 2 separate cakin workspaces A and B, if you perform source devel/setup.bash in workspace A and then perform source devel/setup.bash in workspace B, the second command will override the first command and the packages in workspace A will not be available to you.

To overcome this, in workspace A, perform source devel/setup.bash as usual but after this, going into workspace B ensure to perform source devel/setup.bash --extend and now packages from both workspaces will be enabled. In a similar light, perhaps you could try source /opt/ros/melodic/setup.bash and then source devel/setup.bash --extend in your workspace
大概意思就是连续的source后边的会覆盖前边的,对后一个使用extend就会保留前面的了。

参考链接

  1. https://shliang.blog.csdn.net/article/details/117230220?spm=1001.2101.3001.6650.1&utm_medium=distribute.pc_relevant.none-task-blog-2%7Edefault%7ECTRLIST%7Edefault-1-117230220-blog-106771848.pc_relevant_default&depth_1-utm_source=distribute.pc_relevant.none-task-blog-2%7Edefault%7ECTRLIST%7Edefault-1-117230220-blog-106771848.pc_relevant_default&utm_relevant_index=2
  2. https://stackoverflow.com/questions/49221565/unable-to-use-cv-bridge-with-ros-kinetic-and-python3?rq=1
  3. https://answers.ros.org/question/349276/ros-commands-no-longer-working-after-source-catkin_wsdevelsetupbash/

你可能感兴趣的:(深度学习,ubuntu,python,深度学习,计算机视觉)