Cascade Classifier
Step 1
Prepare two sets of images.
The first set consists of positive images, while the second set consists of negative images.
Step 2
Send all the images to a machine learning algorithm called AdaBoost so that it learns the features of the images as well as the details of a face.
For example, a face consists of eyes, mouth, nose, and so on.
Step 3
[1]'cascade'
This picture shows that if any of the features from C1 to CN is false, the cascade returns ‘No Detection’.
Example Code
I’m going to detect faces in the ‘test image’ below using OpenCV.
[2] 'test image'
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import cv2
face_detector = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
image = cv2.imread('people1.jpg')
# Why use grayscale images?: Cascade detectors with OpenCV recommend using grayscale images.
image_gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# scaleFactor: Image reduction ratio, default value is 1.1
# minNeighbors: Speicies how many neighboring squares must be detected to set it as the final detection area. default is 3
# minSize: Minimum object size. (w, h) tuple
# maxSize: Maximum object size. (w, h) tuple
detections = face_detector.detectMultiScale(image_gray,
scaleFactor=1.21,
minNeighbors=None,
minSize=None,
maxSize=None)
for (x, y, w, h) in detections:
cv2.rectangle(image, (x, y), (x+w, y+h), (0,255,0), 5)
cv2.imshow(image)
The result of code.