概述
[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\venvLibsite-packagescv2data\haarcascade_frontalface_alt.xml"
xml_path[1] = "D:pythonProject_face\venvLibsite-packagescv2data\haarcascade_frontalface_default.xml"
xml_path[2] = "D:pythonProject_face\venvLibsite-packagescv2data\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]基于openCV的实时人脸检测系统V1的全部内容,希望文章能够帮你解决[openCV/Python]基于openCV的实时人脸检测系统V1所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复