forked from dsutanto/zenai-kpc-python
Fix blnak live stream
This commit is contained in:
1 parent
d93ee99c1f
commit
6afffb2184
2 files changed
+11
-2
No files matched your search
@@ -56,6 +56,12 @@ def api_live_video():
|
||||
try:
|
||||
with open(LIVE_STREAM_FRAME_PATH, "rb") as f:
|
||||
jpeg = f.read()
|
||||
if not jpeg or len(jpeg) < 2 or jpeg[:2] != b"\xff\xd8":
|
||||
consecutive_fails += 1
|
||||
if consecutive_fails >= MAX_FAILS:
|
||||
return
|
||||
time.sleep(0.05)
|
||||
continue
|
||||
consecutive_fails = 0
|
||||
yield (b"--frame\r\n"
|
||||
b"Content-Type: image/jpeg\r\n\r\n" + jpeg + b"\r\n")
|
||||
|
||||
@@ -1384,11 +1384,14 @@ 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(
|
||||
ok, jpeg = cv2.imencode(
|
||||
".jpg", frame, [cv2.IMWRITE_JPEG_QUALITY, LIVE_STREAM_QUALITY]
|
||||
)
|
||||
with open(LIVE_STREAM_FRAME_PATH, "wb") as f:
|
||||
if ok:
|
||||
tmp_path = f"{LIVE_STREAM_FRAME_PATH}.tmp"
|
||||
with open(tmp_path, "wb") as f:
|
||||
f.write(jpeg.tobytes())
|
||||
os.replace(tmp_path, LIVE_STREAM_FRAME_PATH)
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
|
||||
Reference in new issue
Block a user