018 OpenCV face detection

Table of contents

1. Environment

2. Classifier principle

2.1. Overview

2.2. Working principle

3. Face detection code


1. Environment

The usage environment of this article is:

  • Windows10
  • Python 3.9.17
  • opencv-python 4.8.0.74

2. Classifier principle

CascadeClassifier is a powerful class in OpenCV (open source computer vision library) for implementing cascade classifiers. This is a machine learning technology that is widely used in computer vision tasks such as face detection and object recognition. The following is an in-depth analysis of CascadeClassifier.

2.1. Overview

In computer vision, object detection is an important task, whose goal is to find objects of interest in images or videos. For face detection, we need to find and locate all faces in the image. To accomplish this task, we need a powerful classifier. However, building a powerful and efficient classifier is a challenge because we need to process a large amount of data and accurately detect faces under various conditions (different lighting, poses, expressions, etc.).

This is where CascadeClassifier comes into play. CascadeClassifier is a cascade classifier that combines multiple "weak" classifiers to create a strong classifier. Each weak classifier is based on Haar features or LBP (local binary pattern) features. These features are simple patterns in images that can be used to describe the structure of the image. By combining these weak classifiers, we can get a strong classifier that can accurately detect faces under various conditions.

2.2. Working principle

The working principle of CascadeClassifier can be divided into two stages: training and detection.

  1. Training phase: In this phase, we need to provide a large number of positive samples (images containing the target) and negative samples (images not containing the target). Then, CascadeClassifier uses the AdaBoost algorithm to train the classifier. The AdaBoost algorithm optimizes the performance of the classifier by iteratively increasing the weight of misclassified samples. This process will produce a series of weak classifiers, each of which has a good classification effect on a part of the samples. These weak classifiers are then combined into a strong classifier.
  2. Detection phase: In the detection phase, CascadeClassifier uses a sliding window method to scan the image. For each window, the classifier calculates a score that represents the likelihood that the window contains the target. This score is then compared to a threshold, and if the score is above the threshold, the window is considered to contain the target. This process is repeated at different scales and positions to detect objects of different sizes and positions.

3. Face detection code

The code requires one picture and two xml files. I have provided the files.

Link: https://pan.baidu.com/s/1cvPvhhuYD_KXHVVaG9dEng?pwd=1234 
Extraction code: 1234

In the following code, an image is read first, and then two xml files (model files) are read. Then the face is detected on the original image, and the face area image is used to detect the eyes.

from __future__ import print_function
import cv2 as cv
import argparse

# 可视化
def detectAndDisplay(frame):
    # 彩色图转灰度图
    frame_gray = cv.cvtColor(frame, cv.COLOR_BGR2GRAY)
    # 使用直方图均衡化算法处理灰度图,防止图像太亮或者太暗
    frame_gray = cv.equalizeHist(frame_gray)

    # 人脸检测
    faces = face_cascade.detectMultiScale(frame_gray)
    for (x,y,w,h) in faces:
        center = (x + w//2, y + h//2)
        # 将人脸用椭圆标注出来
        frame = cv.ellipse(frame, center, (w//2, h//2), 0, 0, 360, (255, 0, 255), 4)
        # 取出人脸roi小图
        faceROI = frame_gray[y:y+h,x:x+w]
        # 在人脸roi小图上识别眼睛
        eyes = eyes_cascade.detectMultiScale(faceROI)
        for (x2,y2,w2,h2) in eyes:
            eye_center = (x + x2 + w2//2, y + y2 + h2//2) # 眼睛中心
            radius = int(round((w2 + h2)*0.25)) # 圆半径
            frame = cv.circle(frame, eye_center, radius, (255, 0, 0 ), 4) # 使用圆将眼睛圈出来

    cv.imshow('Capture - Face detection', frame)

parser = argparse.ArgumentParser(description='Code for Cascade Classifier tutorial.')
# 人脸模型路径
parser.add_argument('--face_cascade', help='Path to face cascade.', default='data/haarcascades/haarcascade_frontalface_alt.xml')
# 眼睛模型路径
parser.add_argument('--eyes_cascade', help='Path to eyes cascade.', default='data/haarcascades/haarcascade_eye_tree_eyeglasses.xml')
parser.add_argument('--image', help='image path', type=str, default='data/6.jpg') 
args = parser.parse_args()

face_cascade_name = args.face_cascade
eyes_cascade_name = args.eyes_cascade
# 创建一个级联分类器对象(人脸)
face_cascade = cv.CascadeClassifier()
# 创建一个级联分类器对象(眼睛)
eyes_cascade = cv.CascadeClassifier()

#加载级联分类器参数文件(人脸)
if not face_cascade.load(cv.samples.findFile(face_cascade_name)):
    print('--(!)Error loading face cascade')
    exit(0)
#加载级联分类器参数文件(眼睛)
if not eyes_cascade.load(cv.samples.findFile(eyes_cascade_name)):
    print('--(!)Error loading eyes cascade')
    exit(0)

img_path = args.image
#头读取图像
frame = cv.imread(img_path)
detectAndDisplay(frame)
cv.waitKey(0)

Guess you like

Origin blog.csdn.net/m0_72734364/article/details/134869173