[openCV/Python]基于openCV的实时人脸检测系统V1

 [openCV/Python]基于openCV的实时人脸检测系统V1

import cv2 as cv
import os

from rename import *


def getFileList(dir, Filelist, ext=None):
    """
    获取文件夹及其子文件夹中文件列表
    输入 dir:文件夹根目录
    输入 ext: 扩展名
    返回: 文件路径列表
    """
    newDir = dir
    if os.path.isfile(dir):
        if ext is None:
            Filelist.append(dir)
        else:
            if ext in dir[-3:]:
                Filelist.append(dir)

    elif os.path.isdir(dir):
        for s in os.listdir(dir):
            newDir = os.path.join(dir, s)
            getFileList(newDir, Filelist, ext)

    return Filelist


def preprocess(img):
    img = cv.resize(img, (500, 500))
    img=cv.flip(img,1)
    return img


def camera_face_detect():
    camera = cv.VideoCapture(0)
    xml_path = {}
    xml_path[0] = "D:\pythonProject_face\\venv\Lib\site-packages\cv2\data\\haarcascade_frontalface_alt.xml"
    xml_path[1] = "D:\pythonProject_face\\venv\Lib\site-packages\cv2\data\\haarcascade_frontalface_default.xml"
    xml_path[2] = "D:\pythonProject_face\\venv\Lib\site-packages\cv2\data\\haarcascade_frontalface_alt2.xml"
    previous_face_num = 0
    while camera.isOpened():
        face_num = 0

        ret, img = camera.read()
        if ret is True:
            img = preprocess(img)
            cv.imshow("Window", img)

            x_list = {}
            y_list = {}
            w_list = {}
            h_list = {}

            gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
            for xml_num in range(len(xml_path)):
                xml = xml_path[xml_num]
                faceCascade = cv.CascadeClassifier(xml)
                faceRect = faceCascade.detectMultiScale(gray, 1.07, 5)
                for (x, y, w, h) in faceRect:
                    flag_same = 0
                    for i in range(len(x_list)):  # 高IOU则判定为重合,不予录入
                        IOU = (min(x_list[i] + w_list[i], x + w) - max(x_list[i], x)) * (
                                min(y_list[i] + h_list[i], y + h) - max(y_list[i], y)) / (
                                      max(x_list[i] + w_list[i], x + w) - min(x_list[i], x)) * (
                                      max(y_list[i] + h_list[i], y + h) - min(y_list[i], y))
                        if IOU > 0.8:
                            flag_same = 1
                    if flag_same == 0:
                        face_num += 1
                        x_list[face_num - 1] = x
                        y_list[face_num - 1] = y
                        w_list[face_num - 1] = w
                        h_list[face_num - 1] = h
                        cv.imshow("Window", cv.rectangle(img, (x, y), (x + w, y + h), (0, 0, 255), 2))
                        cv.imshow("Window",
                                  cv.putText(img, "Face" + str(face_num), (x, y - 5), cv.FONT_HERSHEY_SIMPLEX, 1,
                                             (0, 0, 255), 2))

            if face_num != previous_face_num:
                print("\n发现" + str(face_num) + "张人脸")
            previous_face_num = face_num

            if cv.waitKey(1)&0xFF==27:
                break
        else:
            break

你可能感兴趣的:(openCV,Python,python,opencv)