python - 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)
”不编码每一帧,跳过帧以减少延迟。谢谢!
解决方案
推荐阅读
- python - 如何在列表理解中获取列表的长度
- javascript - React 和 Javascript 通过与之前的状态进行比较从状态中获取更新/编辑的值
- c# - 尝试激活“X.Controllers.Events.JsonSerializer”时无法解析“Newtonsoft.Json.JsonSerializerSettings”类型的服务
- flutter - 如何在颤动中发送文件
- flutter - Webview Flutter 无法更改 URL
- c# - 如果 PageSize 为 0,Linq 获取所有记录
- reactjs - React JS - 运行 npm add gh-pages 命令时出现警告
- c# - WPF 编程,如何将事件移动到另一个类(外部)
- lua - 用于 lua 表的类似 JS 对象的函数
- javascript - Get changes based on uploaded time from firebase