glenn-jocher commited on
Commit
9e9219f
1 Parent(s): b5b56a3

Fix `nan`-robust stream FPS (#6198)

Browse files

Fix for Webcam stop working suddenly (Issue #6197)

Files changed (1) hide show
  1. utils/datasets.py +3 -1
utils/datasets.py CHANGED
@@ -6,6 +6,7 @@ Dataloaders and dataset utils
6
  import glob
7
  import hashlib
8
  import json
 
9
  import os
10
  import random
11
  import shutil
@@ -308,8 +309,9 @@ class LoadStreams:
308
  assert cap.isOpened(), f'{st}Failed to open {s}'
309
  w = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
310
  h = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
311
- self.fps[i] = max(cap.get(cv2.CAP_PROP_FPS) % 100, 0) or 30.0 # 30 FPS fallback
312
  self.frames[i] = max(int(cap.get(cv2.CAP_PROP_FRAME_COUNT)), 0) or float('inf') # infinite stream fallback
 
313
 
314
  _, self.imgs[i] = cap.read() # guarantee first frame
315
  self.threads[i] = Thread(target=self.update, args=([i, cap, s]), daemon=True)
 
6
  import glob
7
  import hashlib
8
  import json
9
+ import math
10
  import os
11
  import random
12
  import shutil
 
309
  assert cap.isOpened(), f'{st}Failed to open {s}'
310
  w = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
311
  h = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
312
+ fps = cap.get(cv2.CAP_PROP_FPS) # warning: may return 0 or nan
313
  self.frames[i] = max(int(cap.get(cv2.CAP_PROP_FRAME_COUNT)), 0) or float('inf') # infinite stream fallback
314
+ self.fps[i] = max((fps if math.isfinite(fps) else 0) % 100, 0) or 30 # 30 FPS fallback
315
 
316
  _, self.imgs[i] = cap.read() # guarantee first frame
317
  self.threads[i] = Thread(target=self.update, args=([i, cap, s]), daemon=True)