我尝试将OpenCV与face_recognition一起使用以获取实时人脸识别,但是结果却很慢且很慢。有什么帮助吗? 代码:
import face_recognition
import os
import cv2
from PIL import Image
appdata = os.getenv('APPDATA') + "/Project/"
face_cascade = cv2.CascadeClassifier((appdata + "data/face_cascade.xml").replace("\\", "/"))
cap = cv2.VideoCapture(0) #Capture video (OpenCV)
def Main():
while True: #OpenCV start video capture from webcam
ret, frame = cap.read()
gray_scale = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(gray_scale, 1.5, 5)
for x, y, w, h in faces:
#Start face recognition (face_recognition)
roi_color = frame[y: (y + h) - 15, x: (x + w) - 15] #This cuts the background leaving only the face
cv2.rectangle(frame, (x, y), ((x + w) - 15, (y + h) - 15), (255, 35, 36), 2)
face_encodings = face_recognition.face_encodings(roi_color) #This encodes face features and causes lag
cv2.imshow('sysPy', frame)
if cv2.waitKey(20) == 27:
break
cap.release()
cv2.destroyAllWindows()
Main()
我还考虑过“ face_encodings = face_recognition.face_encodings(roi_color)
”不对每个帧进行编码,而是跳过帧以减少延迟。谢谢!