Skip to content

Commit

Permalink
stream framerate fix
Browse files Browse the repository at this point in the history
  • Loading branch information
ben-milanko committed Apr 11, 2021
1 parent 1a74600 commit 3d57d56
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions utils/datasets.py
Original file line number Diff line number Diff line change
Expand Up @@ -284,10 +284,11 @@ def __init__(self, sources='streams.txt', img_size=640, stride=32):
assert cap.isOpened(), f'Failed to open {s}'
w = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
h = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
fps = cap.get(cv2.CAP_PROP_FPS) % 100
self.fps = cap.get(cv2.CAP_PROP_FPS) % 100

_, self.imgs[i] = cap.read() # guarantee first frame
thread = Thread(target=self.update, args=([i, cap]), daemon=True)
print(f' success ({w}x{h} at {fps:.2f} FPS).')
print(f' success ({w}x{h} at {self.fps:.2f} FPS).')
thread.start()
print('') # newline

Expand All @@ -306,9 +307,12 @@ def update(self, index, cap):
cap.grab()
if n == 4: # read every 4th frame
success, im = cap.retrieve()
if not success:
cap.release()
raise StopIteration # This does not correctly throw in a thread
self.imgs[index] = im if success else self.imgs[index] * 0
n = 0
time.sleep(0.01) # wait time
time.sleep(1/self.fps) # wait time

def __iter__(self):
self.count = -1
Expand Down

0 comments on commit 3d57d56

Please sign in to comment.