Python and OpenCV installation tutorial on Raspberry Pi 2 or Raspberry Pi B +, pythonopencv
My Raspberry Pi 2 just arrived yesterday. This guy looks cute.
This guy has a 4-core MHz processor and 1 GB memory. You know, Raspberry Pi 2 is much faster than most computers in my middle school computer lab.
Since the release of Raspberry Pi 2, I have received many requests asking me to write a detailed description of installing OpenCV and Python on it.
So if you want to start and run OpenCV and Python in Raspberry Pi, let's look at it!
For the rest of the blog post, I will provide detailed installation instructions on Raspberry Pi 2 and Raspberry Pi B +.
I will also describe the installation time of each step. Some of these steps require much processing time. For example, it takes about 2.8 hours to compile OpenCV on Raspberry Pi 2 and 9.5 hours on Raspberry Pi B +. So please arrange your installation accordingly.
Finally, we will use Raspberry Pi in the PyImageSearch Gurus Computer Vision course. Our project will include home monitoring applications, such as motion detection and personnel tracking in the room.
The following is a simple example of motion detection and tracking. At that time, I was making a call to move back and forth in the apartment.
Install OpenCV and Python on Raspberry Pi 2/B +
Assume that you have Raspberry Pi 2/B + installed. If you don't have one, I suggest you buy one, which is both cheap and fun.
Personally, I prefer to pay more for it from Canakit. Their logistics is fast and reliable, and their ready-to-go service is also very good.
Okay, let's start installing OpenCV and Python.
Step 0:
Assume that you have enabled Raspberry Pi 2/B +. Open the terminal, first update and upgrade the installed software package, and then update the Raspberry Pi firmware.
$ sudo apt-get update$ sudo apt-get upgrade$ sudo rpi-update
Step 1:
Install the required Installation Tools and packages:
$ sudo apt-get install build-essential cmake pkg-config
Both build-essential and pkg-config may have been installed. Just in case, we should type them in the apt-get command.
Time consumed:
Raspberry Pi B +: less than 2 minutes
Raspberry Pi 2: less than 40 seconds
Step 2:
Install necessary image I/O packages so that you can read images in JPEG, PNG, TIFF, and other formats.
$ sudo apt-get install libjpeg8-dev libtiff4-dev libjasper-dev libpng12-dev
Time consumed:
Raspberry Pi B +: less than 5 minutes
Raspberry Pi 2: less than 30 seconds
Step 3:
Install the GTK development library, which is used to build the GUI. The highgui library in OpenCV also needs it to display images on the screen.
$ sudo apt-get install libgtk2.0-dev
Time consumed:
Raspberry Pi B +: less than 10 minutes
Raspberry Pi 2: less than 3 minutes
Step 4:
Install necessary video I/O packages. OpenCV needs them to read video files.
$ sudo apt-get install libavcodec-dev libavformat-dev libswscale-dev libv4l-dev
Time consumed:
Raspberry Pi B +: less than 5 minutes
Raspberry Pi 2: less than 30 seconds
Step 5:
Library required for installing OpenCV optimization operations.
$ sudo apt-get install libatlas-base-dev gfortran
Time consumed:
Raspberry Pi B +: less than 2 minutes
Raspberry Pi 2: less than 30 seconds
Step 6:
Install pip:
$ wget https://bootstrap.pypa.io/get-pip.py$ sudo python get-pip.py
Time consumed:
Raspberry Pi B +: less than 2 minutes
Raspberry Pi 2: less than 30 seconds
Step 7:
Install virtualenv and virtualenvwrapper
$ sudo pip install virtualenv virtualenvwrapper
Then, update ~ The/. profile file is as follows:
export WORKON_HOME=$HOME/.virtualenvssource /usr/local/bin/virtualenvwrapper.sh
Reload the. profile file:
$ source ~/.profile
Create your computer vision Virtual Environment
$ mkvirtualenv cv
Time consumed:
Raspberry Pi B +: less than 2 minutes
Raspberry Pi 2: less than 2 minutes
Step 8:
Now we have installed Python 2.7 development tools:
$ sudo apt-get install python2.7-dev
Note: We will use Python2.7. Because OpenCV 2.4.X does not support Python 3, it is not clear when the python interface of OpenCV 3.0 can be improved. Therefore, we recommend that you use OpenCV2.4.X.
We also need to install NumPy because the Python interface of OpenCV uses Numpy multi-dimensional arrays to represent images.
$ pip install numpy
Time consumed:
Raspberry Pi B +: less than 45 minutes
Raspberry Pi 2: less than 15 minutes
Step 9:
Download and decompress OpenCV:
$ wget -O opencv-2.4.10.zip http://sourceforge.net/projects/opencvlibrary/files/opencv-unix/2.4.10/opencv-2.4.10.zip/download$ unzip opencv-2.4.10.zip$ cd opencv-2.4.10
Installation:
$ mkdir build$ cd build$ cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local -D BUILD_NEW_PYTHON_SUPPORT=ON -D INSTALL_C_EXAMPLES=ON -D INSTALL_PYTHON_EXAMPLES=ON -D BUILD_EXAMPLES=ON ..
Time consumed:
Raspberry Pi B +: less than 3 minutes
Raspberry Pi 2: less than 1.5 minutes
Compile OpenCV:
$ make
Important: make sure that in the cv virtual environment, OpenCV should also be compiled according to Python and NumPy in the environment. Otherwise, OpenCV will be compiled with Python and NumPy in the system, and various problems will occur.
Time consumed:
Raspberry Pi B +: less than 9.5 hours
Raspberry Pi 2: less than 2.8 hours
Finally, we install OpenCV:
$ sudo make install$ sudo ldconfig
Time consumed:
Raspberry Pi B +: less than 3 minutes
Raspberry Pi 2: less than 1 minute
Step 10:
At this step, OpenCV should have been installed in/usr/local/lib/python2.7/site-packages.
However, to use OpenCV In the cv virtual environment, we first need to match the link to our site-packages directory:
$ cd ~/.virtualenvs/cv/lib/python2.7/site-packages/$ ln -s /usr/local/lib/python2.7/site-packages/cv2.so cv2.so$ ln -s /usr/local/lib/python2.7/site-packages/cv.py cv.py
Step 11:
Finally, let's test the installation of OpenCV and Python:
$ workon cv$ python>>> import cv2>>> cv2.__version__'2.4.10'
OpenCV and Python have been successfully installed on your Raspberry Pi.
This is an example of running on my Raspberry Pi. I log on to the Raspberry Pi via ssh and then read and display an image.
Summary
In this blog, I have detailed how to install Python and OpenCV on Raspberry Pi 2/Raspberry Pi B +. Their installation time is also provided. Please consider your installation as appropriate.
With the upgrade of the Raspberry Pi, the installation instructions may change. If you encounter some extreme situations or installation instructions change, please feel free to contact me. Of course, I cannot guarantee that each email can be replied, so it is best to summarize the methods for installing OpenCV and Python on Raspberry Pi into a list.
Soon I will talk about how to operate the attached camera on the Raspberry Pi.
Before that, take a look at the PyImageSearch Gurus Computer Vision course. We will use the Raspberry Pi in some of these projects. For example, we will build a monitoring application for motion detection and personnel tracking in the room.