【问题标题】:how to speed up video capture from webcam in opencv?如何加快opencv中网络摄像头的视频捕获速度?
【发布时间】:2021-06-30 21:51:45
【问题描述】:

我需要实时视频捕获我已经尝试降低分辨率,设置静态 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)

【问题讨论】:

  • 我认为问题在于 cap.read() 之后的过程是巨大的。您的网络摄像头 30 fps 是网络摄像头的能力。
  • “视频”的定义在哪里?
  • @MarioAbbruscato 不好意思,我加了它

标签: python opencv face-recognition


【解决方案1】:

试试这个并查看计算 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()

【讨论】:

    【解决方案2】:

    您的模型推理很可能需要超过 33 毫秒(1000 毫秒/30 FPS),从而限制了您的 FPS。尝试从循环中删除您的人脸识别模型,看看它是否仍然很慢。

    如果这样可以解决您的问题,那么您的 CPU 或 GPU 是限制因素,具体取决于您运行模型的方式。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-02-06
      • 1970-01-01
      • 2015-08-18
      • 2014-06-05
      • 1970-01-01
      • 2022-06-30
      相关资源
      最近更新 更多