Skip to content

Commit

Permalink
Fix nan-robust stream FPS
Browse files Browse the repository at this point in the history
Fix for Webcam stop working suddenly (Issue #6197)
  • Loading branch information
glenn-jocher committed Jan 5, 2022
1 parent b5b56a3 commit 25b0d13
Showing 1 changed file with 3 additions and 1 deletion.
4 changes: 3 additions & 1 deletion utils/datasets.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import glob
import hashlib
import json
import math
import os
import random
import shutil
Expand Down Expand Up @@ -308,8 +309,9 @@ def __init__(self, sources='streams.txt', img_size=640, stride=32, auto=True):
assert cap.isOpened(), f'{st}Failed to open {s}'
w = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
h = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
self.fps[i] = max(cap.get(cv2.CAP_PROP_FPS) % 100, 0) or 30.0 # 30 FPS fallback
fps = cap.get(cv2.CAP_PROP_FPS) # warning: may return 0 or nan
self.frames[i] = max(int(cap.get(cv2.CAP_PROP_FRAME_COUNT)), 0) or float('inf') # infinite stream fallback
self.fps[i] = max((fps if math.isfinite(fps) else 0) % 100, 0) or 30 # 30 FPS fallback

_, self.imgs[i] = cap.read() # guarantee first frame
self.threads[i] = Thread(target=self.update, args=([i, cap, s]), daemon=True)
Expand Down

0 comments on commit 25b0d13

Please sign in to comment.