Face Detection with Haar Cascade using OpenCV
- 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.
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.
Figure 1: Face detection using Haar Cascade classifier with OpenCV.