Many friends who use ROS will find that many functions of ROS are compiled based on python2, which causes ROS to report errors when calling cv_bridge with python3, because ROS calls cv_bridge of python2 by default. This article is to help you guys how to call python3's cv_bridge in ROS, I hope it will be helpful to you.
The following command can test whether you can call python3's cv_bridge normally
Open Python3
python3
import package
import cv_bridge
from cv_bridge import CvBridge, CvBridgeError
Here is the description of the author's situation (ubuntu18.04(arm), python3.6.9):
the first sentence import cv_bridge is no problem, but the second line imports the last two words from there, it will report an error, because python3 does not have it, so you need to Perform the following operations to solve this problem.
Table of contents
- 1. Installation environment
-
- 1.1 Create a new workspace (important)
- 1.2 Compile workspace
- 1.3 Pull and decompress the source code of cv_bridge to the local (you can [click here to download] (https://download.csdn.net/download/weixin_44355653/88189825?spm=1001.2014.3001.5503) after decompression, copy and paste and move to directory==py3_cv_bridge_ws/src==in)
- 1.4 Compile workspace
-
- 1.4.1 Compilation error problems and solutions
- 1.4.2 View ==libbost_python*== files (my arm ’s ubuntu18.04 here , if yours is x86 x, it’s in the /usr/lib/x86-64-linux-gnu folder))
- 1.4.3 Enter the error file vision_opencv/cv_bridge/CMakeLists.txt, change python37 to python3, and then recompile.
- 1.4.4 The appearance of successful compilation
- 1.5 Add environment variables
- 1.6 To check whether it is successful, you can call cv_bridge of python3
1. Installation environment
sudo apt-get install python3-dev python3-numpy python3-yaml ros-melodic-cv-bridge python3-rospkg-modules
pip3 install pip --update
pip3 install rosdep rosinstall catkin_pkg
1.1 Create a new workspace (important)
mkdir -p py3_cv_bridge_ws/src
1.2 Compile workspace
catkin_init_workspace
1.3 Pull and decompress the cv_bridge source code to the local (you can click here to download and decompress , copy and paste and move to the directory if you can’t pull it out after timeoutpy3_cv_bridge_ws/srcmiddle)
git clone https://github.com/ros-perception/vision_opencv.git
1.4 Compile workspace
cd ~/py3_cv_bridge_ws
catkin_make install -DPYTHON_EXECUTABLE=/usr/bin/python3
1.4.1 Compilation error problems and solutions
1.4.2 View ==libbost_python*== files (my arm ’s ubuntu18.04 here , if yours is x86 x, it’s in the /usr/lib/x86-64-linux-gnu folder))
cd /usr/lib/aarch64-linux-gnu/
ls libboost_python*
You will see the following information
1.4.3 Enter the error file vision_opencv/cv_bridge/CMakeLists.txt, change python37 to python3, and then recompile.
1.4.4 The appearance of successful compilation
Seeing 100% means that the compilation is successful
1.5 Add environment variables
This step is added so that when you want to use python3's cv_bridge, it can be called automatically by default instead of calling python2's cv_bridge.
Add the following command to the last line of the .bashrc file
source /home/你系统的名字/py3_cv_bridge_ws/install/setup.bash --extend
1.6 To check whether it is successful, you can call cv_bridge of python3
Use the two lines of code that encountered an error at the beginning of the article for inspection and testing
Open Python3
python3
import package
import cv_bridge
from cv_bridge import CvBridge, CvBridgeError
If there is no error reported here, it proves that the cv_bridge of python3 can be called successfully. When writing a ROS node, if you want to use cv_bridge of py3 or py2, you only need to add the command in the first line of the program file. You can choose to call python3, then add it in the first line of the program
file The following two lines of commands
#!/usr/bin/env python3
# encoding: utf-8
To call python, add the following two lines of commands in the first line of the program file
#!/usr/bin/env python
# encoding: utf-8