Saturday 16 March 2024

OpenCV: Face detection

In this post, I am going to use Haar Cascade Classifiers, to detect the faces in an Image. Haar Cascade Classifiers are machine learning models that have been trained to recognize specific objects,

 

Step 1: Go to below location and download haarcascade_frontalface_default.xml file.

https://github.com/opencv/opencv/tree/master/data/haarcascades

 

'haarcascade_frontalface_default.xml' file is one of the pre-trained models provided by OpenCV for face detection.

 

Step 2: Get an instance of Haar cascade classifier.

 

haar_cascade = cv.CascadeClassifier('haarcascade_frontalface_default.xml')

In the above snippet, 'haarcascade_frontalface_default.xml' is the XML file that contains the pre-trained model for face detection.

 

Step 3: Detect faces.

faces_rectangle = haar_cascade.detectMultiScale(gray_scaled_image, scaleFactor=1.3, minNeighbors=1)

'detectMultiScale' method is used to detect objects (faces, in this case) in an image. Coordinates of the detected faces are stored in the variable ‘faces_rectangle’.

 

Signature

cv2.detectMultiScale(image, scaleFactor=1.1, minNeighbors=3, flags=0, minSize=None, maxSize=None)

 

Following table summarizes the parameters of detectMultiScale method.

 

Parameter

Description

image

Source image on which you want to detect the objects.

scaleFactor

Controls how much the image size is reduced at each image scale. Default value is 1.1, that means image size is redued by 10 percent.

minNeighbors

S ecifies how many neighbors each candidate object should have to retain it. A higher value means that the algorithm will be more selective in accepting detected objects.

A lower value might return more false positives.

flags

This is optional parameter. It gives you additional control over the object detection process.

minSize

This is optional parameter (width, height). Specify the minimum object size that detect should consider.

maxSize

This is optional parameter (width, height). Specify the maximum object size that the detector should consider.

 

Step 4: Draw a rectangle using the coordinates returned by 'detectMultiScale' method.

for (a, b, c, d) in faces_rectangle:
    cv.rectangle(image, (a,b), (a+c, b+d), color=(0, 255, 0), thickness=3)

Let’s work with below image to identify the faces

 

rrr.png



Find the below working application.

 

face_detection.py

 

import cv2 as cv

image = cv.imread('rrr.png')
gray_scaled_image = cv.cvtColor(image, cv.COLOR_BGR2GRAY)

haar_cascade = cv.CascadeClassifier('haarcascade_frontalface_default.xml')
faces_rectangle = haar_cascade.detectMultiScale(gray_scaled_image, scaleFactor=1.3, minNeighbors=1)
print(f'Total faces found {len(faces_rectangle)}')

for (a, b, c, d) in faces_rectangle:
    cv.rectangle(image, (a,b), (a+c, b+d), color=(0, 255, 0), thickness=3)

cv.imshow('image', image)

cv.waitKey(0)

# Close the OpenCV windows
cv.destroyAllWindows()

 

Output

Total faces found 7

Total 7 faces are detected by the algorithm. Two foreground faces and one background face are correct, remaining faces are false positives.

 


 

How to address false positives?

By tweaking the parameters like minSize, minNeighbors etc., we can address false positives.

faces_rectangle = haar_cascade.detectMultiScale(gray_scaled_image, scaleFactor=1.3, minNeighbors=5, minSize=(110, 110))


a.   Set the scaleFactor to 1.3

b.   minNeighbors to 5

c.    minSize to (110, 110)

 

Above statement identifies 2 faces.



 


Previous                                                    Next                                                    Home

No comments:

Post a Comment