My Raspberry Pi 2 just arrived in the mail yesterday, and man is this berry sweet.
This tiny little PC packs a real punch with a 900mhz quadcore processor and 1gb of RAM. To give some perspective, the Raspberry Pi 2 is faster than the majority of the desktops in my high school computer lab.
Anyway, since the announcement of the Raspberry Pi 2 I’ve been getting a lot of requests to provide detailed installation instructions for OpenCV and Python.
So if you’re looking to get OpenCV and Python up-and-running on your Raspberry Pi, look no further!
In the rest of this blog post I provide detailed installation instructions for both the Raspberry Pi 2 and the Raspberry Pi B+.
I’ve also provided install timings for each step. Some of these steps require a lot of processing time. For example, compiling the OpenCV library on a Raspberry Pi 2 takes approximately 2.8 hours versus the 9.5 hours on the Raspberry Pi B+, so please plan your install accordingly.
Finally, it’s worth mentioning that we’ll be utilizing the Raspberry Pi inside the PyImageSearch Gurus computer vision course. Our projects will include home surveillance applications such as detecting motion and tracking people in a room.
Here’s a quick example of detecting motion and tracking myself as I walk around my apartment on the phone:
Install OpenCV and Python on your Raspberry Pi 2 and B+
UPDATE: The tutorial you are reading now covers how to install OpenCV 3 with Python 2.7 and Python 3 bindings on Raspbian Wheezy. Raspbian Jessie has now replaced Raspbian Wheezy and if this is the first time you are reading this tutorial then in all likelihood you are using Raspbian Jessie. Please use the following updated guides to help you install OpenCV + Python on your Raspberry Pi.
- How to install OpenCV 3.0 on Raspbian Jessie.
- Install guide: Raspberry Pi 3 + Raspbian Jessie + OpenCV 3.
I’m going to assume that you have either your Raspberry Pi 2 or Raspberry Pi B+ unboxed and setup. If you don’t have a Raspberry Pi yet, I definitely suggest picking one up. They are super cheap and a lot of fun to play with.
Personally, I prefer to spend a little extra money and purchase from Canakit — their shipping is fast and reliable, plus their complete ready-to-go bundles are really nice.
Anyway, let’s get into the OpenCV and Python install instructions.
Step 0:
Again, I’m going to assume that you have just unboxed your Raspberry Pi 2/B+. Open up a terminal and we’ll start by updating and upgrading installed packages, followed by updating the Raspberry Pi firmware:
$ sudo apt-get update $ sudo apt-get upgrade $ sudo rpi-update
Step 1:
Install the required developer tools and packages:
$ sudo apt-get install build-essential cmake pkg-config
Both build-essential
and pkg-config
are likely already installed, but just in case they are not, be sure to include them in your apt-get
command.
Timings:
Raspberry Pi B+: < 2 minutes
Raspberry Pi 2: < 40 seconds
Step 2:
Install the necessary image I/O packages. These packages allow you to load various image file formats such as JPEG, PNG, TIFF, etc.
$ sudo apt-get install libjpeg8-dev libtiff4-dev libjasper-dev libpng12-dev
Timings:
Raspberry Pi B+: < 5 minutes
Raspberry Pi 2: < 30 seconds
Step 3:
Install the GTK development library. This library is used to build Graphical User Interfaces (GUIs) and is required for the highgui
library of OpenCV which allows you to view images on your screen:
$ sudo apt-get install libgtk2.0-dev
Timings:
Raspberry Pi B+: < 10 minutes
Raspberry Pi 2: < 3 minutes
Step 4:
Install the necessary video I/O packages. These packages are used to load video files using OpenCV:
$ sudo apt-get install libavcodec-dev libavformat-dev libswscale-dev libv4l-dev
Timings:
Raspberry Pi B+: < 5 minutes
Raspberry Pi 2: < 30 seconds
Step 5:
Install libraries that are used to optimize various operations within OpenCV:
$ sudo apt-get install libatlas-base-dev gfortran
Timings:
Raspberry Pi B+: < 2 minutes
Raspberry Pi 2: < 30 seconds
Step 6:
Install pip
:
$ wget https://bootstrap.pypa.io/get-pip.py $ sudo python get-pip.py
Timings:
Raspberry Pi B+: < 2 minutes
Raspberry Pi 2: < 30 seconds
Step 7:
Install virtualenv
and virtualenvwrapper
:
$ sudo pip install virtualenv virtualenvwrapper $ sudo rm -rf ~/.cache/pip
Then, update your ~/.profile
file to include the following lines:
# virtualenv and virtualenvwrapper export WORKON_HOME=$HOME/.virtualenvs source /usr/local/bin/virtualenvwrapper.sh
Reload your .profile
file:
$ source ~/.profile
Create your computer vision virtual environment:
$ mkvirtualenv cv
Timings:
Raspberry Pi B+: < 2 minutes
Raspberry Pi 2: < 2 minutes
Step 8:
Now we can install the Python 2.7 development tools:
$ sudo apt-get install python2.7-dev
Note: Yes, we are going to use Python 2.7. OpenCV 2.4.X does not yet support Python 3 and OpenCV 3.0 is still in beta. It’s also unclear when the Python bindings for OpenCV 3.0 will be complete so I advise to stick with OpenCV 2.4.X for the time being.
We also need to install NumPy since the OpenCV Python bindings represent images as multi-dimensional NumPy arrays:
$ pip install numpy
Timings:
Raspberry Pi B+: < 45 minutes
Raspberry Pi 2: < 15 minutes
Step 9:
Download OpenCV and unpack it:
$ 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
Setup the build:
$ 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 ..
Timings:
Raspberry Pi B+: < 3 minutes
Raspberry Pi 2: < 1.5 minutes
Compile OpenCV:
$ make
Important: Make sure you’re in the cv
virtual environment so OpenCV is compiled against the virtual environment Python and NumPy. Otherwise, OpenCV will be compiled against the system Python and NumPy which can lead to problems down the line.
Timings:
Raspberry Pi B+: < 9.5 hours
Raspberry Pi 2: < 2.8 hours
Finally, we can install OpenCV:
$ sudo make install $ sudo ldconfig
Timings:
Raspberry Pi B+: < 3 minutes
Raspberry Pi 2: < 1 minute
Step 10:
If you’ve gotten this far in the guide, OpenCV should now be installed in /usr/local/lib/python2.7/site-packages
But in order to utilize OpenCV within our cv
virtual environment, we first need to sym-link OpenCV into 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, we can give our OpenCV and Python installation a test drive:
$ workon cv $ python >>> import cv2 >>> cv2.__version__ '2.4.10'
OpenCV and Python is now successfully installed on your Raspberry Pi!
Here is an example of me ssh’ing (with X11 forwarding) into my Raspberry Pi, followed by loading and displaying an image:
What's next? We recommend PyImageSearch University.
86 total classes • 115+ hours of on-demand code walkthrough videos • Last updated: October 2024
★★★★★ 4.84 (128 Ratings) • 16,000+ Students Enrolled
I strongly believe that if you had the right teacher you could master computer vision and deep learning.
Do you think learning computer vision and deep learning has to be time-consuming, overwhelming, and complicated? Or has to involve complex mathematics and equations? Or requires a degree in computer science?
That’s not the case.
All you need to master computer vision and deep learning is for someone to explain things to you in simple, intuitive terms. And that’s exactly what I do. My mission is to change education and how complex Artificial Intelligence topics are taught.
If you're serious about learning computer vision, your next stop should be PyImageSearch University, the most comprehensive computer vision, deep learning, and OpenCV course online today. Here you’ll learn how to successfully and confidently apply computer vision to your work, research, and projects. Join me in computer vision mastery.
Inside PyImageSearch University you'll find:
- ✓ 86 courses on essential computer vision, deep learning, and OpenCV topics
- ✓ 86 Certificates of Completion
- ✓ 115+ hours of on-demand video
- ✓ Brand new courses released regularly, ensuring you can keep up with state-of-the-art techniques
- ✓ Pre-configured Jupyter Notebooks in Google Colab
- ✓ Run all code examples in your web browser — works on Windows, macOS, and Linux (no dev environment configuration required!)
- ✓ Access to centralized code repos for all 540+ tutorials on PyImageSearch
- ✓ Easy one-click downloads for code, datasets, pre-trained models, etc.
- ✓ Access on mobile, laptop, desktop, etc.
Summary
In this blog post I detailed how to install OpenCV and Python on your Raspberry Pi 2 or Raspberry Pi B+. Timings for each installation step were also provided so you could plan out the install accordingly.
As the Raspberry Pi (along with Raspbian/NOOBS) evolves the installation instructions will likely change. If you run across any edge cases or variations in the install instructions, please feel free to let me know. While I can’t promise that I can reply to every email, but I think it would be good to curate a list of methods to setup OpenCV and Python on Raspberry Pi systems.
And in future blog posts we’ll explore how to utilize the camera add-on for the Raspberry Pi.
Until then, take a look at the PyImageSearch Gurus computer vision course. We’ll be utilizing the Raspberry Pi inside the course for a few projects, including building a home surveillance application that can detect motion and people in rooms.
Join the PyImageSearch Newsletter and Grab My FREE 17-page Resource Guide PDF
Enter your email address below to join the PyImageSearch Newsletter and download my FREE 17-page Resource Guide PDF on Computer Vision, OpenCV, and Deep Learning.