Chapter 8. Computer Vision

8.0 Introduction

Computer vision (CV) allows your Raspberry Pi to “see things.” In practical terms, this means that your Raspberry Pi can analyze an image, looking for items of interest, and can even recognize faces and text.

If you link this with a camera to supply the images, all sorts of possibilities open up.

8.1 Installing OpenCV

Problem

You want to install OpenCV 3 computer vision software on your Raspberry Pi.

Solution

To install OpenCV, first install the prerequisite packages using these commands:

$ 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

You may also need to update pip using:

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

Then install OpenCV itself and Python image utilities using these commands:

$ sudo pip install opencv-contrib-python==4.1.0.25
$ pip install imutils

After installation is complete, you can check that everything is working by starting Python 3, importing cv2 and checking the version:

pi@raspberrypi:~ $ python3
Python 3.7.3 (default, Apr  3 2019, 05:39:12) 
[GCC 8.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2
>>> cv2.__version__
'4.1.0'
>>> exit()

Discussion

Computer vision is both processor and memory intensive, so although OpenCV will work on an older Raspberry Pi, they can be slow on anything earlier than a Raspberry ...

Get Raspberry Pi Cookbook, 3rd Edition now with the O’Reilly learning platform.

O’Reilly members experience live online training, plus books, videos, and digital content from nearly 200 publishers.