Title: Installing OpenCV on your Raspberry Pi Zero - PyImageSearch

URL Source: https://pyimagesearch.com/2015/12/14/installing-opencv-on-your-raspberry-pi-zero/

Published Time: 2015-12-14T15:00:23+00:00

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

In this blog post I’ll demonstrate how to install OpenCV 3 on the [Raspberry Pi Zero](https://www.raspberrypi.org/blog/raspberry-pi-zero/).

Since I’ve covered how to install OpenCV on the Raspberry Pi in **_[multiple, previous blog posts](https://pyimagesearch.com/opencv-tutorials-resources-guides/)_**, I’ll keep this post on the shorter side and detail only the relevant commands necessary to get OpenCV up and running. For a more thorough discussion on how to install OpenCV 3 on your Pi (along with a _22-minute video installation guide_), please refer to [this post](https://pyimagesearch.com/2015/10/26/how-to-install-opencv-3-on-raspbian-jessie/).

I’ll also be making the following assumptions in this installation guide:

*   You are using _**Raspberry Pi Zero hardware**_ (so the timings supplied with each command will match up).
*   You have _**Raspbian Jessie**_ installed on your Pi Zero.
*   You want to install _**OpenCV v3.0**_ with _**Python 2.7 bindings**_ (for Python 3 support, [see this post](https://pyimagesearch.com/2015/10/26/how-to-install-opencv-3-on-raspbian-jessie/)).

Again, I have already covered installing OpenCV on [multiple Raspberry Pi platforms and Raspbian flavors](https://pyimagesearch.com/opencv-tutorials-resources-guides/) — the primary goal of this tutorial is to get OpenCV up and running on your Pi Zero so you can get started learning about computer vision, image processing, and the OpenCV library.

If you haven’t seen the [Raspberry Pi Zero](https://www.raspberrypi.org/blog/raspberry-pi-zero/) yet, it’s a really cool piece of hardware. It packs a single core 1GHz ARM processor. 512mb of RAM. _And it’s smaller than a credit card._

But the best part?

_**It’s only $5!**_

While the Pi Zero isn’t quite fast enough for advanced video processing, it’s still a great tool that you can use to learn the basics of computer vision and OpenCV.

### Step #1: Expand filesystem

If you’re using a _brand new_ install of Raspbian Jessie, then the first thing you should do is ensure your filesystem has been expanded to include all available space on your micro-SD card:

$ sudo raspi-config

Select the first option _“1. Expand Filesystem”_, arrow down to _“Finish”_, and reboot your Pi:

[![Image 2: Figure 1: Expanding the filesystem on your Raspberry Pi Zero.](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20600%20409'%3E%3C/svg%3E)](https://pyimagesearch.com/wp-content/uploads/2015/12/raspberry_pi_zero_expand_filesystem.jpg)

**Figure 1:** Expanding the filesystem on your Raspberry Pi Zero.

After rebooting, your filesystem will have be expanded to include all available space on your micro-SD card.

### Step #2: Install dependencies

I’ve discussed each of these dependencies are in [previous posts](https://pyimagesearch.com/2015/10/26/how-to-install-opencv-3-on-raspbian-jessie/), so I’ll just provide a brief description, the command(s) themselves, along with the **_amount of time it takes to execute each command_** so you can plan your OpenCV install accordingly (the compilation of OpenCV alone takes _9+ hours_).

First, we need to update and upgrade our existing packages:

$ sudo apt-get update
$ sudo apt-get upgrade

**Timing: 2m 29s**

Install our developer tools:

$ sudo apt-get install build-essential cmake pkg-config

**Timing: 49s**

Let’s grab the image I/O packages and install them:

$ sudo apt-get install libjpeg-dev libtiff5-dev libjasper-dev libpng12-dev

**Timing: 36s**

Along with some video I/O packages (although it’s unlikely that you’ll be doing a lot of video processing with the Raspberry Pi Zero):

$ sudo apt-get install libavcodec-dev libavformat-dev libswscale-dev libv4l-dev
$ sudo apt-get install libxvidcore-dev libx264-dev

**Timing: 36s**

We’ll need to install the GTK development library for OpenCV’s GUI interface:

$ sudo apt-get install libgtk2.0-dev

**Timing: 2m 57s**

Let’s also pull down a couple routine optimization packages leveraged by OpenCV:

$ sudo apt-get install libatlas-base-dev gfortran

**Timing: 52s**

Lastly, let’s install the Python 2.7 headers so wen can compile our OpenCV + Python bindings:

$ sudo apt-get install python2.7-dev

**Timings: 55s**

_**Note:** I’ll only be covering how to install_ OpenCV 3 _with_ Python 2.7 bindings _in this post. If you would like to install OpenCV 3 with Python 3 bindings, [please refer to this post](https://pyimagesearch.com/2015/10/26/how-to-install-opencv-3-on-raspbian-jessie/)._

### Step #3: Grab the OpenCV source

At this point, all of our dependences have been installed, so let’s grab the `3.0.0` release of [OpenCV from GitHub](https://github.com/Itseez/opencv) and pull it down:

$ cd ~
$ wget -O opencv.zip https://github.com/Itseez/opencv/archive/3.0.0.zip
$ unzip opencv.zip

**Timing: 1m 58s**

Let’s also grab the [opencv\_contrib](https://github.com/itseez/opencv_contrib) repository as well:

$ wget -O opencv\_contrib.zip https://github.com/Itseez/opencv\_contrib/archive/3.0.0.zip
$ unzip opencv\_contrib.zip

**Timing: 1m 5s**

It’s especially important to grab the `opencv_contrib` repo if you want access to [SIFT and SURF](https://pyimagesearch.com/2015/07/16/where-did-sift-and-surf-go-in-opencv-3/), both of which have been removed from the default install of OpenCV.

Now that `opencv.zip` and `opencv_contrib.zip` have been expanded, let’s delete them to save space:

$ rm opencv.zip opencv\_contrib.zip

### Step #4: Setup Python

The first step in setting up Python for the OpenCV build is to install `pip` , a Python package manager:

$ wget https://bootstrap.pypa.io/get-pip.py
$ sudo python get-pip.py

**Timing: 49s**

Let’s also install `virtualenv` and `virtualenvwarpper` , allowing us to create separate, isolated Python environments for each of our future projects:

$ sudo pip install virtualenv virtualenvwrapper
$ sudo rm -rf ~/.cache/pip

**Timing: 30s**

_**Note:** I’ve discussed both `virtualenv` and `virtualenvwrapper` many times on the PyImageSearch blog. If this is your first time using them, I suggest referring to [this blog post on installing OpenCV 3 on Raspbian Jessie](https://pyimagesearch.com/2015/10/26/how-to-install-opencv-3-on-raspbian-jessie/)._

To complete the install of `virtualenv` and `virtualenvwrapper` , open up your `~./profile` :

$ nano ~/.profile

And append the following lines to the bottom of the file:

\# virtualenv and virtualenvwrapper
export WORKON\_HOME=$HOME/.virtualenvs
source /usr/local/bin/virtualenvwrapper.sh

Now, `source` your `~/.profile` file to reload the changes:

$ source ~/.profile

Let’s create a new Python virtual environment appropriately named `cv` :

$ mkvirtualenv cv

**Timing: 31s**

The only requirement to build Python + OpenCV bindings is to have [NumPy](http://www.numpy.org/) installed, so let’s use `pip` to install NumPy for us:

$ pip install numpy

**Timing: 35m 4s**

### Step #5: Compile and install OpenCV for the Raspberry Pi Zero

We are now ready to compile and install OpenCV. Make sure you are in the `cv` virtual environment by using the `workon` command:

$ workon cv

And then setup the build using CMake:

$ cd ~/opencv-3.0.0/
$ mkdir build
$ cd build
$ cmake -D CMAKE\_BUILD\_TYPE=RELEASE \\
    -D CMAKE\_INSTALL\_PREFIX=/usr/local \\
    -D INSTALL\_C\_EXAMPLES=ON \\
    -D INSTALL\_PYTHON\_EXAMPLES=ON \\
    -D OPENCV\_EXTRA\_MODULES\_PATH=~/opencv\_contrib-3.0.0/modules \\
    -D BUILD\_EXAMPLES=ON ..

**Timing: 4m 29s**

Now that the build is all setup, run `make` to start the compilation process (this is going to take awhile, so you might want to let this run overnight):

$ make

**Timing: 9h 42m**

Assuming OpenCV compiled without error, you can install it on your Raspberry Pi Zero using:

$ sudo make install
$ sudo ldconfig

**Timing: 2m 31s**

### Step #6: Finishing the install

Provided you completed **Step #5** without an error, your OpenCV bindings should now be installed in `/usr/local/lib/python2.7/site-packages` :

$ ls -l /usr/local/lib/python2.7/site-packages
total 1640
-rw-r--r-- 1 root staff 1677024 Dec  2 08:34 cv2.so

All we need to do now is [sym-link](https://en.wikipedia.org/wiki/Symbolic_link) the `cv2.so` file (which are our actual Python + OpenCV bindings) into the `site-packages` directory of the `cv` virtual environment:

$ cd ~/.virtualenvs/cv/lib/python2.7/site-packages/
$ ln -s /usr/local/lib/python2.7/site-packages/cv2.so cv2.so

### Step #7: Verifying your OpenCV install

All that’s left to do now is verify that OpenCV has been correctly installed on your Raspberry Pi Zero.

Whenever you want to use OpenCV, first make sure you are in the `cv` virtual environment:

$ workon cv

And from there you can fire up a Python shell and import the OpenCV bindings:

$ workon cv
$ python
>>> import cv2
>>> cv2.\_\_version\_\_
'3.0.0'
>>>

Or you can execute a Python script that imports OpenCV.

Once OpenCV has been installed, you can remove both the `opencv-3.0.0` and `opencv_contrib-3.0.0` directories, freeing up a bunch of space on your filesystem:

$ rm -rf opencv-3.0.0 opencv\_contrib-3.0.0

But be cautious before you run this command! Make sure OpenCV has been properly installed on your system before blowing away these directories, otherwise you will have to start the (long, 9+ hour) compile _all over again!_

Troubleshooting
---------------

If you ran into any error installing OpenCV 3 on your Raspberry Pi Zero, I would suggest consulting the _**Troubleshooting**_ section of _**[this post](https://pyimagesearch.com/2015/10/26/how-to-install-opencv-3-on-raspbian-jessie/)**_ which goes into added detail for each installation step.

The post also includes a complete **22 minute video** where _I demonstrate how to run each_ command to flawlessly get OpenCV 3 installed on your Raspberry Pi:

[![Image 3: Figure 2: Getting OpenCV up and running on your Raspberry Pi.](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20512%20386'%3E%3C/svg%3E)](https://pyimagesearch.com/2015/10/26/how-to-install-opencv-3-on-raspbian-jessie/)

**Figure 2:** Getting OpenCV up and running on your Raspberry Pi.

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

This post detailed how to install OpenCV 3 on your Raspberry Pi Zero. The purpose of this blog post was to provide accurate timings that you can use when planning your own install of OpenCV on your Pi Zero.

In order to get OpenCV up and running, I made the following assumptions:

*   You are running _**Raspbian Jessie**_ on your Raspberry Pi Zero.
*   You are installing _**OpenCV v3**_.
*   You want to use _**Python 2.7**_ with your OpenCV bindings.

If you would like to use _**Python 3+**_ with your OpenCV bindings, [please consult this post](https://pyimagesearch.com/2015/10/26/how-to-install-opencv-3-on-raspbian-jessie/), where I have elaborated more on each step, provided more detailed information, and included a 22 minute video that walks you through step-by-step on installing OpenCV 3 on your Raspberry Pi.

![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.