Opencv face detection (Haar feature + cascade classification)

Haar feature + Adaboost cascade classifier

The object detection technology based on the Haar feature classifier is a very effective object detection technology, which is mostly used for face detection and pedestrian detection. Haar-like feature is a commonly used feature description operator in the field of computer vision, also known as Haar feature. The Haar feature is to subtract the sum of all the pixel values ​​of the white rectangle from the sum of all the pixel values ​​of the black rectangle in the figure below.

Insert picture description here

Haar features can generate a series of sub-features by zooming in and shifting in the detection window. In the paper "Rapid Object Detection using a Boosted Cascade of Simple Features", the detector used by the author was set to a size of 24*24, and more than 180,000 features were obtained.

Insert picture description here

The Haar feature value reflects the gray level changes of the image. Experiments have proved that a very small number of features can be combined to form an effective classifier. For example, some features of the face can be simply described by rectangular features, such as: the eyes are darker than the cheeks, the sides of the nose are darker than the bridges of the nose, and the mouth is darker than the surroundings. To achieve this goal, we apply each feature to all training images (positive sample face images, negative sample non-face images). We want to select the features with the lowest error rate, which means that they are the best features for detecting facial and non-face images. The author finally selected 6061 features from 180,000 features.

In order to find targets in different positions in the image, the detection window needs to be moved successively (the Haar feature in the window also moves accordingly), so that it can traverse to every position in the image. In order to detect targets of different sizes, there are generally two methods: gradually reduce the image or gradually enlarge the detection window, so that you can traverse to the targets of different sizes in the image.

If you use these 6061 features to check each 24x24 window to see if it is a face, it will be time-consuming, so it is better to have a simple method to prove that this window is not a face area, if it is not, just discard it. Deal with it again. In order to achieve this goal, the author proposed the concept of cascaded classifiers.

Instead of testing the 6061 features of the window at the beginning, divide these features into different groups. Use one by one in different classification stages. Usually the first few stages use less feature detection. The study found that the correct rate of the classifier's judgment is not a human face is very high, and the correct rate of the judgment is a human face is average. If a window fails the first stage of the test, you can simply abandon the following test, and if it passes, it will enter the second stage of testing. If a window has passed all the tests, then this window is considered a face area.

The author divides 6061 features into 38 stages, and the number of features in the first five stages is 1, 10, 25, 25, and 50, respectively. The number of features contained in the remaining layers has increased dramatically. The total number of features is 6061.

import os
os.chdir("E:\\code\opencv\\10.人脸检测识别\\")
from imutils import *
image = imread('face.png')
show(image)

Insert picture description here

  • Parameter description of detector.detectMultiScale function
  1. image: input image
  2. scaleFactor=1.1: This is the ratio of each time the image is reduced, the default is 1.1
  3. minNeighbors=3: The number of surrounding rectangular boxes required for successful matching. The area matched by each feature is a rectangular box. Only when multiple rectangular boxes exist at the same time, the matching is considered successful, such as a human face. This default The value is 3.
  4. minSize: the minimum range of matching faces
  5. flags=0: can take the following values:
    CASCADE_DO_CANNY_PRUNING=1, use canny edge detection to exclude some image areas with few or many edges
    CASCADE_SCALE_IMAGE=2, normal ratio detection
    CASCADE_FIND_BIGGEST_OBJECT=4, only detect the largest object
    CASCADE_DO_ROUGH_SEARCH=8 Detection
# 级联分类器
detector = cv2.CascadeClassifier('haarcascade_frontalface_alt.xml')
rects = detector.detectMultiScale(image, scaleFactor=1.1, minNeighbors=2, minSize=(10,10), flags=cv2.CASCADE_SCALE_IMAGE)

for (x,y,w,h) in rects:
    # 画矩形框
    cv2.rectangle(image, (x,y), (x+w,y+h), (0,255,0), 2)
    
show(image)

Insert picture description here

def facedetect(image):
    image = imread(image)
    # 级联分类器
    detector = cv2.CascadeClassifier('haarcascade_frontalface_alt.xml')
    rects = detector.detectMultiScale(image, scaleFactor=1.1, minNeighbors=2, minSize=(10,10), flags=cv2.CASCADE_SCALE_IMAGE)

    for (x,y,w,h) in rects:
        # 画矩形框
        cv2.rectangle(image, (x,y), (x+w,y+h), (0,255,0), 2)

    show(image)
facedetect('Solvay.jpg')

Insert picture description here

Guess you like

Origin blog.csdn.net/cyj5201314/article/details/115218577