Title: Raspbian Stretch: Install OpenCV 3 + Python on your Raspberry Pi - PyImageSearch

URL Source: https://pyimagesearch.com/2017/09/04/raspbian-stretch-install-opencv-3-python-on-your-raspberry-pi/

Published Time: 2017-09-04T14:00:40+00:00

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

It’s been over two years since the release of Raspbian Jessie. As of August 17th, 2017, the Raspberry Pi foundation has officially released the successor to Raspbian Jessie — _**Raspbian Stretch.**_

Just as I have done in previous blog posts, I’ll be demonstrating **how to install OpenCV 3 with Python bindings on Raspbian _Stretch_**_._

If you are looking for previous installation instructions for different platforms, please consult this list:

*   [Install guide: Raspberry Pi 3 + **Raspbian Jessie** + OpenCV 3](https://pyimagesearch.com/2016/04/18/install-guide-raspberry-pi-3-raspbian-jessie-opencv-3/)
*   [How to install OpenCV 3.0 on _**Raspbian Jessie**._](https://pyimagesearch.com/2015/10/26/how-to-install-opencv-3-on-raspbian-jessie/)
*   [Installing OpenCV on your _Raspberry Pi Zero_ running _**Raspbian Jessie**._](https://pyimagesearch.com/2015/12/14/installing-opencv-on-your-raspberry-pi-zero/)
*   [Installing OpenCV 3.0 for both Python 2.7 and Python 3+ on _**Raspbian Wheezy**_**.**](https://pyimagesearch.com/2015/07/27/installing-opencv-3-0-for-both-python-2-7-and-python-3-on-your-raspberry-pi-2/)
*   [Install OpenCV 2.4 for Python 2.7 on _**Raspbian Wheezy**_.](https://pyimagesearch.com/2015/02/23/install-opencv-and-python-on-your-raspberry-pi-2-and-b/)

Otherwise, let’s proceed with getting OpenCV 3 with Python bindings installed on Raspian Stretch!

The quick start video tutorial
------------------------------

If this is your first time installing OpenCV or you are just getting started with Linux I _highly suggest_ that you watch the video below and follow along with me as you guide you step-by-step on how to install OpenCV 3 on your Raspberry Pi running Raspbian Stretch:

Otherwise, if you feel comfortable using the command line or if you have previous experience with Linux environments, feel free to use the text-based version of this guide below.

Assumptions
-----------

In this tutorial, I am going to assume that you already own a **[Raspberry Pi 3](http://www.amazon.com/gp/product/B01C6FFNY4/ref=as_li_tl?ie=UTF8&camp=1789&creative=390957&creativeASIN=B01C6FFNY4&linkCode=as2&tag=trndingcom-20&linkId=KQ4HNH4UOHXJG44M)** with **[Raspbian Stretch installed](https://www.raspberrypi.org/downloads/raspbian/)**.

If you don’t already have the Raspbian Stretch OS, you’ll need to upgrade your OS to take advantage of [Raspbian Stretch’s new features](https://www.raspberrypi.org/blog/raspbian-stretch/).

To upgrade your Raspberry Pi 3 to Raspbian Stretch, you may [download it here](https://www.raspberrypi.org/downloads/raspbian/) and follow these [upgrade instructions](https://www.raspberrypi.org/documentation/installation/installing-images/README.md) (or [these for the NOOBS route](https://www.raspberrypi.org/downloads/noobs/) which is recommended for beginners). The former instructions take approximately 10 minutes to download via a torrent client and about 10 minutes to flash the SD card at which point you can power up and proceed to the next section.

_**Note:** If you are upgrading your Raspberry Pi 3 from Raspbian Jessie to Raspbian Stretch, there is the potential for problems. **Proceed at your own risk**, and consult the [Raspberry Pi forums](https://www.raspberrypi.org/forums/viewforum.php?f=66&sid=4f6cdf440215ab801a2f3b41a6dfdf92) for help._

_**Important:** It is my recommendation that you proceed with a **fresh install of Raspbian Stretch!** Upgrading from Raspbian Jessie is **not** recommended.  
_

Assuming that your OS is up to date, you’ll need one of the following for the remainder of this post:

*   _Physical access_ to your Raspberry Pi 3 so that you can open up a terminal and execute commands
*   _Remote access_ via SSH or VNC.

I’ll be doing the majority of this tutorial via SSH, but as long as you have access to a terminal, you can easily follow along.

_**Can’t SSH?**_ If you see your Pi on your network, but can’t ssh to it, you may need to enable SSH. This can easily be done via the Raspberry Pi desktop preferences menu (you’ll need an HDMI cable and a keyboard/mouse) or running `sudo service ssh start` from the command line of your Pi.

After you’ve changed the setting and rebooted, you can test SSH directly on the Pi with the localhost address. Open a terminal and type `ssh pi@127.0.0.1` to see if it is working.

_**Keyboard layout giving you problems?**_ Change your keyboard layout by going to the Raspberry Pi desktop preferences menu. I use the standard US Keyboard layout, but you’ll want to select the one appropriate for your keyboard or desire (any Dvorkac users out there?).

Installing OpenCV 3 on a Raspberry Pi 3 running Raspbian Stretch
----------------------------------------------------------------

If you’ve ever installed OpenCV on a Raspberry Pi (or any other platform before), you know that the process can be quite time consuming with many dependencies and pre-requisites that have to be installed. **The goal of this tutorial is to thus guide you step-by-step through the compile and installation process.**

In order to make the installation process go more smoothly, I’ve included timings for each step so you know when to take a break, grab a cup of coffee, and checkup on email while the Pi compiles OpenCV.

Let’s go ahead and get started installing OpenCV 3 on your Raspberry Pi 3 running Raspbian Stretch.

### Step #1: Expand filesystem

Are you using a _brand new_ install of Raspbian Stretch?

If so, the first thing you should do is expand your filesystem to include _all available space_ on your micro-SD card:

$ sudo raspi-config

And then select the “Advanced Options” menu item:

[![Image 2](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20600%20402'%3E%3C/svg%3E)](https://pyimagesearch.com/wp-content/uploads/2017/08/advanced_options.png)

**Figure 1:** Select the “_Advanced Options_” item from the “_raspi-config_” menu.

Followed by selecting “Expand filesystem”:

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

**Figure 2:** Expanding the filesystem on your Raspberry Pi 3.

Once prompted, you should select the first option, _**“A1. Expand File System”**_, _**hit Enter**_ on your keyboard, arrow down to the _**“<Finish>”**_ button, and then reboot your Pi — you may be prompted to reboot, but if you aren’t you can execute:

$ sudo reboot

After rebooting, your file system should have been expanded to include all available space on your micro-SD card. You can verify that the disk has been expanded by executing `df -h` and examining the output:

$ df -h
Filesystem      Size  Used Avail Use% Mounted on
/dev/root        30G  4.2G   24G  15% /
devtmpfs        434M     0  434M   0% /dev
tmpfs           438M     0  438M   0% /dev/shm
tmpfs           438M   12M  427M   3% /run
tmpfs           5.0M  4.0K  5.0M   1% /run/lock
tmpfs           438M     0  438M   0% /sys/fs/cgroup
/dev/mmcblk0p1   42M   21M   21M  51% /boot
tmpfs            88M     0   88M   0% /run/user/1000

As you can see, my Raspbian filesystem has been expanded to include all 32GB of the micro-SD card.

However, even with my filesystem expanded, I have already used 15% of my 32GB card.

If you are using an 8GB card you may be using close to 50% of the available space, so one simple thing to do is to delete both LibreOffice and Wolfram engine to free up some space on your Pi:

$ sudo apt-get purge wolfram-engine
$ sudo apt-get purge libreoffice\*
$ sudo apt-get clean
$ sudo apt-get autoremove

**After removing the Wolfram Engine and LibreOffice, _you can reclaim almost 1GB!_**

### Step #2: Install dependencies

[This isn’t the first time I’ve discussed how to install OpenCV on the Raspberry Pi](https://pyimagesearch.com/opencv-tutorials-resources-guides/), so I’ll keep these instructions on the brief side, allowing you to work through the installation process: I’ve also included the _**amount of time it takes to execute each command**_ (some depend on your Internet speed) so you can plan your OpenCV + Raspberry Pi 3 install accordingly (OpenCV itself takes approximately **4 hours to compile** — more on this later).

The first step is to update and upgrade any existing packages:

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

**Timing: 2m 14s  
**

We then need to install some developer tools, including [CMake](https://cmake.org/), which helps us configure the OpenCV build process:

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

**Timing: 19s**

Next, we need to install some image I/O packages that allow us to load various image file formats from disk. Examples of such file formats include JPEG, PNG, TIFF, etc.:

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

**Timing: 21s**

Just as we need image I/O packages, we also need video I/O packages. These libraries allow us to read various video file formats from disk as well as work directly with video streams:

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

**Timing: 32s**

The OpenCV library comes with a sub-module named `highgui` which is used to display images to our screen and build basic GUIs. In order to compile the `highgui` module, we need to install the GTK development library:

$ sudo apt-get install libgtk2.0-dev libgtk-3-dev

**Timing: 1m 36s**

Many operations inside of OpenCV (namely matrix operations) can be optimized further by installing a few extra dependencies:

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

**Timing: 23s**

These optimization libraries are _especially important_ for resource constrained devices such as the Raspberry Pi.

Lastly, let’s install both the Python 2.7 and Python 3 header files so we can compile OpenCV with Python bindings:

$ sudo apt-get install python2.7-dev python3-dev

**Timing: 45s**

If you’re working with a fresh install of the OS, it is possible that these versions of Python are already at the newest version (you’ll see a terminal message stating this).

If you skip this step, you may notice an error related to the `Python.h` header file not being found when running `make` to compile OpenCV.

### Step #3: Download the OpenCV source code

Now that we have our dependencies installed, let’s grab the `3.3.0` archive of OpenCV from the official [OpenCV repository](https://github.com/Itseez/opencv). This version includes the `dnn` module which we discussed in a previous post where we did [Deep Learning with OpenCV](https://pyimagesearch.com/2017/08/21/deep-learning-with-opencv/) (**_Note:_** As future versions of openCV are released, you can replace `3.3.0` with the latest version number):

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

**Timing: 41s**

We’ll want the _full install_ of OpenCV 3 ([to have access to features such as SIFT and SURF](https://pyimagesearch.com/2015/07/16/where-did-sift-and-surf-go-in-opencv-3/), for instance), so we also need to 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.3.0.zip
$ unzip opencv\_contrib.zip

**Timing: 37s**

You might need to expand the command above using the “<=>” button during your copy and paste. The `.zip` in the `3.3.0.zip` may appear to be cutoff in some browsers. The full URL of the OpenCV 3.3.0 archive is:

[https://github.com/Itseez/opencv\_contrib/archive/3.3.0.zip](https://github.com/Itseez/opencv_contrib/archive/3.3.0.zip)

_**Note:** Make sure your `opencv` and `opencv_contrib` versions are the same (in this case, `3.3.0`). If the versions numbers do not match up, then you’ll likely run into either compile-time or runtime errors._

### Step #4: Python 2.7 or Python 3?

Before we can start compiling OpenCV on our Raspberry Pi 3, we first need to install `pip` , a Python package manager:

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

**Timing: 33s**

You may get a message that pip is already up to date when issuing these commands, but it is best not to skip this step.

If you’re a longtime PyImageSearch reader, then you’ll know that I’m a _huge fan_ of both [virtualenv](https://virtualenv.pypa.io/en/latest/) and [virtualenvwrapper](https://virtualenvwrapper.readthedocs.org/en/latest/). Installing these packages is not a requirement and you can _absolutely_ get OpenCV installed without them, but that said, _**I highly recommend you install them**_ as other existing PyImageSearch tutorials (as well as future tutorials) also leverage Python virtual environments. I’ll also be assuming that you have both `virtualenv` and `virtualenvwrapper` installed throughout the remainder of this guide.

**So, given that, what’s the point of using `virtualenv` and `virtualenvwrapper` ?**

First, it’s important to understand that a virtual environment is a _special tool_ used to keep the dependencies required by different projects in separate places by creating _isolated, independent_ Python environments for each of them.

In short, it solves the _“Project X depends on version 1.x, but Project Y needs 4.x”_ dilemma. It also keeps your global `site-packages` neat, tidy, and free from clutter.

If you would like a full explanation on why Python virtual environments are good practice, absolutely [give this excellent blog post on RealPython a read](https://realpython.com/blog/python/python-virtual-environments-a-primer/).

It’s _**standard practice**_ in the Python community to be using virtual environments of some sort, so I _highly recommend_ that you do the same:

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

**Timing: 35s**

Now that both `virtualenv` and `virtualenvwrapper` have been installed, we need to update our `~/.profile` file to include the following lines at the _bottom_ of the file:

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

In previous tutorials, I’ve recommended using your favorite terminal-based text editor such as `vim` , `emacs` , or `nano` to update the `~/.profile` file. If you’re comfortable with these editors, go ahead and update the file to reflect the changes mentioned above.

Otherwise, you should simply use `cat` and output redirection to handle updating `~/.profile` :

$ echo -e "\\n# virtualenv and virtualenvwrapper" >> ~/.profile
$ echo "export WORKON\_HOME=$HOME/.virtualenvs" >> ~/.profile
$ echo "export VIRTUALENVWRAPPER\_PYTHON=/usr/bin/python3" >> ~/.profile
$ echo "source /usr/local/bin/virtualenvwrapper.sh" >> ~/.profile

Now that we have our `~/.profile` updated, we need to reload it to make sure the changes take affect. You can force a reload of your `~/.profile` file by:

1.  Logging out and then logging back in.
2.  Closing a terminal instance and opening up a new one
3.  Or my personal favorite, _**just use the `source` command:**_

$ source ~/.profile

_**Note:** I recommend running the `source ~/.profile` file_ **each time** _you open up a new terminal to ensure your system variables have been setup correctly._

#### Creating your Python virtual environment

Next, let’s create the Python virtual environment that we’ll use for computer vision development:

$ mkvirtualenv cv -p python2

This command will create a new Python virtual environment named `cv` using _**Python 2.7**_.

If you instead want to use _**Python 3**_, you’ll want to use this command instead:

$ mkvirtualenv cv -p python3

**Timing: 24s**

Again, _**I can’t stress this point enough:**_ the `cv` Python virtual environment is _**entirely independent and sequestered**_ from the default Python version included in the download of Raspbian Stretch. Any Python packages in the _global_ `site-packages` directory _will not_ be available to the `cv` virtual environment. Similarly, any Python packages installed in `site-packages` of `cv` _will not_ be available to the global install of Python. Keep this in mind when you’re working in your Python virtual environment and it will help avoid a lot of confusion and headaches.

#### How to check if you’re in the “cv” virtual environment

If you ever reboot your Raspberry Pi; log out and log back in; or open up a new terminal, you’ll need to use the `workon` command to re-access the `cv` virtual environment. In previous blog posts, I’ve seen readers use the `mkvirtualenv` command — _**this is entirely unneeded!**_ The `mkvirtualenv` command is meant to be executed only once: to actually _create_ the virtual environment.

After that, you can use `workon` and you’ll be dropped down into your virtual environment:

$ source ~/.profile
$ workon cv

To validate and ensure you are in the `cv` virtual environment, examine your command line — _if you see the text `(cv)` preceding your prompt, then you **are** in the `cv` virtual environment:_

[![Image 4](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20600%20402'%3E%3C/svg%3E)](https://pyimagesearch.com/wp-content/uploads/2017/08/cv_virtualenv.png)

**Figure 3:** Make sure you see the “_(cv)_” text on your prompt, indicating that you _**are**_ in the cv virtual environment.

Otherwise, if you _**do not**_ see the `(cv)` text, then you _**are not**_ in the `cv` virtual environment:

[![Image 5](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20600%20402'%3E%3C/svg%3E)](https://pyimagesearch.com/wp-content/uploads/2017/08/no_virtualenv.png)

**Figure 4:** If you do not see the “_(cv)_” text on your prompt, then you _**are** **not**_ in the cv virtual environment and need to run “_source_” and “_workon_” to resolve this issue.

To fix this, simply execute the `source` and `workon` commands mentioned above.

### Installing NumPy on your Raspberry Pi

Assuming you’ve made it this far, you should now be in the `cv` virtual environment (which you should stay in for the rest of this tutorial). Our only Python dependency is [NumPy](http://www.numpy.org/), a Python package used for numerical processing:

$ pip install numpy

**Timing: 11m 12s**

Be sure to grab a cup of coffee or go for a nice walk, the NumPy installation can take a bit of time.

_**Note:** A question I’ve often seen is **“Help, my NumPy installation has hung and it’s not installing!”** Actually, it is installing, it just takes time to pull down the sources and compile. You can verify that NumPy is compiling and installing by running `top` . Here you’ll see that your CPU cycles are being used compiling NumPy. Be patient. The Raspberry Pi isn’t as fast as your laptop/desktop._

### Step #5: Compile and Install OpenCV

We are now ready to compile and install OpenCV! Double-check that you are in the `cv` virtual environment by examining your prompt (you should see the `(cv)` text preceding it), and if not, simply execute `workon` :

$ workon cv

Once you have ensured you are in the `cv` virtual environment, we can setup our build using CMake:

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

**Timing: 2m 56s**

Now, before we move on to the actual compilation step, _**make sure you examine the output of CMake!**_

Start by scrolling down the section titled `Python 2` and `Python 3` .

_**If you are compiling OpenCV 3 for Python 2.7**_, then make sure your `Python 2` section includes valid paths to the `Interpreter` , `Libraries` , `numpy` and `packages path` , similar to my screenshot below:

[![Image 6](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20600%20369'%3E%3C/svg%3E)](https://pyimagesearch.com/wp-content/uploads/2017/09/py27_cv.png)

**Figure 6:** Checking that Python 3 will be used when compiling OpenCV 3 for Raspbian Stretch on the Raspberry Pi 3.

Notice how the `Interpreter` points to our `python2.7` binary located in the `cv` virtual environment. The `numpy` variable also points to the NumPy installation in the `cv` environment.

Similarly, _**if you’re compiling OpenCV for Python 3**_, make sure the `Python 3` section looks like the figure below:

[![Image 7](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20600%20399'%3E%3C/svg%3E)](https://pyimagesearch.com/wp-content/uploads/2017/08/python3_opencv.png)

**Figure 6:** Checking that Python 3 will be used when compiling OpenCV 3 for Raspbian Stretch on the Raspberry Pi 3.

Again, the `Interpreter` points to our `python3.5` binary located in the `cv` virtual environment while `numpy` points to our NumPy install.

In either case, if you **_do not_** see the `cv` virtual environment in these variables paths, _**it’s almost certainly because you are NOT in the `cv` virtual environment prior to running CMake!**_

If this is the case, access the `cv` virtual environment using `workon cv` and re-run the `cmake` command outlined above.

#### Configure your swap space size before compiling

Before you start the compile process, you should **increase your swap space size**. This enables OpenCV to **compile with all four cores** of the Raspberry PI without the compile hanging due to memory problems.

Open your `/etc/dphys-swapfile` and then edit the `CONF_SWAPSIZE` variable:

\# set size to absolute value, leaving empty (default) then uses computed value
#   you most likely don't want this, unless you have an special disk situation
# CONF\_SWAPSIZE=100
CONF\_SWAPSIZE=1024

Notice that I’ve commented out the 100MB line and added a 1024MB line. This is the secret to getting compiling with multiple cores on the Raspbian Stretch.

_**If you skip this step, OpenCV might not compile.**_

To activate the new swap space, restart the swap service:

$ sudo /etc/init.d/dphys-swapfile stop
$ sudo /etc/init.d/dphys-swapfile start

_**Note:** It is possible to burn out the Raspberry Pi microSD card because flash memory has a limited number of writes until the card won’t work. It is **highly recommended** that you change this setting back to the default when you are done compiling and testing the install (see below). To read more about swap sizes corrupting memory, see [this page.](https://www.bitpi.co/2015/02/11/how-to-change-raspberry-pis-swapfile-size-on-rasbian/)_

Finally, we are now ready to compile OpenCV:

$ make -j4

**Timing: 1h 30m**

Once OpenCV 3 has finished compiling, your output should look similar to mine below:

[![Image 8](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20796%20492'%3E%3C/svg%3E)](https://pyimagesearch.com/wp-content/uploads/2017/09/cv_make.png)

**Figure 7:** Our OpenCV 3 compile on Raspbian Stretch has completed successfully.

From there, all you need to do is install OpenCV 3 on your Raspberry Pi 3:

$ sudo make install
$ sudo ldconfig

**Timing: 52s**

### Step #6: Finish installing OpenCV on your Pi

We’re almost done — just a few more steps to go and you’ll be ready to use your Raspberry Pi 3 with OpenCV 3 on Raspbian Stretch.

#### For Python 2.7:

Provided your **Step #5** finished without error, OpenCV should now be installed in `/usr/local/lib/python2.7/site-pacakges` . 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 1895772 Mar 20 20:00 cv2.so

_**Note:** In some cases, OpenCV can be installed in `/usr/local/lib/python2.7/dist-packages` (note the `dist-packages` rather than `site-packages` ). If you do not find the `cv2.so` bindings in `site-packages` , we be sure to check `dist-packages` ._

Our final step is to [sym-link](https://en.wikipedia.org/wiki/Symbolic_link) the OpenCV bindings into our `cv` virtual environment for Python 2.7:

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

#### For Python 3:

After running `make install` , your OpenCV + Python bindings should be installed in `/usr/local/lib/python3.5/site-packages` . Again, you can verify this with the `ls` command:

$ ls -l /usr/local/lib/python3.5/site-packages/
total 1852
-rw-r--r-- 1 root staff 1895932 Mar 20 21:51 cv2.cpython-34m.so

I honestly don’t know why, perhaps it’s a bug in the CMake script, but when compiling OpenCV 3 bindings for Python 3+, the output `.so` file is named `cv2.cpython-35m-arm-linux-gnueabihf.so` (or some variant of) rather than simply `cv2.so` (like in the Python 2.7 bindings).

Again, I’m not sure exactly _why_ this happens, but it’s an easy fix. All we need to do is rename the file:

$ cd /usr/local/lib/python3.5/site-packages/
$ sudo mv cv2.cpython-35m-arm-linux-gnueabihf.so cv2.so

After renaming to `cv2.so` , we can sym-link our OpenCV bindings into the `cv` virtual environment for Python 3.5:

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

### Step #7: Testing your OpenCV 3 install

**Congratulations, you now have OpenCV 3 installed on your Raspberry Pi 3 running Raspbian Stretch!**

But before we pop the champagne and get drunk on our victory, let’s first verify that your OpenCV installation is working properly.

Open up a new terminal, execute the `source` and `workon` commands, and then finally attempt to import the Python + OpenCV bindings:

$ source ~/.profile 
$ workon cv
$ python
>>> import cv2
>>> cv2.\_\_version\_\_
'3.3.0'
>>>

As you can see from the screenshot of my own terminal, **OpenCV 3 has been successfully installed on my Raspberry Pi 3 + Python 3.5 environment:**

[![Image 9](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20600%20402'%3E%3C/svg%3E)](https://pyimagesearch.com/wp-content/uploads/2017/08/cv2__version__.png)

**Figure 8:** Confirming OpenCV 3 has been successfully installed on my Raspberry Pi 3 running Raspbian Stretch.

Once OpenCV has been installed, you can remove both the `opencv-3.3.0` and `opencv_contrib-3.3.0` directories to free up a bunch of space on your disk:

$ rm -rf opencv-3.3.0 opencv\_contrib-3.3.0

However, be cautious with this command! Make sure OpenCV has been properly installed on your system before blowing away these directories. A mistake here could cost you _**hours**_ in compile time.

#### Don’t forget to change your swap size back!

Open your `/etc/dphys-swapfile` and then edit the `CONF_SWAPSIZE` variable:

\# set size to absolute value, leaving empty (default) then uses computed value
#   you most likely don't want this, unless you have an special disk situation
CONF\_SWAPSIZE=100
# CONF\_SWAPSIZE=1024

Notice that I’ve commented out the 1024MB line and uncommented the 100MB line.

_**If you skip this step, your memory card won’t last as long.**_ As stated above, larger swap spaces may lead to memory corruption, so I recommend setting it back to 100MB.

To revert to the smaller swap space, restart the swap service:

$ sudo /etc/init.d/dphys-swapfile stop
$ sudo /etc/init.d/dphys-swapfile start

Troubleshooting and FAQ
-----------------------

_**Q.**_ When I try to execute `mkvirtualenv` and `workon` , I get a “command not found error”.

_**A.**_ There are three reasons why this could be happening, all of them related to **Step #4**:

1.  Make certain that you have installed `virtualenv` and `virtualenvwrapper` via `pip` . You can check this by running `pip freeze` and then examining the output, ensuring you see occurrences of both `virtualenv` and `virtualenvwrapper` .
2.  You might not have updated your `~/.profile` correctly. Use a text editor such as `nano` to view your `~/.profile` file and ensure that the proper `export` and `source` commands are present (again, check **Step #4** for the contents that should be appended to `~/.profile` .
3.  You did not `source` your `~/.profile` after editing it, rebooting, opening a new terminal, etc. Any time you open a new terminal and want to use a virtual environment, make sure you execute `source ~/.profile` to load the contents — this will give you access to the `mkvirtualenv` and `workon` commands.

_**Q.**_ After I open a new terminal, logout, or reboot my Pi, I cannot execute `mkvirtualenv` or `workon` .

_**A.**_ See **reason #3** from the previous question.

_**Q.**_ When I (1) open up a Python shell that imports OpenCV or (2) execute a Python script that calls OpenCV, I get an error: `ImportError: No module named cv2` .

_**A.**_ Unfortunately, this error is extremely hard to diagnose, mainly because there are multiple issues that could be causing the problem. To start, make sure you are in the `cv` virtual environment by using `workon cv` . If the `workon` command fails, then see the first question in this FAQ. If you’re _still_ getting an error, investigate the contents of the `site-packages` directory for your `cv` virtual environment. You can find the `site-packages` directory in `~/.virtualenvs/cv/lib/python2.7/site-packages/` or `~/.virtualenvs/cv/lib/python3.5/site-packages/` (depending on which Python version you used for the install). Make sure that your sym-link to the `cv2.so` file is valid and points to an existing file.

_**Q.**_ I’m running into other errors.

_**A.**_ Feel free to leave a comment and I’ll try to provide guidance; however, please understand that without physical access to your Pi it can often be hard to diagnose compile/install errors. If you’re in a rush to get OpenCV up and running on your Raspberry Pi be sure to take a look at the _Quickstart Bundle_ and _Hardcopy Bundle_ of my book, [_Practical Python and OpenCV_](https://pyimagesearch.com/practical-python-opencv/). Both of these bundles include a Raspbian .img file with OpenCV pre-configured and pre-installed. Simply download the .img file, flash it to your Raspberry Pi, and boot! This method is by far the _easiest, hassle free_ method to getting started with OpenCV on your Raspberry Pi.

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](https://pyimagesearch.com/2015/03/30/accessing-the-raspberry-pi-camera-with-opencv-and-python/) 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_](https://pyimagesearch.com/practical-python-opencv/?src=pi-opencv-install). 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._**

[![Image 10: raspberry_pi_in_post](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20375%20250'%3E%3C/svg%3E)](https://pyimagesearch.com/practical-python-opencv/?src=pi-opencv-install)

**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_**](https://pyimagesearch.com/practical-python-opencv/?src=pi-opencv-install) _**to learn more about the real-world projects you can solve using your Raspberry Pi + Practical Python and OpenCV .**_

### 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, we learned how to upgrade your _**Raspberry Pi 3**_‘s OS to _**Raspbian Stretch**_ and to install _**OpenCV 3**_ with either Python 2.7 or Python 3 bindings.

If you are running a different version of Raspbian (such as _Raspbian Wheezy_) or want to install a different version of OpenCV (such as OpenCV 2.4), please consult the following tutorials:

*   [Install guide: Raspberry Pi 3 + **Raspbian Jessie** + OpenCV 3](https://pyimagesearch.com/2016/04/18/install-guide-raspberry-pi-3-raspbian-jessie-opencv-3/)
*   [How to install OpenCV 3.0 on _**Raspbian Jessie**._](https://pyimagesearch.com/2015/10/26/how-to-install-opencv-3-on-raspbian-jessie/)
*   [Installing OpenCV on your _Raspberry Pi Zero_ running _**Raspbian Jessie**._](https://pyimagesearch.com/2015/12/14/installing-opencv-on-your-raspberry-pi-zero/)
*   [Installing OpenCV 3.0 for both Python 2.7 and Python 3+ on _**Raspbian Wheezy**_**.**](https://pyimagesearch.com/2015/07/27/installing-opencv-3-0-for-both-python-2-7-and-python-3-on-your-raspberry-pi-2/)
*   [Install OpenCV 2.4 for Python 2.7 on _**Raspbian Wheezy**_.](https://pyimagesearch.com/2015/02/23/install-opencv-and-python-on-your-raspberry-pi-2-and-b/)

**Are you looking for a project to work on with your new install of OpenCV on Raspbian Stretch?** Readers have been big fans of this post on [Home surveillance and motion detection with the Raspberry Pi, Python, OpenCV, and Dropbox](https://pyimagesearch.com/2015/06/01/home-surveillance-and-motion-detection-with-the-raspberry-pi-python-and-opencv/).

_But before you go…_
--------------------

I tend to utilize the Raspberry Pi quite a bit on this blog, so if you’re interested in learning more about the Raspberry Pi + computer vision, _**enter your email address in the form below to be notified when these posts go live!**_

![Image 11](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.