opencv learning_four ---- 人脸识别

import cv2 as cv
import numpy as np


def face_detect(img):
    face_cascade = cv.CascadeClassifier(
        r'D:/Anaconda/envs/tf2/Library/etc/haarcascades/haarcascade_frontalface_alt.xml')
    eye_cascade = cv.CascadeClassifier(
        r'D:/Anaconda/envs/tf2/Library/etc/haarcascades/haarcascade_eye.xml')

    gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)

    faces = face_cascade.detectMultiScale(gray, 1.3, 5)

    for (x, y, w, h) in faces:
        img = cv.rectangle(img, (x, y), (x + w, y + h), (255, 0, 0), 2)
        # 眼睛检测
        roi_gray = gray[y:y + h, x:x + w]
        eyes = eye_cascade.detectMultiScale(roi_gray, 1.03, 5, 0, (30, 30))
        for (ex, ey, ew, eh) in eyes:
            cv.rectangle(img, (ex + x, ey + y), (x + ex + ew, y + ey + eh), (0, 255, 0), 2)

    return img

值得注意的是:xml文件必须使用绝对路径,这里是结合cameo,所以直接修改每一帧的图像。

上一篇:图像边缘检测,sobel,scharr


下一篇:min_max_gray 算子