Title: Install OpenCV and Python on your Raspberry Pi 2 and B+ - PyImageSearch

URL Source: https://pyimagesearch.com/2015/02/23/install-opencv-and-python-on-your-raspberry-pi-2-and-b/

Published Time: 2015-02-23T15:00:13+00:00

Markdown Content:
[![Image 1: raspberry_pi_2_3](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20600%20483'%3E%3C/svg%3E)](https://pyimagesearch.com/wp-content/uploads/2015/02/raspberry_pi_2_3.jpg)

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](https://pyimagesearch.com/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:

[![Image 2: pyimagesearch_gurus_motion_detection](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20600%20438'%3E%3C/svg%3E)](https://pyimagesearch.com/pyimagesearch-gurus/)

**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**_.](https://pyimagesearch.com/2015/10/26/how-to-install-opencv-3-on-raspbian-jessie/)
*   [Install guide: Raspberry Pi 3 + _**Raspbian Jessie**_ + OpenCV 3.](https://pyimagesearch.com/2016/04/18/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](http://www.amazon.com/gp/product/B00G1PNG54/ref=as_li_tl?ie=UTF8&camp=1789&creative=390957&creativeASIN=B00G1PNG54&linkCode=as2&tag=trndingcom-20&linkId=ALHWRQ5K43RPQ4RX) — 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:

[![Image 3: raspberry_pi_example](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20600%20391'%3E%3C/svg%3E)](https://pyimagesearch.com/wp-content/uploads/2015/02/raspberry_pi_example.jpg)

### What's next? We recommend [PyImageSearch University](https://pyimagesearch.com/pyimagesearch-university/?utm_source=blogPost&utm_medium=bottomBanner&utm_campaign=What%27s%20next%3F%20I%20recommend).

**Course information:**  
84 total classes • 114+ hours of on-demand code walkthrough videos • Last updated: February 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:**

*   ✓ **84 courses** on essential computer vision, deep learning, and OpenCV topics
*   ✓ **84 Certificates** of Completion
*   ✓ **114+ 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_ 536+ tutorials** on PyImageSearch
*   ✓ **Easy one-click downloads** for code, datasets, pre-trained models, etc.
*   ✓ **Access** on mobile, laptop, desktop, etc.

[Click here to join PyImageSearch University](https://pyimagesearch.com/pyimagesearch-university/?utm_source=blogPost&utm_medium=bottomBanner&utm_campaign=What%27s%20next%3F%20I%20recommend)

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](https://pyimagesearch.com/contact/). 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](https://pyimagesearch.com/pyimagesearch-gurus/). 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.

![Image 4](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20410%20567'%3E%3C/svg%3E)

#### 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.