Face Detection with Haar Cascade using OpenCV

Table of Contents
  • Load and Preprocess Images: cv2.imread(), cv2.cvtColor()
  • Load Pretrained Haar Cascade Model: CascadeClassifier()
  • Detect Faces: detectMultiScale

You can obtain the pre-trained Haar Cascade Classifier XML file for face detection from the OpenCV GitHub repository or the OpenCV official website. As of my last knowledge update in September 2021, you can find the file at the following URL:

OpenCV GitHub repository

Save haarcascade_frontalface_default.xml file in a local folder from where you can load it.

Introduction

Face Detection with Haar Cascade using OpenCV is a computer vision technique that allows for the automatic detection of human faces within digital images or video streams. Haar Cascade is a machine learning-based approach that uses a trained model to identify faces based on patterns of pixel intensities. OpenCV, a popular open-source computer vision library, provides tools and pre-trained models for face detection using Haar Cascade.

In this process, an image is loaded, converted to grayscale for simplicity, and then processed by the Haar Cascade classifier. The classifier scans the image at different scales and positions, identifying potential face regions based on predefined criteria. When a face is detected, a bounding box or rectangle is drawn around it.

Face detection with Haar Cascade is widely used in applications such as facial recognition, emotion analysis, and human-computer interaction. It serves as a fundamental step in various computer vision tasks, enabling the localization and subsequent analysis of facial features within images or video frames.

Let us look at a simple example of multiple face detection from images using Haar Cascade classifier from OpenCV.

Face Detection using Haar Cascade Classifier
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
import cv2
import numpy as np
import matplotlib.pyplot as plt
path = 'D:/img/cricket.jpg'
img = cv2.imread(path, cv2.IMREAD_COLOR)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

# Load the cascade
face_cascade = cv2.CascadeClassifier('models/haarcascade_frontalface_default.xml')
# Detect faces
faces = face_cascade.detectMultiScale(gray, 1.1, 4)
# Draw rectangle around the faces
for (x, y, w, h) in faces:
    cv2.rectangle(img, (x, y), (x + w, y + h), (0, 0, 255), 3)

fig, axs = plt.subplots(1,1)
rgb = cv2.cvtColor(img,cv2.COLOR_BGR2RGB)
axs.imshow(rgb)
axs.axis('off')
axs.set_title('Detected Faces',fontsize='medium')

Code Explanation

Step 1: Load the OpenCV library for image processing and computer vision.

Step 2: Import the NumPy library for numerical operations (not used in this code).

Step 3: Import the Matplotlib library for creating plots and visualizations.

Step 4: Specify the file path to the image ('cricket.jpg') that you want to process. Change this path to the location of your image.

Step 5: Read the image from the specified path in color (CV2.IMREAD_COLOR) and load it into the variable 'img'.

Step 6: Convert the loaded color image ('img') to grayscale using the 'cv2.cvtColor' function. Grayscale images are often used for face detection because they reduce the complexity of the data.

Step 7: Load the Haar Cascade Classifier for frontal face detection. This classifier is a pre-trained model used for detecting faces in images.

Step 8: Detect faces in the grayscale image ('gray') using the 'detectMultiScale' method of the 'face_cascade' classifier. The parameters '1.1' and '4' control the scaling factor and minimum neighbors, affecting the sensitivity and accuracy of face detection.

Step 9: Iterate over the detected faces and draw a red rectangle around each detected face on the original color image ('img'). The coordinates and dimensions of each face are retrieved from the 'faces' variable.

Step 10: Create a Matplotlib figure and axis for displaying the image.

Step 11: Convert the color image ('img') from the BGR color space (used by OpenCV) to the RGB color space (used by Matplotlib) so that it can be displayed correctly.

Step 12: Display the RGB image on the Matplotlib axis.

Step 13: Turn off the axis labels and ticks.

Step 14: Set the title of the displayed image to 'Detected Faces' with a medium font size.

No image
Figure 1: Face detection using Haar Cascade classifier with OpenCV.

Explanation of Parameters for cv2.CascadeClassifier()

The cv2.CascadeClassifier() function is a part of the OpenCV library in Python, specifically designed for computer vision tasks such as object detection, face recognition, and more. This function is used to create an instance of a cascade classifier, which is a machine learning-based model used for detecting objects or patterns in images or videos. Here's an explanation of the parameters of the cv2.CascadeClassifier() function:

  1. XML File Path (required):
    The primary parameter you need to provide when calling cv2.CascadeClassifier() is the path to an XML file that contains the pre-trained cascade classifier model. This XML file contains information about the model's architecture, features, and other parameters necessary for object detection. OpenCV provides pre-trained cascade classifiers for various tasks like face detection, eye detection, and more. You can also train your custom cascade classifiers, but this parameter typically points to a pre-trained model's XML file.
  2. Scale Factor (optional):
    The scale factor is an optional parameter that controls how much the image size is reduced at each image scale. It helps in detecting objects at different sizes within the image. A smaller scale factor (e.g., 1.1) will increase the chance of detecting smaller objects but may also increase false positives. A larger scale factor (e.g., 1.3) will decrease the chance of missing objects but may be slower.
  3. Min Neighbors (optional):
    The minNeighbors parameter is also optional and controls the number of neighbors a detected object should have to be considered a valid detection. Increasing this value reduces false positives but may also miss some true positives. Lowering it may result in more detections but with more false positives.
  4. Min Size (optional):
    The minSize parameter specifies the minimum size of the object to be detected. Objects smaller than this size will not be considered. It's specified as a tuple (width, height).
  5. Max Size (optional):
    The maxSize parameter specifies the maximum size of the object to be detected. Objects larger than this size will not be considered. It's also specified as a tuple (width, height).

OpenCV Tutorials