Using a pre-trained cascade classifier

A cascade classifier can be loaded and applied to an image (grayscale) using the following code, where we first read the image, then convert it to grayscale, and finally detect all the faces using a cascade classifier:

import cv2

gray_img = cv2.cvtColor(cv2.imread('example.png'), cv2.COLOR_RGB2GRAY)

cascade_clf = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
faces = cascade_clf.detectMultiScale(gray_img,
scaleFactor=1.1,
minNeighbors=3,
flags=cv2.CASCADE_SCALE_IMAGE)

From the previous code, the detectMultiScale function comes with a number of options: 

  • minFeatureSize is the minimum face size to consider—for example, 20 x 20 pixels.
  • searchScaleFactor is the amount by which we rescale the image (scale pyramid). For example, a value of 1.1 will gradually reduce the size of the input image by 10 %, making it more likely for a face (image) with a larger value to be found.
  • minNeighbors is the number of neighbors that each candidate rectangle will have to retain. Typically, we choose 3 or 5.
  • flags is an options object used to tweak the algorithm—for example, whether to look for all faces or just the largest face (cv2.cv.CASCADE_FIND_BIGGEST_OBJECT).

If detection is successful, the function will return a list of bounding boxes (faces) that contain the coordinates of the detected face regions, as follows:

for (x, y, w, h) in faces: 
    # draw bounding box on frame 
    cv2.rectangle(frame, (x, y), (x + w, y + h), (100, 255, 0), 
thickness=2)

In the previous code, we iterate through the returned faces and add a rectangle outline with a thickness of 2 pixels to each of the faces.

If your pre-trained face cascade does not detect anything, a common reason is usually that the path to the pre-trained cascade file could not be found. In this case, CascadeClassifier will fail silently. Thus, it is always a good idea to check whether the returned classifier casc = cv2.CascadeClassifier(filename) is empty, by checking casc.empty().

This is what you should get if you run the code on the Lenna.png picture:

Image credit—Lenna.png by Conor Lawless is licensed under CC BY 2.0

From the previous screenshot, on the left, you see the original image, and on the right is the image that was passed to OpenCV, and the rectangle outline of the detected face.

Now, let's try to wrap this detector into a class to make it usable for our application.

..................Content has been hidden....................

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