The following guide will take you though the steps to install OpenCV, an open source computer vision library on the Raspberry Pi 3 embedded computer running the Raspbian Jessie operating system.
Timings between steps have been included so you know the approximate time between steps. This also helps you know that the pi is not stuck if a step is suppose to take a significant amount of time to complete
If you are able please hook up a to a wired internet connection as this will speed up the process of downloading the rather large amount of files needed. Also I suggest adding a heat sink to the main processor, as during the compiling of OpenCV the raspberry will reach its thermal envelope thus slowing down the install process significantly.
The Raspberry pi 3 has the following improved features of the previous model. However it does maintain the same form factor and still sips the power.
- A 1.2GHz 64-bit quad-core ARMv8 CPU
- 802.11n Wireless LAN
- Bluetooth 4.1
- Bluetooth Low Energy (BLE)
Install time is approx 3-4 hours…
Step 1: Expanding the Filesystem
First lets expand the filesystem if you haven’t already. This step will free up the free available space on the micro SD card:
$ sudo raspi-confi
A configuration screen will appear. Please go ahead and select the first option Expand File System, hit Enter, and use the arrow key to select the Finish button, and then restart your new Raspberry Pi:
You can verify that the step worked by executing df –h and looking at the updated space.
$ df -h Filesystem Size Used Avail Use% Mounted on /dev/root 7.2G 3.2G 3.7G 47% / devtmpfs 448M 0 458M 0% /dev tmpfs 462M 0 453M 0% /dev/shm tmpfs 462M 6.3M 456M 1% /run tmpfs 5.0M 4.0K 5.0M 1% /run/lock tmpfs 462M 0 462M 0% /sys/fs/cgroup /dev/mmcblk0p1 60M 20M 41M 33% /boot tmpfs 94M 0 94M 0% /run/user/1000
The Raspbian file system has now been expanded to include all 8GB of the micro SD card.
with the filesystem expanded, we have already used a significant portion of the 8GB card!
Step 2: Install all Dependencies
Please understand that OpenCV itself takes around 1h 11m to compile.
First update and upgrade existing packages:
Timing: ~1m 28s
Next we will install a few awesome developer tools, including CMake, CMake helps configure OpenCV to build process, Cmake is a very cool tool that will ensure the rather large install of OpenCV completes with no major surprises:
$ sudo apt-get install build-essential cmake pkg-config
Next we will us image packages these will load image file formats when needed from the main disk. Examples of file formats include PNG, JPEG, TIFF and so forth.
$ sudo apt-get install libjpeg-dev libtiff5-dev libjasper-dev libpng12-dev
Video I/O packages will also be needed. This library help’s read various video file formats from both disk and work directly with live video streams:
$ sudo apt-get install libavcodec-dev libavformat-dev libswscale-dev libv4l-dev $ sudo apt-get install libxvidcore-dev libx264-dev
OpenCV has a module highgui this is used too display images to the screen and build basic GUI’s. However to compile the highgui module, we will need to install the GTK development library:
Next we will use a few dependencies that with help optimize OponCV, It is always good practice to use them when doing computer vision on weaker hardware such as the Pi.:
$ sudo apt-get install libatlas-base-dev gfortran
These libraries are important as the Pi has limited resources. Remember a Pi is not a performance race car but rather a energy efficient hybrid.
Now we will install both Python 2.7 and Python 3 header files so that we can compile OpenCV with Python bindings, Use the following command to complete this step before moving on to downloading the source code for OpenCV.
$ sudo apt-get install python2.7-dev python3-dev
Do not skip this step as it will likely result in a error related to the Python.h header file not being found when you compile OpenCV using make.
Step 3: Downloading the OpenCV source code.
All the dependencies should now be installed, go ahead and take the 3.2.0 archive of OpenCV from the official OpenCV repository. (Helpful Note: As newer versions of openCV become avalible, you can easily replace 3.2.0 with the latest matching version):
$ cd ~ $ wget -O opencv.zip https://github.com/Itseez/opencv/archive/3.2.0.zip $ unzip opencv.zip
$ wget -O opencv_contrib.zip https://github.com/Itseez/opencv_contrib/archive/3.2.0.zip $ unzip opencv_contrib.zip
Below is the full link to the online archive if you have any problems with the code above.
Next we will be installing pip , a well maintained Python package manager:
Go to the following link to Install a Python virtual environment.
Installing NumPy on your Raspberry Pi
Navigate to the cv virtual environment if you are not in it currently. Python has one dependency we need to install called NumPy, a nifty Python package used for numerical processing:
$ pip install numpy
NumPy installation will take a long time to install. This is a good time to take a break or work on another project that you have been putting off. (Ya that one)
Helpful Note: While it may look frozen at times let it continue to process, there are many dependencies it needs to load.
Step 4: Compile and Install OpenCV
We can now compile and install OpenCV! First make sure you are in the “cv” virtual environment we created earlier, if not, use the below command:
$ workon cv
Once you are in the “cv” virtual environment, we can setup the build using CMake:
We need to look though CMake to ensure that everything will install like we want start by scrolling down to the section titled Python 2.7 and Python 3 .
If you are using OpenCV 3 for Python 2.7, make sure that the Python 2 section includes valid paths to the Interpreter , numpy, Libraries and the packages path, Same goes for Python 3 if you are going that route.
The Interpreter points to the python2.7 binary located in the cv virtual environment we made earlier, and the numpy variable will point to the NumPy install.
You can now compile OpenCV (Computer Vision) on your Pi: \o/
Helpful Note: Compiling OpenCV computer vision software in approximately 72 minutes on the Raspberry Pi 3 which is a 24% improvement over the ~95 minutes for the Raspberry Pi 2.
The –j4 command controls the number of compute cores that will be used when compiling OpenCV 3. The Raspberry Pi 3 has four compute cores, so if we use a value of 4 it will allow OpenCV to compile much faster.
The below command will kick off the install of the OpenCV:
$ sudo make install $ sudo ldconfig
Step 5: Finish installing OpenCV on your Raspberry Pi
There only a few more steps needed to follow to finish out the install.
For Python 2.7:
Provided your Step 5 finished without error, OpenCV should now be installed in/usr/local/lib/python2.7/site–packages . You can verify this using the ls command:
$ ls -l /usr/local/lib/python2.7/site-packages/ total 1852 -rw-r--r-- 1 root staff 1885772 May 5 10:00 cv2.so
Helpful Note: Once in a while OpenCV may be installed in /usr/local/lib/python2.7/dist–packages rather than site–packages . If you do not find the cv 2.so bindings in site–packages , check dist–packages instead.
In the final step we reference the OpenCV bindings into the previously created CV virtual environment for Python 2.7:
For Python 3:
After running make install , the Open-CV bindings should be installed in/usr/local/lib/python3.4/site–packages . Go ahead and verify this with the ls (list Command) command:
$ cd /usr/local/lib/python3.4/site-packages/ $ sudo mv cv2.cpython-34m.so cv2.so
After renaming to cv2.so , we can sym-link our OpenCV bindings into the cv virtual environment for Python 3.4:
Step 6: OpenCV 3.2 is now installed!
Congratulation’s, you now have OpenCV 3 installed on your Raspberry Pi 3.
Before we move on however let’s verify that OpenCV installed properly.
Open up a new terminal window, run the source and workon commands that you know and love, and then import the Python + OpenCV bindings:
$ source ~/.profile $ workon cv $ python >>> import cv2 >>> cv2.__version__ '3.2.0' >>>
Final cleanup time, now that OpenCV has been installed, you can remove both the opencv–3.2.0, andopencv_contrib–3.2.0 directories to free up as much critical disk space on the SD card that you can.
$ rm -rf opencv-3.2.0 opencv_contrib-3.2.0
Next we will move onto testing Open CV, face detection and face tracking using the official PiCamera.
Here is a link to the tutorial on Single image face detection with OpenCV
This install guide was largely based on the work of Adrian Rosebrock’s page found here. Some parts were removed and simplified. I would suggest heading over to Adrian’s site http://www.pyimagesearch.com for more in-depth tutorials on using OpenCV with the Raspberry Pi.