[英]How to capture video and save from webcam on click or pressing any key from keyboard using OpenCV
[英]how to speed up video capture from webcam in opencv?
我需要实时视频捕获我已经尝试降低分辨率,设置 static fps 并且没有工作为什么我得到一个缓慢的视频输入虽然它说我的 fps 是 30 我真的不知道问题到底出在哪里它真的让我潜水疯狂的。
import cv2
import os
import face_recognition
import pickle
from cv2.cv2 import CAP_DSHOW
known_faces_dir = "known_faces"
video = cv2.VideoCapture(0)
accuracy = 0.6
frame_thikness = 3
font_size = 2
MODEL = "cnn"
print("loading known faces")
known_faces = []
known_names = []
unknown_faces = []
for name in os.listdir(known_faces_dir):
for filename in os.listdir(f"{known_faces_dir}/{name}"):
image = face_recognition.load_image_file(f"{known_faces_dir}/{name}/{filename}")
encodings = face_recognition.face_encodings(image)[0]
# encodings = pickle.load(open(f"{name}/{filename}","rb"))
known_faces.append(encodings)
known_names.append(name)
print("treating unknow faces")
while True :
# print(filename)
# image = face_recognition.load_image_file(f"{unknown_faces_dir}/{filename}")
ret, image = video.read()
print(video.get(cv2.CAP_PROP_FPS))
locations = face_recognition.face_locations(image, model=MODEL)
encodings = face_recognition.face_encodings(image, locations)
# image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
for face_location, face_encodings in zip(locations, encodings):
results = face_recognition.compare_faces(known_faces, face_encodings, tolerance=0.54)
if True in results:
match = known_names[results.index(True)]
print("Face Found" f"{match}")
top_l = (face_location[3], face_location[0])
bottom_r = (face_location[1], face_location[2])
color = [0, 255, 0]
cv2.rectangle(image, top_l, bottom_r, color, frame_thikness)
top_l = (face_location[3], face_location[2])
bottom_r = (face_location[1], face_location[2] + 22)
cv2.rectangle(image, top_l, bottom_r, color, cv2.FILLED)
cv2.putText(image, str(match), (face_location[3]+10, face_location[2]+15), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (200, 200, 200), 2)
cv2.imshow("", image)
if cv2.waitKey(1)&0xFF == ord("e"):
break
# cv2.waitKey(10200)
video.release()
cv2.destroyWindow(filename)
试试这个并查看计算 fps 的经过时间。 然后添加其他进程。
从一开始就使用灰色图像是个好主意。 如果面部图像是彩色的,则将其保存为灰色并仅使用灰色图像。 如果没有必要,避免该过程重复相同的事情。
import numpy as np
import cv2
import time
cap = cv2.VideoCapture(0)
start_time = time.time()
end_time = start_time
elapsed_time = 0
font = cv2.FONT_HERSHEY_SIMPLEX
org = (50, 50)
fontScale = 1
color = (255, 0, 0)
thickness = 2
while(True):
start_time = time.time()
# Capture frame-by-frame
ret, frame = cap.read()
# Our operations on the frame come here
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
# Display the resulting frame
cv2.putText(gray, str(1 / elapsed_time) + "fps", org, font,
fontScale, color, thickness, cv2.LINE_AA)
cv2.imshow('frame',gray)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
end_time = time.time()
elapsed_time = end_time - start_time
# When everything done, release the capture
cap.release()
cv2.destroyAllWindows()
您的 model 的推理很可能需要超过 33 毫秒(1000 毫秒/30 FPS),从而限制了您的 FPS。 尝试从循环中删除您的面部识别 model ,看看它是否仍然很慢。
如果这样可以解决您的问题,您的 CPU 或 GPU 是限制因素,具体取决于您如何运行 model。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.