Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Face detection using Cascade Classifier using OpenCV-Python

  • Difficulty Level : Medium
  • Last Updated : 18 Oct, 2021

In this article, we are going to see how to detect faces using a cascade classifier in OpenCV Python. Face detection has much significance in different fields of today’s world. It is a significant step in several applications, face recognition (also used as biometrics), photography (for auto-focus on the face), face analysis (age, gender, emotion recognition), video surveillance, etc.

One of the popular algorithms for facial detection is “haarcascade”. It is computationally less expensive, a fast algorithm, and gives high accuracy.

Haarcascade file can be download from here: haarcascade_frontalface_default.xml

It works in four stages:

  • Haar-feature selection: A Haar-like feature consists of dark regions and light regions. It produces a single value by taking the difference of the sum of the intensities of the dark regions and the sum of the intensities of light regions. It is done to extract useful elements necessary for identifying an object. The features proposed by viola and jones are:

  • Creation of Integral Images: A given pixel in the integral image is the sum of all the pixels on the left and all the pixels above it. Since the process of extracting Haar-like features involves calculating the difference of dark and light rectangular regions, the introduction of Integral Images reduces the time needed to complete this task significantly.
  • AdaBoost Training: This algorithm selects the best features from all features. It combines multiple “weak classifiers” (best features) into one “strong classifier”. The generated “strong classifier” is basically the linear combination of all “weak classifiers”.
  • Cascade Classifier: It is a method for combining increasingly more complex classifiers like AdaBoost in a cascade which allows negative input (non-face) to be quickly discarded while spending more computation on promising or positive face-like regions. It significantly reduces the computation time and makes the process more efficient.

OpenCV comes with lots of pre-trained classifiers. Those XML files can be loaded by cascadeClassifier method of the cv2 module. Here we are going to use haarcascade_frontalface_default.xml for detecting faces.

Stepwise Implementation:

Step 1: Loading the image


img = cv2.imread('Photos/cric.jpg')

Step 2: Converting the image to grayscale

Initially, the image is a three-layer image (i.e., RGB), So It is converted to a one-layer image (i.e., grayscale). 


gray_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

Step 3: Loading the required haar-cascade XML classifier file

CascadeClassifier method in cv2 module supports the loading of haar-cascade XML files. Here, we need “haarcascade_frontalface_default.xml” for face detection.


haar_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')

Step 4: Applying the face detection method on the grayscale image

This is done using the cv2::CascadeClassifier::detectMultiScale method, which returns boundary rectangles for the detected faces (i.e., x, y, w, h). It takes two parameters namely, scaleFactor and minNeighbors. ScaleFactor determines the factor of increase in window size which initially starts at size “minSize”, and after testing all windows of that size, the window is scaled up by the “scaleFactor”, and the window size goes up to “maxSize”. If the “scaleFactor” is large, (e.g., 2.0), there will be fewer steps, so detection will be faster, but we may miss objects whose size is between two tested scales. (default scale factor is 1.3). Higher the values of the “minNeighbors”, less will be the number of false positives, and less error will be in terms of false detection of faces. However, there is a chance of missing some unclear face traces as well.


faces_rect = haar_cascade.detectMultiScale(
    gray, scaleFactor=1.1, minNeighbors=9)

Step 5: Iterating through rectangles of detected faces

Rectangles are drawn around the detected faces by the rectangle method of the cv2 module by iterating over all detected faces. 


for (x, y, w, h) in faces_rect:
    cv2.rectangle(img, (x, y), (x+w, y+h), (0, 255, 0), thickness=2)
cv2.imshow('Detected faces', img)

Below is the implementation:


# Importing OpenCV package
import cv2
# Reading the image
img = cv2.imread('Photos/cric4.jpg')
# Converting image to grayscale
gray_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# Loading the required haar-cascade xml classifier file
haar_cascade = cv2.CascadeClassifier('Haarcascade_frontalface_default.xml')
# Applying the face detection method on the grayscale image
faces_rect = haar_cascade.detectMultiScale(gray_img, 1.1, 9)
# Iterating through rectangles of detected faces
for (x, y, w, h) in faces_rect:
    cv2.rectangle(img, (x, y), (x+w, y+h), (0, 255, 0), 2)
cv2.imshow('Detected faces', img)


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!