Installing OpenCV on Raspberry Pi
This article introduces how to install OpenCV running on Python2 and Python3 on Raspberry Pi.
Installing OpenCV for Python2 is very simple, just a few commands will do. Installing OpenCV for Python3 is more complicated and requires compilation. Don’t worry, this article assumes you are a beginner and will guide you step by step. This tutorial has been personally tested by the author and is considered the most reliable tutorial available online.
Original Author: Tommy Zihao, Tongji University Open Source Software Association, WeChat Official Account: Tommy’s Research Room
Please link back to the original article when reprinting or copying and indicate the source: Tommy Zihao.
Original article address: http://suo.im/5gS6if Published on 2018-10-05
OpenCV is an open-source computer vision library loved by programmers, with powerful built-in functions and an open-source community. OpenCV, combined with the portable and inexpensive Raspberry Pi, can directly read videos from the Raspberry Pi camera PiCamera to perform various computer vision developments such as face recognition, edge detection, semantic segmentation, autonomous driving, and image recognition. Many excellent open-source projects, such as Google’s artificial intelligence framework TensorFlow and the face recognition open-source project face_recognition, require OpenCV to be installed as a prerequisite. Many undergraduate graduation projects also need to use it.
There are many tutorials online about installing OpenCV on Raspberry Pi, written by both foreigners and Chinese, but they are all not very reliable. After 7 months of exploration and repeated failures, the author finally found a reliable installation process and summarized it in this article. All processes have been personally tested and validated.
Installing OpenCV for Python2 is very simple, just a few commands will do. Installing OpenCV for Python3 is more complicated and requires compilation. The author recommends installing both. Don’t worry, this article assumes you are a beginner and will guide you step by step. This tutorial has been personally tested by the author and is considered the most reliable tutorial available online.
After following this tutorial to install, you can quickly get started using Raspberry Pi to perform face recognition as taught by Tommy.
Installation Prerequisites
1. Configure the Raspberry Pi’s Raspbian Operating System
The system used in this tutorial is the Raspbian-stretch operating system released by the Raspberry Pi Foundation on June 27, 2018, download link.
Absolute beginners can refer to these two articles I wrote, from unboxing to completing various configurations of Raspberry Pi tutorials.
Tommy’s Raspberry Pi Series Tutorial 01: Raspberry Pi Boot Tutorial
Tommy’s Raspberry Pi Series Tutorial 02: One-time Configuration of Raspberry Pi
2. Switch to domestic apt-get download sources and pip download sources
To prevent slow download speeds.
Beginners who do not know how to change the source can refer to this article for a one-time configuration of Raspberry Pi.
3. If you have the official Raspberry Pi camera Picamera, you need to configure it correctly as follows
If you do not have the official camera, it does not affect the installation.
Enter the following command in the command line, this command means to open the modules file with the nano editor:
sudo nano /etc/modules
Add a line at the end of this file
bcm2835-v4l2
This is the effect
First press ctrl
+ o
to save, then press enter, and then press ctrl
+ x
to exit the nano editor and return to the command line interface.
Input the command
vcgencmd get_camera
If you get the following result, it proves that the camera is connected successfully.
You can enter the command
raspistill -o image.jpg
To take a photo with the camera, named image.jpg, stored in the /pi/home path, which is the path displayed in the upper left corner of the resource manager when opened. If you can see the red light on the camera and there is a photo in the directory, it further indicates that the camera is configured correctly.
Installing OpenCV Running on Python2 on Raspberry Pi
1. Installation
Open the command line interface of Raspberry Pi, and two commands can complete the installation. The first command takes about half an hour, please be patient. The second command only takes a few seconds.
Tommy’s friendly reminder:
It is recommended to run the first command using the command line tool that comes with the Raspberry Pi desktop, rather than using a remote ssh connection. Because the execution time of the command is too long, if the ssh disconnects midway, you cannot know whether it has been installed successfully.
sudo apt-get install libopencv-dev
sudo apt-get install python-opencv
2. Testing OpenCV on Python2
After installation, enter python
or python2
in the command line and press enter.
import cv2
If the result shown in the image below appears, it indicates that OpenCV is successfully installed in the Python2 environment.
You can also enter
cv2.__version__
To check the OpenCV version number.
Why is it called cv2 instead of opencv? This is because OpenCV is developed based on C/C++, with two versions, the ‘cv’ version API is developed in C language, and the ‘cv2’ version API is developed in C++ language. To maintain backward compatibility, it is called ‘cv2’, but we all know that cv2 is the real OpenCV.
You can also enter the following three commands in the desktop command line to call the Raspberry Pi camera, displaying the captured image on the desktop, press ctrl
+ c
to exit.
git clone https://github.com/TommyZihao/opencvtest.git
cd opencvtest
python2 testopencv.py
Installing OpenCV Running on Python3 on Raspberry Pi
1. Install numpy
Open the command line interface and enter the following command to install the Python scientific computing library numpy.
sudo pip3 install numpy
2. Expand the root directory to the entire SD card in Raspberry Pi settings
Enter the command in the command line to enter the Raspberry Pi configuration interface. Use the up and down keys and left and right keys to switch the cursor position.
sudo raspi-config
Seventh line: Advanced Options
Select Expand Filesystem to expand the root directory to this SD card, making full use of the storage space of the SD card. If this step is not done, subsequent commands may hang. Exit the settings interface and restart the Raspberry Pi.
sudo reboot
3. Install the libraries required for OpenCV
Run the following eight commands one by one. It takes about seven minutes (note that in the third-to-last command, four -dev software packages need to be installed).
sudo apt-get install build-essential git cmake pkg-config -y
sudo apt-get install libjpeg8-dev -y
sudo apt-get install libtiff5-dev -y
sudo apt-get install libjasper-dev -y
sudo apt-get install libpng12-dev -y
sudo apt-get install libavcodec-dev libavformat-dev libswscale-dev libv4l-dev -y
sudo apt-get install libgtk2.0-dev -y
sudo apt-get install libatlas-base-dev gfortran -y
4. Download OpenCV
Enter the following three commands in the command line to download two compressed packages to the Raspberry Pi’s /home/pi/Downloads directory. The first compressed package is 86.8MB, and the second compressed package is 54.5MB:
cd
wget https://github.com/Itseez/opencv/archive/3.4.0.zip
wget https://github.com/Itseez/opencv_contrib/archive/3.4.0.zip
If the download speed is very slow (for example, a few KB per second):
Method 1: You can enter the link after wget in your computer browser to download the compressed package, and then use Fillzilla or USB drive to transfer the file to the Raspberry Pi’s /home/pi/Downloads directory (must not be wrong).
Method 2: You can download these two compressed packages from Baidu Netdisk on your computer and then use Fillzilla or USB drive to transfer the files to the Raspberry Pi’s /home/pi/Downloads directory (must not be wrong).
Unzip these two compressed packages
cd /home/pi/Downloads
unzip opencv-3.4.0.zip
unzip opencv_contrib-3.4.0.zip
5. Set compilation parameters
Set compilation parameters
cd /home/pi/Downloads/opencv-3.4.0
mkdir build
cd build
Set CMAKE parameters, note that the following is a single command (including the last two dots), please be patient for about fifteen minutes:
cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local -D INSTALL_C_EXAMPLES=ON -D INSTALL_PYTHON_EXAMPLES=ON -D OPENCV_EXTRA_MODULES_PATH=/home/pi/Downloads/opencv_contrib-3.4.0/modules -D BUILD_EXAMPLES=ON -D WITH_LIBV4L=ON PYTHON3_EXECUTABLE=/usr/bin/python3.5 PYTHON_INCLUDE_DIR=/usr/include/python3.5 PYTHON_LIBRARY=/usr/lib/arm-linux-gnueabihf/libpython3.5m.so PYTHON3_NUMPY_INCLUDE_DIRS=/home/pi/.local/lib/python3.5/site-packages/numpy/core/include ..
Use the image below to determine whether you successfully configured CMAKE. If it fails, it may be because the paths of the two compressed packages do not strictly follow the above requirements. If successful, you can start the most important compilation.
6. Compilation
The last step, and also the most important step: compilation.
Ensure that the Raspberry Pi has at least 5G of storage space, and it is recommended to run this command using the command line tool on the Raspberry Pi desktop, rather than using a remote ssh connection. Because the execution time of the command is too long, if the ssh disconnects midway, you cannot know whether it has been installed successfully.
cd /home/pi/Downloads/opencv-3.4.0/build
make
After two hours of compilation, it is completed at 40%
Wait for five hours for the compilation. Note that during this period, the Raspberry Pi should have sufficient power supply and should not run other tasks to avoid memory errors.
After the make
command is completed, execute the following command, which takes about a minute:
sudo make install
7. Testing OpenCV on Python3
After installation, enter python3
in the command line and press enter.
import cv2
Press enter
cv2.__version__
Press enter
If the result shown in the image below appears, it indicates that OpenCV is successfully installed in the Python3 environment.
Tommy’s Note:
I started trying to install OpenCV on Raspberry Pi for the first time on March 7, 2018, and after countless tutorials and numerous failures. Some tutorials require configuring virtual environments, some require modifying memory allocation; some tutorials use all four CPU cores of the Raspberry Pi to compile, and each time there would be errors; some tutorials are extremely unfriendly to beginners, leaving them completely unsure how to operate. After seven months of hard work and success, I want to write this article with my blood and tears to help every beginner quickly get started instead of being stuck with bugs.
This article will continue to be updated and corrected in the future, with the latest version based on Tommy’s blog collection on GitHub.
Please link back to the original article when reprinting or copying and indicate the source: Tommy Zihao.
Original article address: http://suo.im/5gS6if Published on 2018-10-05
Tongji University Open Source Software Association
Chongqing University Raspberry Pi Enthusiasts Club, Artificial Intelligence Association, Innovation Practice Center
References and Further Reading
[Raspberry Pi] Raspberry Pi + OpenCV3.4 + python3.5 success and attention to details
Installing Python-OpenCV on Raspberry Pi
Configuration tutorial for OpenCV3.4 based on Raspberry Pi 3B + Python3.5