How to Install OpenCV for Python on a Raspberry Pi

Red Raspberry

Computer Vision isn’t just for GPU-powered desktops. Using OpenCV and Python on a Raspberry Pi equipped with a Camera Module, you can program and deploy amazing solutions including motion detection.

As I discovered personally, installing the correct libraries for OpenCV and Python for this task can be challenging. Here are the steps I used to get it working.

Raspberry photo by Mockup Graphics on Unsplash
IMPORTANT NOTE: the sudo commands provided below are now outdated.  You can quickly install OpenCV on your Raspberry Pi using the setup.sh script included with the latest release of CamKnows™, my open-source Computer Vision motion detection application for the Raspberry Pi.

Raspberry Pi Prerequisites

  • Raspberry Pi Zero, 3 or 4
  • Raspberry Pi OS running the Stretch distribution or later

Your Pi OS Info

You can verify your Pi OS information using the following command:

$ lsb_release -a

Example output:

No LSB modules are available.
Distributor ID: Raspbian
Description:    Raspbian GNU/Linux 10 (buster)
Release:        10
Codename:       buster

You’ll need this information for reference later.

Install OpenCV and Python Packages

Now execute the following commands to update your package manager and install the basics:

$ sudo apt-get update

$ sudo apt-get install libhdf5-dev libhdf5-serial-dev libhdf5-103

$ sudo apt-get install libqtgui4 libqtwebkit4 libqt4-test python3-pyqt5

With these prerequisites installed, browse to the opencv-contrib-python page on piwheels to determine the version compatible with your Pi OS version.

In my specific case, at the time of this writing, the latest version with a status of Build succeeded for Buster is 4.4.0.46.

Using this information, you’ll now execute the following command. Update the version for your specific setup.

$ sudo pip3 install opencv-contrib-python==4.4.0.46

With this method, opencv-contrib-python should install successfully, and quickly! Using the wrong version number, or excluding it from the install command, will result in a very slow install process that ultimately fails to complete. It took me several tries with the wrong version before realizing I needed to refer to the piwheels site. Once I had the right version specified, installation was quite fast, even on the ultra-compact Pi Zero.

Raspberry Pi Zero W Size Comparison
The Raspberry Pi Zero W vs. an AMD 486 DX2 66 MHz CPU. Bounty hunter not included. Photo by the author.

Now that you have this setup, you’ll want to install some additional packages:

$ sudo pip3 install imutils

$ sudo apt-get install libatlas-base-dev

And you’re all set!

Next Steps with Computer Vision and OpenCV

What can you do with OpenCV? Here is a code example that demonstrates using OpenCV and Python to calculate the difference between two images to infer if motion has occurred.

The above code is based on Recipe 8.6. Motion Detection from Simon Monk’s Raspberry Pi Cookbook, now in its 3rd Edition. I have found it to be an invaluable resource for leveraging the capabilities of these diminutive but powerful devices to solve real problems.

There are numerous examples where businesses and manufacturers have deployed the Raspberry Pi to achieve significant cost savings. It can be a key component in your move towards edge computing as part of a larger AI and Machine Learning strategy.

Hopefully the steps I have provided above will help you get started more quickly building your own OpenCV applications. I’m looking forward to posting more details on my recent project work here shortly. (Update 9/7/2021 – I’m using OpenCV as part of CamKnows™, a Computer Vision motion detection application for the Raspberry Pi which is now available on GitHub.)