Face Detection using a Cascade Classifier



Face detection is the process of identifying faces in an image or video frame. It is a fundamental task in computer vision and it has many applications like face recognition, facial expression analysis, and many more.

One of the most popular methods for face detection is the Cascade Classifier, which was introduced by Viola, P. and Jones, M in 2001. The Cascade Classifier is a machine learning algorithm that uses Haar-like features to detect faces in an image.

Scikit-image provides a versatile method for detecting faces in images using the "Cascade" class. This class is part of the "feature" submodule within the scikit-image library.

Using the skimage.feature.Cascade()

The skimage.feature.Cascade() class is designed for creating cascades of classifiers that are used in object detection. Which is inherited from the "object" class.

The fundamental concept behind a cascade of classifiers is to build multiple classifiers of medium accuracy and then ensemble them to form a single strong classifier. This approach offers several advantages. One of the key benefits is that easy examples can be classified by evaluating only a subset of the classifiers within the cascade. This makes the detection process much faster compared to evaluating one strong classifier for every instance.

It is important to note that, in this implementation, the concept of the cascade approach employed the multi-scale block local binary pattern (MB-LBP) features, instead of using the set of Haar-like features.

Syntax

The following is the syntax of this class −

skimage.feature.Cascade()

The below example demonstrates the process of face detection within an image using an object detection framework based on machine learning.

To begin the detection process, an XML file is required to access the pre-trained data. The framework is designed to work with files that have been trained using Multi-block Local Binary Patterns Features (referred to as MB-LBP) and Gentle Adaboost with an attentional cascade. So, the detection framework will also work with xml files from OpenCV. which contain pre-trained models for various objects, including cat faces and profile faces. If your goal is to detect frontal faces, the corresponding file is already included in the scikit-image library.

The next step involves specifying parameters for the "detect_multi_scale" function. Each parameter has a specific meaning −

  • scale_ratio: This parameter is responsible for multi-scale search for faces by adjusting the size of the search window. The smallest window size corresponds to the training window size specified in the XML file. This parameter determines the increment ratio for the search window size at each step. Increasing this parameter reduces search time but may affect accuracy, potentially leading to some faces not being detected.

  • step_ratio: This parameter defines the step of the sliding window used for face detection at various image scales. If set to one, it searches all possible locations. A value greater than one (e.g., two) means the window is moved by a specific number of pixels, reducing the number of locations searched. Increasing this parameter can speed up the algorithm but may impact accuracy.

  • Min_size and max_size: These parameters specify the minimum and maximum size of the search window during the scale search. If you know the expected size of faces in the images you're working with, you should specify these parameters as precisely as possible, because it can reduce computational time and minimize false detections. Increasing the "min_size" parameter can notably speed up processing because the majority of time is spent searching at the smallest scales.

  • min_neighbor_number and intersection_score_threshold: These parameters are used to cluster excessive detections of the same face and filter out false detections. True faces often have a lot of detections around them, while false detections typically result in only a single detection.

  • The algorithm first identifies clusters, where two rectangle detections are placed in the same cluster if their intersection score between them is larger than the intersection_score_threshold. The intersection score is calculated as the intersection area divided by the smaller rectangle's ratio. This approach was chosen over the intersection over union method to handle a corner case where a small rectangle within a larger one has a small intersection score. Then, each cluster is filtered using the "min_neighbor_number" parameter, which retains clusters with an equal or greater number of detections.

It's important to remember that some false detections are inevitable. To create a highly precise detector, you may need to undertake the training process yourself using OpenCV's train cascade utility.

Example

The following example performs face detection using a cascade classifier and displaying the detected faces in an image.

import matplotlib.pyplot as plt
from matplotlib import patches
from skimage import data, io
from skimage.feature import Cascade

# Load the pre-trained face cascade classifier.
trained_cascade = data.lbp_frontal_face_cascade_filename()

# Initialize the face detector.
face_detector = Cascade(trained_cascade)

# Load an image containing faces.
image = io.imread('Images/facedetection_input.jpg')

# Detect faces in the image with multiple scales and settings.
detected_faces = face_detector.detect_multi_scale(img=image,
   scale_factor=1.2,
   step_ratio=1,
   min_size=(60, 60),
   max_size=(123, 123))

# Display the original image.
plt.imshow(image, cmap='gray')
image_description = plt.gca()

# Draw rectangles around detected faces.
for face in detected_faces:
   image_description.add_patch(
      patches.Rectangle(
         (face['c'], face['r']),
         face['width'],
         face['height'],
         fill=False,
         color='r',
         linewidth=2
      )
   )

plt.show()

Output

On executing the above program, you will get the following output −

face detection cascade classifier
Advertisements