[PYTHON] Face detection using a cascade classifier

1. Background

Currently, I am writing a program that recognizes faces using deep learning. First, a large number of face images were prepared as learning data. At this time, the face was detected from the image showing the person, and only the face part was cut out. Of these, a cascade classifier was used as a method for detecting faces. This article describes a cascade classifier.

2. What is a cascade classifier?

A cascade classifier is a classifier composed of multiple simple classifiers. These simple classifiers are applied in sequence to the target image. It is detected after passing through all the classifiers. On the other hand, if it is rejected even once by a simple classifier, it will not be detected at that point. Therefore, images that clearly do not correspond to the detection target are rejected at the initial stage, which is fast. In OpenCV, a cascade classifier using HAAR-Like features is prepared.

OpenCV cascade file https://github.com/opencv/opencv/tree/master/data/haarcascades

The HAAR-Like feature is a feature based on the brightness and darkness of the image. It is characterized by the difference between the white area and the black area in the local area of the image. Therefore, it is common to convert the image to grayscale before using it.

3. Cascade classifier in OpenCV

In OpenCV, after reading the cascade file containing the cascade classifier, it is detected by detect.MultiScake (). The return value of detect.MultiScake () is an N × 4 matrix, where N is the detected number and 4 is the 4 elements of [x, y, width, height]. These four elements are the areas where the detected area is surrounded by a rectangle. x is the upper left x coordinate of the rectangle, y is the upper left y coordinate of the rectangle, width is the width of the rectangle, and height is the height of the rectangle. Therefore, if you want to mark the detected part, use the OpenCV drawing function. You can write rectangle (image, (x, y), (x + width, y + height), (255,0,0), 2) etc. using rectangle (). Try it on the image below. PB1-3.jpg

The source code is shown below.

face_detect.py


import cv2

#Load image
image = cv2.imread('sample.jpg')

#Convert image to grayscale
image_gray = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)

#Read a classifier from a file
cascade = cv2.CascadeClassifier("/opencv/data/haarcascades/haarcascade_frontalface_alt.xml")

#Detect using a classifier
faces = cascade.detectMultiScale(image_gray, scaleFactor=1.1, minNeighbors=1, minSize=(1, 1))

#detectMultiScale()Output the return value of(For confirmation)
print(faces)

#Mark the part of the detected face image with a rectangle
for (x,y,width,height) in faces:
    cv2.rectangle(image,(x,y),(x+width,y+height),(255,0,0),2)

#Display image
cv2.imshow('result',image)
cv2.waitKey(0)
cv2.destroyAllWindows()

The execution result is as follows. There is only one misrecognition, but all faces can be detected. nogizaka.jpg By the way, since the return value of detectMultiScale () was output, if you look at the output result ... image.png As shown above, the information of the upper left coordinates, width, and height in the detected area (rectangle) is returned.

4. Digression

In the previous article, I learned how to cut out only a part of an image using the array returned by imread.

[OpenCV] About the array returned by imread https://qiita.com/Castiel/items/53ecbee3c06b9d92759e

And this time, I learned how to detect faces. If you use these, you should be able to collect learning data of facial images in deep learning. Next time, I will work on creating learning data for facial images.

Recommended Posts

Face detection using a cascade classifier
Make a face recognizer using TensorFlow
I made a Dir en gray face classifier using TensorFlow --(1) Introduction
I made a Dir en gray face classifier using TensorFlow-④ Face extraction
Face recognition using OpenCV (Haar-like feature classifier)
[Python] Face detection by OpenCV (Haar Cascade)
Make a cascade classifier with google colaboratory
I made a Dir en gray face classifier using TensorFlow --⑩ Face classification test
I made a Dir en gray face classifier using TensorFlow --⑬ Playing (final)
I made a Dir en gray face classifier using TensorFlow --- ⑧ Learning execution
I made a Dir en gray face classifier using TensorFlow --⑫ Web release
I made a Dir en gray face classifier using TensorFlow --- ⑦ Learning model
I made a Dir en gray face classifier using TensorFlow --② Environment construction
I made a Dir en gray face classifier using TensorFlow --⑪ Web release preparation
Similar face image detection using face recognition and PCA and K-means clustering
Feature detection using opencv (corner detection)
I tried face recognition using Face ++
[PyTorch Tutorial ④] TRAINING A CLASSIFIER
Face detection with Python + dlib
Using a printer with Debian 10
Face detection with Python + OpenCV
Face detection summary in Python
Face detection with Haar Cascades
Outline the face using Dlib (1)
Cascade classifier creation with opencv
A memorandum of using eigen3
Anime face detection with OpenCV
I tried to implement anomaly detection using a hidden Markov model
I tried scoring a transvestite contest using Face ++'s Detect API