fix : live view buffering

This commit is contained in:
Alberto-Audrix committed 2026-07-10 13:54:36 +07:00
1 parent af2854d771
commit 314b598c66
4 files changed
+73 -24

No files matched your search

+13 -3
View File
@@ -1002,6 +1002,18 @@ def draw_popups(img, popups, frame_idx):
return alive
def _write_live_frame_atomic(path, jpeg_bytes):
"""Write JPEG atomically so dashboard readers never see a partial file."""
dest = Path(path)
dest.parent.mkdir(parents=True, exist_ok=True)
tmp = dest.with_suffix(dest.suffix + ".tmp")
with open(tmp, "wb") as f:
f.write(jpeg_bytes)
f.flush()
os.fsync(f.fileno())
os.replace(tmp, dest)
def connect_stream(source, warmup=WARMUP_FRAMES):
attempts = 0
while not shutdown_requested:
@@ -1381,12 +1393,10 @@ def run():
if LIVE_STREAM_ENABLED and frame_idx % LIVE_STREAM_EVERY_N == 0:
try:
Path(LIVE_STREAM_FRAME_PATH).parent.mkdir(parents=True, exist_ok=True)
_, jpeg = cv2.imencode(
".jpg", frame, [cv2.IMWRITE_JPEG_QUALITY, LIVE_STREAM_QUALITY]
)
with open(LIVE_STREAM_FRAME_PATH, "wb") as f:
f.write(jpeg.tobytes())
_write_live_frame_atomic(LIVE_STREAM_FRAME_PATH, jpeg.tobytes())
except Exception:
pass