OpenCV Face Detection
OpenCV provides the Haar Feature-based Cascade Classifiers for face detection, this model was presented by Paul Viola and Michael Jones in 2001.
This method apply series of classifiers to every subwindow of input picture, the first one classifier eliminates a large number of non-faces examples with very little processing. The other classifiers eliminate additional negatives but require additional computation. After several stages of processing the number of sub-windows have been reduced radically.
As you can see in the picture below, this architecture uses filters to extract features from image, and those filters became more and more complexe in each stage from one to n,
Let’s try this detector.
import cv2 as cv
# Load Haar Cascade Classifier
face_cascade = cv.CascadeClassifier('haarcascade_frontalface_default.xml')
eye_cascade = cv.CascadeClassifier('haarcascade_eye.xml')
# Read the image
img = cv.imread('test.jpg')
# Convert image to graysclae
gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
# Detect faces
faces = face_cascade.detectMultiScale(gray, 1.3, 5)
for (x,y,w,h) in faces:
roi_gray = gray[y:y+h, x:x+w]
roi_color = img[y:y+h, x:x+w]
eyes = eye_cascade.detectMultiScale(roi_gray)
for (ex,ey,ew,eh) in eyes:
You find the models in OpenCV github repository, Link.
The result :