首页 > 解决方案 > OpenCV人脸识别太慢

问题描述

我尝试使用带有 face_recognition 的 OpenCV 来获得实时人脸识别,但它最终变得缓慢且滞后。请问有什么帮助吗?代码:

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)”不编码每一帧,跳过帧以减少延迟。谢谢!

标签: pythonopencvface-recognition

解决方案


推荐阅读