我正在尝试构建一个应满足这些要求的实时人脸检测器:
这些要求促使我构建了一个脚本,用于从网络摄像头捕获帧,并使用 MTCNN 模型分析这些帧并最终给出输出。即使在较暗的环境中,该解决方案也使我能够获得不错的准确度,并在大约 3.6 秒内检测到面部(一次分析 3 帧,这在准确度和速度之间似乎很划算)。这是我实际使用的代码:
import cv2
from time import sleep
from deepface import DeepFace
import os
import datetime
frames_analyzed = 3
backends = [
'opencv',
'ssd',
'dlib',
'mtcnn',
'retinaface',
'mediapipe',
'yolov8',
'yunet',
]
while True:
i = 0
start_time = datetime.datetime.now()
while i < frames_analyzed:
vidcap = cv2.VideoCapture(0)
if vidcap.isOpened():
ret, frame = vidcap.read()
if ret:
cv2.imwrite("Frames/"+str(i)+".jpg",frame)
else:
print("Error : Failed to capture frame")
else:
print("Cannot open camera")
vidcap.release()
i = i+1
face_detected = 0
for i in range(frames_analyzed-1):
try:
DeepFace.extract_faces(img_path = "Frames/"+str(i)+".jpg", detector_backend = backends[3])
face_detected = face_detected+1
except:
continue
os.remove("Frames/"+str(i)+".jpg")
end_time = datetime.datetime.now()
print(face_detected, (end_time-start_time).total_seconds())
这些是我的问题: