Face Detection Using Viola Jones Haar_cascade Classifiers

kamal_DS
3 min readFeb 21, 2023

--

Face detection is an important task in computer vision and is used in a wide range of applications such as security systems, photo tagging, and video analytics. One of the most widely used methods for face detection is the Haar Cascade Classifier, which was introduced by Viola and Jones in their seminal paper “Rapid Object Detection using a Boosted Cascade of Simple Features”.

In this blog post, we will explore the basic concepts of Haar Cascade Classifiers and how they can be used for face detection. We will also provide a small example using Open CV to detect faces in an image.

What are Haar Cascade Classifiers?

Haar Cascade Classifiers are based on Haar wavelets, which are mathematical functions that can be used to detect edges in an image. These functions are applied to sub-windows of an image to extract features such as edges, lines, and corners. These features are then used to classify the sub-windows as either containing an object of interest (e.g., a face) or not.

A Haar Cascade Classifier is a machine learning algorithm that uses a set of positive and negative training samples to learn a model for face detection. The classifier is trained using a process called Adaboost, which combines weak classifiers (i.e., classifiers that perform only slightly better than chance) into a strong classifier. The resulting classifier is a weighted combination of weak classifiers that can be used to classify new images.

How do Haar Cascade Classifiers work?

Haar Cascade Classifiers work by sliding a sub-window over the image and extracting a set of features for each sub-window. The features are calculated using Haar wavelets and can be represented as a vector. The classifier then uses the vector of features to predict whether the sub-window contains a face or not.

The process of detecting a face using a Haar Cascade Classifier can be summarized as follows:

  1. Sliding a sub-window over the image
  2. Extracting a set of features for each sub-window using Haar wavelets
  3. Classifying the sub-window as either containing a face or not using the learned model

The sub-windows are typically scaled to different sizes to account for variations in the size of faces in the image.

Open CV is a popular open-source computer vision library that provides a wide range of functions for image processing, including face detection using Haar Cascade Classifiers.

To detect faces in an image using Open CV, we need to first load the pre-trained Haar Cascade Classifier for face detection. This can be done using the following code:

Test_Image:

import cv2
import numpy
from predictions import predict
image = cv2.imread('D:\\Computer vision\\Image Processing\\Projects\\Face Recognition\\test_data\\girl.png')


model = cv2.CascadeClassifier('D:\\Computer vision\\Image Processing\\Projects\\Face Recognition\\cascade classifiers\\haarcascade_frontalface_default.xml')


cordinates , num_of_faces = model.detectMultiScale2(image)


print(cordinates)
print('-----')

print(len(num_of_faces))
pt1 = (cordinates[0][0] , cordinates[0][1])
pt2 = (cordinates[0][2] + cordinates[0][0] ,cordinates[0][3] + cordinates[0][1])

font = cv2.FONT_HERSHEY_COMPLEX
text = 'Number of Faces detected = '+str(len(num_of_faces))
cv2.rectangle(image,pt1,pt2,(0,0,255),2,cv2.LINE_AA)
cv2.putText(image,text,(15,25) , font,0.5,(0,0,0),2,cv2.LINE_8)
predict(image)

cordinates , num_of_faces = model.detectMultiScale2(image)
# the above line detects the Number of faces in the image
# and their coordinates (x1,y1) and (height and width)
pt1 = (cordinates[0][0] , cordinates[0][1])
pt2 = (cordinates[0][2] + cordinates[0][0] ,cordinates[0][3] + cordinates[0][1])

# Since the result will be in 2d format ex: [[ 89 150 274 274]]
# Accesssing x , y , h , w values and placing a rectangle box on the top of the Image

Result:

For Geometric Concepts Using an Open CV, You can Follow My Previous Blog = https://medium.com/@korlakuntasaikamal10/geometric-shapes-on-images-using-opencv-de367122af9c

You can get the complete code from here: https://github.com/saikamal3344/Face-Detection-using-Harcascade-Classifiers-

--

--

kamal_DS

Interested to work in the field of Artificial Intelligence, Machine Learning, Deep Learning, NLP and Computer Vision.