参考资料:
https://stackoverflow.com/questions/49221565/unable-to-use-cv-bridge-with-ros-kinetic-and-python3?rq=1
https://github.com/ros-perception/vision_opencv
https://github.com/ros/geometry
https://github.com/ros/geometry2
从github
上下载cv_bridge
的源码
git clone https://github.com/ros-perception/vision_opencv.git
git clone https://github.com/ros/geometry
git clone https://github.com/ros/geometry2
注意!!!下面是默认的环境
cd .. && catkin config -DPYTHON_EXECUTABLE=/usr/bin/python3 -DPYTHON_INCLUDE_DIR=/usr/include/python3.6m -DPYTHON_LIBRARY=/usr/lib/x86_64-linux-gnu/libpython3.6m.so
catkin config --install
catkin build cv_bridge
source install/setup.bash --extend
这里我使用的是conda的环境
# 修改后的方法
catkin config -DPYTHON_EXECUTABLE=/home/znfs/anaconda3/envs/detectron/bin/python3 -DPYTHON_INCLUDE_DIR=/home/znfs/anaconda3/envs/detectron/include/python3.6m -DPYTHON_LIBRARY=/home/znfs/anaconda3/envs/detectron/lib/libpython3.6m.so
catkin_make -DPYTHON_EXECUTABLE=/home/znfs/anaconda3/envs/detectron/bin/python3
Valid: 验证
from cv_bridge.boost.cv_bridge_boost import getCvType
需要将PYTHONPATH环境变量中,我们编译的这个cv_bridge的lib放到前面。
PYTHONPATH=/home/znfs/project_ws/dynamic_grasp/devel/lib/python3/dist-packages:/opt/ros/melodic/lib/python2.7/dist-packages:/home/znfs/project_ws/baxter/devel/lib/python2.7/dist-packages
Tips: 这里介绍一种方法在程序中重新设置环境变量,使用此方法可以将我们想要的环境置前
import os
# print(os.environ['PYTHONPATH'])
python_env = os.environ['PYTHONPATH'].split(':')
print('PYTHON_ENV change before:', python_env)
sort_pe = ':'.join(sorted(python_env, key=lambda k: 'python3' in k, reverse=True))
print('PYTHON_ENV change after:', sort_pe)
os.environ['PYTHONPATH']=sort_pe