[英]Python/OpenCV - Wrong actual camera FPS or too slow routine
我正在使用 python 和 OpenCV 從網絡攝像頭獲取視頻。 為了錄制視頻,我需要 FPS 信息。 從cv.VideoCapture
的get
屬性中,我得到 30fps:
fps = cap.get(cv.CAP_PROP_FPS)
但是,當我構建讀取后續幀的線程時,實際幀速率似乎要低得多(~12):
def run(self) -> None:
log(f'Started frame grabber with FPS = {self.fps}')
while self.keep_grabbing:
_now = time.perf_counter()
# frame = self.cap.grab() # SAME RESULT AS WITH read()
ret, frame = self.cap.read()
now = time.perf_counter()
log(f'Elapsed Time: {now - _now}')
結果是平均經過 0.083 秒(大約 12 fps) 我不明白我是否正在獲得一個緩慢的軟件(但我無法弄清楚要更改什么),或者 get 屬性是否返回了錯誤的 FPS。 有什么建議嗎?
問題是打開相機的ApiPreference
。 默認情況下,使用cv.CAP_ANY
暗示問題描述的行為。 API 的自動檢測似乎無法有效工作
特別是對於 Ubuntu 20.04 下的 PC 網絡攝像頭,參數cv.CAP_V4L
使攝像頭以 29.8 的有效 FPS 工作,非常接近理論值 (30)。
代碼變為:
import cv2 as cv
import time
# Here the ApiPreference must be chosen carefully
cap = cv.VideoCapture(0, cv.CAP_V4L)
if not cap.isOpened():
exit('Camera not opened')
while True:
_now = time.perf_counter()
ret, frame = cap.read()
now = time.perf_counter()
print(f'Elapsed Time: {now - _now}')
if not ret:
exit('Frame not grabbed')
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.