Open In App

Face detection using Cascade Classifier using OpenCV-Python

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:



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)
cv2.waitKey(0)

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)
  
cv2.waitKey(0)

Output:


Article Tags :