CHAPTER 8 Computer Vision

[Pages:30]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 $ 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

229

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

230 | Chapter 8: Computer Vision

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 Pi 2.

See Also

For information on OpenCV, see . The first recipe in this chapter to use OpenCV is Recipe 8.4. There you will find use- ful details for getting started with OpenCV.

8.2 Setting Up a USB Camera for Computer Vision

Problem

You want to set up a USB webcam for use in computer vision (CV) projects.

Solution

Use a USB webcam that is compatible with the Raspberry Pi (see wrI0U). Choose a good-quality camera. If you are working on a project for which you need the camera close to the subject, select one that has a manual focus option. For getting really close to the subject, a low-cost USB endoscope can be useful. Depending on your CV project, you might want to set up a well-lighted area. Figure 8-1 shows a simple light box made from a translucent plastic storage box that is illuminated from the sides and top to give even lighting. The webcam is attached to a hole in the top of the box. This arrangement is used in Recipe 8.4.

8.2 Setting Up a USB Camera for Computer Vision | 231

Figure 8-1. Using a homemade "light box" for even illumination

You can also buy commercial light tents, designed for photography, that work well. You might need a little trial and error to get your system brightly and evenly illumi- nated. Shadows can be particularly problematic.

Discussion

You can test out your USB camera from the OpenCV console. Start Python 3 and then enter the commands shown below:

>>> import cv2 >>> from imutils.video import VideoStream >>> vs = VideoStream(src=0).start() >>> img = vs.read() >>> cv2.imshow('image',img) >>> cv2.waitKey(0)

A window should open showing an image from your camera after the last line of code is entered. You may have to close the entire terminal window to get the image win- dow to close. In OpenCV even single images are just taken as frames from a video stream. Notice that on the third line above, we have src=0. This means the first camera that OpenCV can find. So, if you have multiple cameras, you can use a different number here.

232 | Chapter 8: Computer Vision

Once the image has been read using vs.read() you can then use OpenCV's imshow utility method to display the image. You will find that you use this a lot to debug your computer vision projects. The final cv2.waitKey(0) is required to allow OpenCV to actually render the image in the background, until a key is pressed.

See Also

To use a Raspberry Pi Camera Module with OpenCV, see Recipe 8.3.

8.3 Using a Raspberry Pi Camera Module for Computer Vision

Problem

You want to use a Raspberry Pi Camera Module that connects directly to your Rasp- berry Pi with OpenCV.

Solution

The Raspberry Pi Camera Module should automatically show up as a camera device once you have followed Recipe 1.17 to install the camera module.

Discussion

Note that in early versions of Raspbian, you had to install a driver to make the camera module available to OpenCV; if OpenCV does not detect the camera module, try updating your version of Raspbian to the latest version (Recipe 3.40).

See Also

See Recipe 1.17 for information on installing the Raspberry Pi Camera Module. See for information on the picamera Python module. To use a USB camera with OpenCV, see Recipe 8.2.

8.4 Counting Coins

Problem

You want to use computer vision to count the number of coins in your webcam's view.

8.3 Using a Raspberry Pi Camera Module for Computer Vision | 233

Solution

Use OpenCV's Hough Circles detector to provide a real-time count of the number of coins placed within view of the webcam. This is one use of CV for which you really need good lighting and a camera fixed in position. I used the setup shown in Figure 8-1. The critical part of many computer vision projects is getting the parameters right and this recipe is no exception. For this reason, before using the final program that just gives a count of coins, we will use a test program that draws outlines around the coins, so that we can see what's going on. You can find the code for this test code as well as the other examples in this recipe with the downloads for the book (see #new-sec-ed3-ch03-03). The program is called ch_08_coin_count_test.py. Put some coins under your camera and then run the program and a window like that of Figure 8-2 should appear.

Figure 8-2. Counting coins

234 | Chapter 8: Computer Vision

If you are lucky, then your coins will all have circles around them and you should see output in the console like this:

$ python3 ch_08_coin_count_test.py [[[380.5 338.5 37.9]

[553.5 249.5 34.9] [538.5 357.5 31.4] [546.5 442.5 30.7] [418.5 244.5 33.1]]]

To refresh the image press any key and when you want to exit the program, press the x key. If your coins are not all circled, then you will need to adjust some parameters in bold in the program ch_08_coin_count_test.py listed below.

import cv2 from imutils.video import VideoStream from imutils import resize

vs = VideoStream(src=0).start()

while True: img = vs.read() img = resize(img, width=800) img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) img = cv2.blur(img, (3, 3))

detected_circles = cv2.HoughCircles(img, cv2.HOUGH_GRADIENT, 1, 20, param1 = 50, param2 = 30, minRadius = 15, maxRadius = 100)

print(detected_circles)

for pt in detected_circles[0]: a, b, r = pt[0], pt[1], pt[2] cv2.circle(img, (a, b), r, (0, 255, 0), 2)

cv2.imshow('image', img) key = cv2.waitKey(0) cv2.destroyAllWindows() if key == ord('x'):

break

vs.stop()

You should not need to change the parameter param1. If you are interested in what it and the other parameters do, you can read about them here: https:// docs.2.4/modules/imgproc/doc/feature_detection.html?highlight=hough- circles.

8.4 Counting Coins | 235

If you are getting a lot of false circles, try increasing the value of param2. But, the most likely parameters in need of change are the minRadius and maxRadius, as these will be sensitive to the resolution of your camera, its lens focal length and the distance to the coins. So if no coins get circled, increase the value of maxRadius. Tweek the parameters until your coins are being correctly identified. Here's a quick run through of how the test program works. Most of the code lives in a try block within the while True: block. This ensures that when you press CTRL-c to quit the program, the video stream is stopped. After reading the image, there are a couple of stages of processing. First, the image is resized to a width of 800, then converted to grayscale and finally a blur filter applied. The blur filter helps improve the circle matching. The call to cv.HoughCircles returns an array of the circles that OpenCV has found. The three values are the x and y coordinates of the center of the circle and the circle's radius. To render these circles on top of the image of the coins, a for loop is used to iterate over each of the detected circle and then uses the cv2.circle method to draw a black (0, 0, 0) circle of with 2 pixels around the coin. The actual coin counting program is just a simplification of the test program, so when you are ready, run the program ch_08_coin_count.py. Try moving coins in and out of the field of view and notice how the count changes.

Discussion

Although not something that you would want to put into a vending machine, an interesting project would be to use the radius of the coins to identify their monetary value and add up the value of the coins on the table.

See Also

For information on installing OpenCV, see Recipe 8.1. For information on setting up a camera, see Recipe 8.2.

8.5 Face Detection

Problem

You want to find the coordinates of faces in a photograph or webcam image.

236 | Chapter 8: Computer Vision

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download