Install OpenCV and Python on your Raspberry Pi 2 and B+

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 2.4 with Python 2.7 bindings on Raspbian Wheezy. This tutorial still works perfectly, but if you are looking to install OpenCV 3 on either Raspbian Wheezy or Raspbian Jessie, please see these tutorials:
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:

Step 1:

Install the required developer tools and packages:
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.
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.
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:
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:
Raspberry Pi B+: < 5 minutes
Raspberry Pi 2: < 30 seconds

Step 5:

Install libraries that are used to optimize various operations within OpenCV:
Raspberry Pi B+: < 2 minutes
Raspberry Pi 2: < 30 seconds

Step 6:

Install pip :
Raspberry Pi B+: < 2 minutes
Raspberry Pi 2: < 30 seconds

Step 7:

Install  virtualenv  and virtualenvwrapper :
Then, update your ~/.profile  file to include the following lines:
Reload your .profile  file:
Create your computer vision virtual environment:
Raspberry Pi B+: < 2 minutes
Raspberry Pi 2: < 2 minutes

Step 8:

Now we can install the Python 2.7 development tools:
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:
Raspberry Pi B+: < 45 minutes
Raspberry Pi 2: < 15 minutes

Step 9:

Download OpenCV and unpack it:
Setup the build:
Raspberry Pi B+: < 3 minutes
Raspberry Pi 2: < 1.5 minutes
Compile OpenCV:
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.
Raspberry Pi B+: < 9.5 hours
Raspberry Pi 2: < 2.8 hours
Finally, we can install OpenCV:
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:

Step 11:

Finally, we can give our OpenCV and Python installation a test drive:
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:

So, what’s next?

Congrats! You have a brand new, fresh install of OpenCV on your Raspberry Pi — and I’m sure you’re just itching to leverage your Raspberry Pi to build some awesome computer vision apps.
But I’m also willing to bet that you’re just getting started learning computer vision and OpenCV, and you’re probably feeling a bit confused and overwhelmed on where exactly to start.
Personally, I’m a big fan of learning by example, so a good first step would be to read this blog post on accessing your Raspberry Pi Camera with the picamera module. This tutorial details the exact steps you need to take to (1) capture photos from the camera module and (2) access the raw video stream.
And if you’re really interested in leveling-up your computer vision skills, you should definitely check out my book, Practical Python and OpenCV + Case Studies. My book not only covers the basics of computer vision and image processing, but also teaches you how to solve real world computer vision problems including face detection in images and video streams,object tracking in video, and handwriting recognition.
All code examples covered in the book are guaranteed to run on the Raspberry Pi 2 and Pi 3 as well! Most programs will also run on the B+ and Zero models, but might be a bit slow due to the limited computing power of the B+ and Zero.
So let’s put your fresh install of OpenCV on your Raspberry Pi to good use — just click here to learn more about the real-world projects you can solve using your Raspberry Pi + Practical Python and OpenCV .


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.



Page views