opencv learning twenty-three: face detection

Face Detection

OpenCV comes with a function detectMultiScale() that can detect pedestrians and faces. It is simple to implement, but the recognition effect is relatively poor.

Opencv's face detection function defines the data file of the specific trackable object type.

The Haar cascade classifier judges whether a given image or sub-image matches a known object by comparing and analyzing adjacent image regions.

The similarity of two images can be measured by the Euclidean distance of their corresponding features. The distance may be defined in space coordinates or color coordinates. The Haar-like feature is a feature for real-time face tracking. Each Haar-like feature describes the contrast mode of adjacent image regions. For example, edges, vertices, and thin lines can all generate judging features.

The Haar cascade is scale-invariant, in other words, it is robust to scale changes. However, the Haar cascade of opencv does not have rotation invariance.

In the copy of the opencv source code, there will be a folder data/haarcascades, which contains all opencv face detection xml files, which can be used to detect faces in still images, videos and images obtained by cameras.
Before you start writing code, you must first download the opencv source code package:
opencv official website (I downloaded 3.4)
Insert picture description here

1. Face detection in static images:

import cv2 as cv
import numpy as np

def face_detect_demo():
    gray = cv.cvtColor(src, cv.COLOR_BGR2GRAY)
    face_detect = cv.CascadeClassifier("D:/opencv3.4.13/opencv/build/etc/haarcascades/haarcascade_frontalface_alt_tree.xml")
    faces = face_detect.detectMultiScale(gray, 1.02, 2)
    for x, y, w, h in faces:
        cv.rectangle(src, (x, y), (x+w, y+h), (0, 0, 255), 2)
    cv.imshow("face_detect", src)

src = cv.imread("C:/Users/lenovo/Desktop/opencv/daima/banknum/template-matching-ocr/images/hezhao1.jpg")  #读取图片位置
cv.namedWindow("input image", cv.WINDOW_AUTOSIZE)
cv.imshow("input image", src)
face_detect_demo()
cv.waitKey(0)
cv.destroyAllWindows()

Run screenshot:
Insert picture description hereSometimes some faces cannot be detected:
modify the parameter values ​​in face_cascade.detectMultiScale(gray, scaleFactor=1.2, minNeighbors=1) to get different recognition accuracy, some recognize more people, some less.

Insert picture description here

2. Face detection in video

import cv2 as cv
import numpy as np

def face_rec():
    # 加载视频
    cameraCapture = cv.VideoCapture('C:/Users/lenovo/Desktop/opencv/daima/banknum/template-matching-ocr/images/xiaoyu3.mp4')
    # cv2级联分类器CascadeClassifier,xml文件为训练数据
    face_cascade = cv.CascadeClassifier("D:/opencv3.4.13/opencv/build/etc/haarcascades/haarcascade_frontalface_default.xml")
    # 读取数据
    success, frame = cameraCapture.read()
    while success and cv.waitKey(1) == -1:
        # 读取数据
        ret, img = cameraCapture.read()
        print(img.shape)
        # 进行人脸检测
        faces = face_cascade.detectMultiScale(img, 1.3, 5)
        # 绘制矩形框
        for (x, y, w, h) in faces:
            img = cv.rectangle(img, (x, y), (x+w, y+h), (255, 0, 0), 2)
        # 设置显示窗口
        cv.namedWindow('camera', 0)
        cv.resizeWindow('camera', 360, 640)
        # 显示处理后的视频
        cv.imshow('camera', img)
        # 读取数据
        success, frame = cameraCapture.read()
        c = cv.waitKey(50)
        if c == 27:
            break
    # 释放视频
    cameraCapture.release()
    # 释放所有窗口
    cv.destroyAllWindows()


if __name__ == '__main__':
    face_rec()

Running results:
python3.7+opencv3 face detection example-1
python3.7+opencv3 face detection example-2
python3.7+opencv3 face detection example-3

Guess you like

Origin blog.csdn.net/weixin_44145452/article/details/112847758