Skip to content

Commit

Permalink
fix precise fps
Browse files Browse the repository at this point in the history
  • Loading branch information
ptg committed Oct 9, 2024
1 parent f879a57 commit 0d7406b
Showing 1 changed file with 7 additions and 1 deletion.
8 changes: 7 additions & 1 deletion ptgctl/tools/mock.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ async def _video_feed(src, fps=None, shape=None):
if not cap.isOpened():
raise ValueError(f"{cap}")
i = 0
t0 = time.time()
lag = 0
while True:
ret, im = cap.read()
if not ret:
Expand All @@ -100,7 +102,11 @@ async def _video_feed(src, fps=None, shape=None):
yield im
pbar.update()
if fps:
await asyncio.sleep(1/fps)
t = time.time()
dt = 1/fps - (t-t0) - lag
await asyncio.sleep(max(0, dt))
lag = max(-dt, 0)
t0 = t + max(0, dt)


@util.async2sync
Expand Down

0 comments on commit 0d7406b

Please sign in to comment.