It’s a big install, and it may take a while before the process is com-
plete.
Next, if you’re starting with a fresh installation of the Raspberry Pi
OS, you’ll need to install pip, Python’s package manager. To do that
using wget (a command-line tool for interacting with and download-
ing files from the internet), enter the following into your Terminal:
$ wget https://bootstrap.pypa.io/get-pip.py
$ sudo python3 get-pip.py
Finally, you’ll install the actual OpenCV library with the following
command:
$ sudo pip install opencv-contrib-python==4.1.0.25
When it’s done, check that the installation worked by going into the
Python interactive interpreter and importing the library:
$ python3
Python 3.7.3 (default, Jan 22 2021, 20:04:44)
[GCC8.3.0] on linux
Type "help", "copyright", "credits" or "license" for more
information.
>>> import cv2
>>> cv2.version
'4.1.0'
>>>
If you get no errors after importing the library and can list the
library version, you know you’ve got OpenCV installed correctly.
If you’re using a USB webcam, you can jump ahead to “Displaying
an Image” on page 151. If you’re using the Raspberry Pi camera
module, there may be one extra step, which we’ll cover now.
Additional Step for the Raspberry
Pi Camera Module
Because we want to use Python and OpenCV to access the Pi-
camera module, we’ll need to install one extra library—picamera.
And because we’re going to be using OpenCV, which makes use
of NumPy arrays, we’ll need to install the array sub-module. (The
reason why I keep saying “may” is that depending on which version
of the Raspberry Pi OS you’re using, these modules may already be
150 Getting Started with Raspberry Pi
GSW_RASPI_4ED_FIN.indd 150GSW_RASPI_4ED_FIN.indd 150 10/28/21 10:54 AM10/28/21 10:54 AM