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.
This is what you should get if you run the code on the Lenna.png picture:
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.