#21 Face Detection with OpenCV
Source: hostadvice.com

#21 Face Detection with OpenCV

Today we will use OpenCV to detect faces and draw a bounding box around them.

What is OpenCV?

This is a computer vision library. It was created by Intel in 1999 and was later made open-source and released to the public. Since human faces are so diverse, face detection models are trained on large amounts of input data for accurate detection. The training dataset must be diverse.

Haar Cascade Classifiers

This method was first introduced in the paper

, written by Paul Viola and Michael Jones.

This paper introduces a method for detecting objects in images in less time. The model follows a three-step process.

introduces a method for quickly and accurately detecting objects in images, particularly faces, through a series of steps:

  • Feature Selection: The authors identify a set of simple features, such as rectangular features, that can efficiently represent different aspects of an object, like edges or textures.

  • Adaboost Training: They employ the Adaboost algorithm to select a small set of highly discriminative features from the initial pool. Adaboost iteratively trains a series of weak classifiers, each focusing on the misclassified examples of the previous classifiers.

  • Cascade Classifier: The authors introduce a cascade classifier structure consisting of several stages, each containing a subset of the selected weak classifiers. The cascade aims to quickly reject non-object regions in an image, thus speeding up the detection process.

Code:

# !pip install opencv-python 
import cv2 
import matplotlib.pyplot as plt        
# Read the image. 
# Returns the image in the form of a Numpy array 
imagePath = '/content/test_image_family.jpg' 
img = cv2.imread(imagePath)        
# Let's look at the dimensions of the image 
img.shape        

This is a 3-dimensional array. They represent the height, width and channels respectively. Since this a colour image, there are 3 channels- RGB (Red, green and Blue) The OpenCV library uses the opposite layout- BGR (Blue, Green and Red)

# Convert the image to Grayscale
gray_image = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
gray_image.shape        

Since the image no longer has 3 channels, there are only two values in the array.

# Load the pre-trained Haar Cascade Classifier
face_classifier = cv2.CascadeClassifier(
    cv2.data.haarcascades + "haarcascade_frontalface_default.xml"
) 

# Performing the Face Detection
face = face_classifier.detectMultiScale(
    gray_image, scaleFactor = 1.1, minNeighbors = 20 , minSize=(40,40)
)        

The detectMultiScale() method is used to identify faces of different sizes in the Input Image.

  • scaleFactor: Scales down the size of the input image to make it easier for the algorithm to detect larger faces. We have specified a scale factor of 1.1, indicating that we want to reduce the image size by 10%.
  • minNeighbors: The cascade classifier applies a sliding window through the image to detect faces in it. You can think of these windows as rectangles. Initially, the classifier will capture a large number of false positives. These are eliminated using the minNeighbors parameter, which specifies the number of neighbouring rectangles that need to be identified for an object to be considered a valid detection. To summarize, passing a small value like 0 or 1 to this parameter would result in a high number of false positives, whereas a large number could lead to losing out on many true positives. The trick here is to find a tradeoff that allows us to eliminate false positives while also accurately identifying true positives.
  • minSize: This sets the minimum size of the object to be detected. The model will ignore faces that are smaller than the minimum size specified.

# Drawing the bounding boxes
for (x, y, w, h) in face:
    cv2.rectangle(img, (x, y), (x + w, y + h), (0, 255, 0), 4)

# Displaying the Image
# we first need to convert the image from the BGR format to RGB:
img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)

plt.figure(figsize=(20,10))
plt.imshow(img_rgb)
plt.axis('off')
plt.show()        

Input Image

For the input image, I intentionally chose one that had blurry faces. I wanted to see how much the model overshoots to detect blurry faces.

Results:

Changing minNeighbors parameter

# This is the base code for performing the Face Detection 
# I will change the minNeighbors with everything else constant 
face = face_classifier.detectMultiScale(
    gray_image, scaleFactor = 1.1, minNeighbors = 0, minSize=(40, 40))        

  • minNeighbors = 5

  • minNeighbors = 8

  • minNeighbors = 11

  • minNeighbors = 20

As seen from the images, there is a trade-off between removing noise and detecting blurred faces.

Full Code: https://github.com/RiyaChhikara/100daysofComputerVision/blob/main/Day21_face_recognition.ipynb


Sources:

  1. Face Detection with Python using OpenCV: https://www.datacamp.com/tutorial/face-detection-python-opencv
  2. Rapid Object Detection using a boosted cascade of simple features: https://ieeexplore.ieee.org/document/990517


Imran Zahoor

Senior Network Consultant Engineer @ Cisco | Network, Automation, ML, DC, Cloud,AI

12 个月

Good job Riya

要查看或添加评论,请登录

Riya Chhikara的更多文章

社区洞察

其他会员也浏览了