Introduction
Face detection is a computer technology that determines the locations and sizes of human faces in arbitrary (digital) images. It detects facial features and ignores anything else, such as buildings, trees and bodies. Face detection can be regarded as a more general case of face localization.
The first step in automatic facial recognition is the accurate detection of human faces in an arbitrary scene. When faces are localized exactly, the recognition is performed on the detected face.
Haar Cascade Detection
It is a machine learning approach where a cascade function is trained from a lot of positive and negative images. This method was proposed by Paul Viola and Michael Jones in their Paper "Rapid Object Detection using a Boosted Cascade of Simple Features" in 2001.
Initially, the algorithm needs a lot of positive (Images of Faces) and negative Images (images without faces) to train the classifier. Then we need to extract Haar features from each image shown below.
Each feature is a single value obtained by subtracting the sum of pixels under white rectangle from the sum of pixels under black rectangle. These features are calculated for all possible location and scale.But among all feature we calculate most of them are irrelevant.
AdaBoost is used to select the best features out of all possible features.It selects 6000 best features.
These features are applied on each convolutional window of size 24x24 and check out if it is a face or not, it is too much time consuming because most of the region in an image is non-face region.
Therefore we use Cascade of Classifier. Instead of applying all the 6000 feature on a window, group the features into different stages of classifiers and apply one by one.If a window fails at any stage, discard it, we don't consider remaining features on it.
OpenCV comes with a trainer as well as a detector.If you want to train your own classifier for any object like car, planes etc. You can use OpenCV to create your own. I will be covering this in my later post in detail. For now, we will be using a pre-trained classifier.
Implementation in Python
OpenCV contains many pre-trained classifiers for face, body, eyes, number plate etc.
Let's go ahead and dive into some code.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import cv2
#load pretrained face detection cllassifier
face_cascade = cv2.CascadeClassifier("haarcascade_frontalface_default.xml")
# load image
img = cv2.imread("test_img.jpg")
# convert the image to grayscale
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# apply face detection on grayscale image ,
# It returns a list of rectangle of each face
# (x,y) -> top Left corner of each Face
# (w,h) -> width and height of each Face
faces = face_cascade.detectMultiScale(gray,1.3,5)
# draw rectangle around each face on input image
for (x,y,w,h) in faces:
cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2)
# Display Result
cv2.imshow("img",img)
k = cv2.waitKey(0)
cap.release()
cv2.destroyAllWindows()
Subscribe and Download Code.
If you like this post,.Subscribe
To download the code: Click here.
If you have any doubt or suggestion , feel free to ask anything in the comment section.
Thanks.
Really helpful.
ReplyDeleteVery informative blog.
Thanks for the efforts.