Fix timing issue

This commit is contained in:
Ruben van de Ven 2024-01-23 16:18:01 +01:00
parent 7e3ba9acd2
commit ba4d2f7909

View file

@ -99,7 +99,7 @@ class FrameEmitter:
logger.info(f"Play from '{str(video_path)}'") logger.info(f"Play from '{str(video_path)}'")
video = cv2.VideoCapture(str(video_path)) video = cv2.VideoCapture(str(video_path))
fps = video.get(cv2.CAP_PROP_FPS) fps = video.get(cv2.CAP_PROP_FPS)
frame_duration = 1./fps target_frame_duration = 1./fps
logger.info(f"Emit frames at {fps} fps") logger.info(f"Emit frames at {fps} fps")
prev_time = time.time() prev_time = time.time()
@ -126,13 +126,13 @@ class FrameEmitter:
self.frame_sock.send(pickle.dumps(frame)) self.frame_sock.send(pickle.dumps(frame))
# defer next loop # defer next loop
new_frame_time = time.time() now = time.time()
time_diff = (new_frame_time - prev_time) time_diff = (now - prev_time)
if time_diff < frame_duration: if time_diff < target_frame_duration:
time.sleep(frame_duration - time_diff) time.sleep(target_frame_duration - time_diff)
new_frame_time += frame_duration - time_diff now += target_frame_duration - time_diff
else:
prev_time = new_frame_time prev_time = now
i += 1 i += 1