Installing and Testing OpenCV
To access the camera with Python code, we’re going to be using
OpenCV (Figure 9-7), which is a feature-packed open-source com-
puter vision library. OpenCV makes it really easy to get images from
the camera, display them on screen, or save them as files. But what
makes OpenCV really stand out is its computer vision algorithms,
which can do some pretty amazing things. Besides basic image
transformations, it can also track, detect, and recognize objects in
an image or video. Later on in this chapter, we’ll try basic face de-
tection with OpenCV (“Face Detection” on page 158). OpenCV is
used by many working professionals in the fields of machine learn-
ing, artificial intelligence, and computer vision, and is considered
a fully professional tool in these fields.
Figure 9-7.
Open CV logo
To install OpenCV for Python, you’ll need to start by installing the
other libraries it depends on. For those, you can use apt-get:
$ sudo apt -get install libhdf5-dev libhdf5-serial-dev
libhdf5-103
$ sudo apt -get install libqtgui4 libqtwebkit4 libqt4-test
python3-pyqt5
$ sudo apt -get install libatlas-base-dev
$ sudo apt -get install libjasper-dev
Working with Cameras 149
GSW_RASPI_4ED_FIN.indd 149GSW_RASPI_4ED_FIN.indd 149 10/28/21 10:54 AM10/28/21 10:54 AM
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
installed. If you’re not sure, just do the following, and if the modules
are installed, you’ll simply get a message telling you so.) To install
what you need, enter the following in theTerminal:
$ pip3 install "picamera[array]"
Now you’re ready to interact with the camera using OpenCV!
Displaying an Image
For many of the examples in this chapter, you’ll need to work in the
desktop environment so that you can display images on the screen.
You can work in IDLE, or save your code as
.py
files from the default
text editor and execute them from the Terminal window. And while
you
can
use a VNC connection, we don’t recommend it, simply
because you often won’t be able to see the camera display (see the
note above.)
We’re going to start you off with some OpenCV basics using image
files, and then you’ll work your way up to reading images from the
camera. Once you’ve got images coming in from the camera, it will
be time to try some face detection:
1. Create a new directory within your home directory called
opencv-test.
2. Open the web browser and search for an image that interests
you. I used a photograph of raspberries from Wikipedia and re-
named it
raspberries.jpeg
.
3. Right-click on the image and click “Save Image As.
4. Save the image within the
opencv-test
folder.
5. In the File Manager (on the Accessories menu), open the
opencv-test
folder and right-click in the folder. Choose Create
New Blank File.
6. Name the file
image-display.py
.
7. Double-click on the newly created
image-display.py
file to
open it in the text editor.
8. Enter the code in Example 9-1.
9. Save the
image-display.py
file and run it from the Terminal
window. If you’ve got everything right, you’ll see a photo in a new
Working with Cameras 151
GSW_RASPI_4ED_FIN.indd 151GSW_RASPI_4ED_FIN.indd 151 10/28/21 10:54 AM10/28/21 10:54 AM
window as in Figure 9-8. You can close the window itself, or in the
Terminal, press Ctrl-C to end the script.
Figure 9-8.
The raspberry photo displayed in a window
Example 9-1. Source code for image-display.py
import cv2
img = cv2.imread("raspberries.jpeg")
cv2.imshow("Raspberries",img)
cv2.waitKey(0)
cv2.destroyAllWindows()
Import the OpenCV library.
Creates a new image object, img, and reads ‘raspberries.jpeg’
into it.
Creates a window object named ‘Raspberries’ and displays the
image object img within it.
Wait for the user to press a key to end the program.
Clean up after ourselves by destroying all windows when the
program ends.
152 Getting Started with Raspberry Pi
GSW_RASPI_4ED_FIN.indd 152GSW_RASPI_4ED_FIN.indd 152 10/28/21 10:54 AM10/28/21 10:54 AM
Image size
You may notice that the
image-display.py
script is an excellent
example of a piece of code doing exactly what you tell it to—in
this case, displaying an image in its exact form. If the image you
choose happens to be 5440×2880 pixels, that’s what OpenCV
will display, regardless of whether or not it’ll fit on your monitor.
Keep reading to see how to resize your image to a more man-
ageable display size.
Modifying an Image
Now that you can load an image into memory and display it on
the screen, the next step is to modify the image before displaying
it (doing this does not modify the image file itself; it simply modifies
the copy of the image that’s held in memory):
1. Save the
image-display.py
file as
superimpose.py
.
2. Make the enhancements to the code that are shown in Ex-
ample 9-2.
3. Save the file and run it from the command line.
4. You should see the same image, but now superimposed with
the shape and the text. To close the window, press Q’ on your
keyboard.
Example 9-2. Source code for superimpose.py
import cv2
img = cv2.imread("raspberries.jpeg’")
font = cv2.FONT_HERSHEY_SIMPLEX
org = (50,50)
font_scale = 1
color = (0, 255, 0)
thickness = 2
cv2.imshow("Raspberries", image)
Working with Cameras 153
GSW_RASPI_4ED_FIN.indd 153GSW_RASPI_4ED_FIN.indd 153 10/28/21 10:54 AM10/28/21 10:54 AM
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset