Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

OpenCV Face Detection

OpenCV Face Detection

Face detection is a classic problem in computer vision, and OpenCV provides a face detection method based on Haar feature cascades, which is simple, easy to use, and highly effective.

This article will introduce in detail how to use the cv2.CascadeClassifier() in OpenCV for face detection.

Introduction to Haar Feature Classifiers

A Haar feature classifier is a machine learning method based on Haar-like features, proposed by Paul Viola and Michael Jones in 2001. It extracts Haar-like features from images and trains using the AdaBoost algorithm, ultimately generating a classifier used to detect objects (such as faces) in images.

A Haar-like feature is a simple rectangular feature that extracts features by computing the difference in pixel values between different regions of an image. For example, a Haar-like feature can be the difference between the sums of pixel values of two adjacent rectangles. These features can capture structural information such as edges and lines in an image.

Haar Feature Classifiers in OpenCV

OpenCV provides pre-trained Haar feature classifiers that can be used directly for face detection. These classifiers are stored as XML files and contain trained model parameters.

The in OpenCV cv2.CascadeClassifier() class is used to load and use these classifiers.

Implementation Steps for Face Detection

  1. Load the Haar feature classifier model: Use cv2.CascadeClassifier() Load the pre-trained face detection model.

  2. Read the image: Use cv2.imread() Read the image to be detected.

  3. Convert to grayscale: Convert the image to grayscale, because Haar feature classifiers run faster on grayscale images.

  4. Detect faces: Use detectMultiScale() method detects faces in the image.

  5. Draw the detection results: Draw the detected face rectangles on the image.

  6. Display the result: Display the detection result.

Load the Haar Feature Classifier

Before using a Haar feature classifier, you first need to load the pre-trained classifier model. OpenCV provides multiple pre-trained classifiers, such as the one for face detection, haarcascade_frontalface_default.xml。

Example

import

cv2

Load the Haar Feature Classifier

face_cascade

=

cv2.

CascadeClassifier

(

cv2.

data

.

haarcascades

'haarcascade_frontalface_default.xml'

)

Read an image

Before performing face detection, you need to read the image to be detected. OpenCV provides the cv2.imread() function to read the image.

Example

Read an image

image

=

cv2.

imread

(

'image.jpg'

)

Convert to grayscale image

Haar feature classifiers typically detect on grayscale images, so the color image needs to be converted to grayscale.

Example

Convert to grayscale image

gray_image

=

cv2.

cvtColor

(

image

,

cv2.

COLOR_BGR2GRAY

)

Perform Face Detection

Use cv2.CascadeClassifier.detectMultiScale() method performs face detection. This method returns the rectangles of detected face regions (x, y, w, h), where (x, y) is the top-left coordinate of the rectangle, and w and h are the width and height of the rectangle, respectively.

Example

Perform Face Detection

faces

=

face_cascade.

detectMultiScale

(

gray_image

,

scaleFactor

=

1.1

,

minNeighbors

=

5

,

minSize

=

(

30

,

30

)

)

Draw the Detection Results

After faces are detected, you can use the cv2.rectangle() method to draw rectangles on the image, marking the locations of the faces.

Example

Draw the Detection Results

for

(

x

,

y

,

w

,

h

)

in

faces:

    cv2.

rectangle

(

image

,

(

x

,

y

)

,

(

x+w

,

y+h

)

,

(

255

,

0

,

0

)

,

2

)

Display Results

Finally, use cv2.imshow() method displays the detection result.

Example

Display Results

cv2.

imshow

(

'Detected Faces'

,

image

)

cv2.

waitKey

(

0

)

cv2.

destroyAllWindows

(

)


Complete Code Example

The following is a complete OpenCV face detection code example:

Example

import

cv2

Load the Haar Feature Classifier

face_cascade

=

cv2.

CascadeClassifier

(

cv2.

data

.

haarcascades

'haarcascade_frontalface_default.xml'

)

Read an image

image

=

cv2.

imread

(

'image.jpg'

)

Convert to grayscale image

gray_image

=

cv2.

cvtColor

(

image

,

cv2.

COLOR_BGR2GRAY

)

Perform Face Detection

faces

=

face_cascade.

detectMultiScale

(

gray_image

,

scaleFactor

=

1.1

,

minNeighbors

=

5

,

minSize

=

(

30

,

30

)

)

Draw the Detection Results

for

(

x

,

y

,

w

,

h

)

in

faces:

    cv2.

rectangle

(

image

,

(

x

,

y

)

,

(

x+w

,

y+h

)

,

(

255

,

0

,

0

)

,

2

)

Display Results

cv2.

imshow

(

'Detected Faces'

,

image

)

cv2.

waitKey

(

0

)

cv2.

destroyAllWindows

(

)