Add object detection, tracking, mobile notifications, and search to any security camera.
Recover2 (#77)
Rory Clear211b49b35b49
diff --git a/clearcam.py b/clearcam.pyindex d7c6277..9ca901f 100644--- a/clearcam.py+++ b/clearcam.py@@ -686,6 +686,7 @@ class HLSStreamer:"-use_wallclock_as_timestamps", "1","-fflags", "+genpts","-i", "-",+ "-loglevel", "quiet","-c:v", "libx264","-pix_fmt", "yuv420p","-crf", "21",@@ -724,6 +725,7 @@ class HLSStreamer:"-f", "concat","-safe", "0","-i", str(concat_list_path),+ "-loglevel", "quiet","-vf", "scale=-2:240,fps=24","-c:v", "libx264","-preset", "veryslow",@@ -757,78 +759,47 @@ class HLSStreamer:def _feed_frames(self):last_frame_time = time.time()- stall_timeout = max(5, int(self.segment_time * 2))- retry_delay = 5.0+ stall_timeout = 10+while self.running:frame = self.cam.get_frame()if frame is None:if time.time() - last_frame_time > stall_timeout:- print("stopping ffmpeg and waiting for camera...")- try:- self.stop()- except Exception as e:- print("raised while recovering:", e)- while True:- recovered_frame = self.cam.get_frame()- if recovered_frame is not None:- while True:- try:- self.start()- return- except Exception as e:- print("Failed to restart HLS pipeline", e)- time.sleep(retry_delay)- time.sleep(0.5)- time.sleep(0.5)+ print("Camera feed stalled, attempting recovery...")+ self._safe_restart()+ time.sleep(0.1)continuelast_frame_time = time.time()try:- if self.ffmpeg_proc is None or self.ffmpeg_proc.stdin is None:- raise BrokenPipeError("ffmpeg process missing/stdin closed")+ if self.ffmpeg_proc is None or self.ffmpeg_proc.poll() is not None:+ raise BrokenPipeError("FFmpeg process not running")self.ffmpeg_proc.stdin.write(frame.tobytes())+ self.ffmpeg_proc.stdin.flush()+except (BrokenPipeError, OSError, ValueError) as e:- print("[WARN] FFmpeg write failed:", e)- try:- self.stop()- except Exception as e2:- print(e2)-- while True:- recovered_frame = self.cam.get_frame()- if recovered_frame is not None:- while True:- try:- print("Attempting to restart HLS pipeline after FFmpeg write failure...")- self.start()- return- except Exception as e3:- print("Restart attempt failed", e3)- time.sleep(retry_delay)- time.sleep(0.5)-- if self.ffmpeg_proc is not None and self.ffmpeg_proc.poll() is not None:- print("FFmpeg process exited, restarting...")- try:- self.stop()- except Exception as e:- print("stop() raised after ffmpeg exit:", e)-- while True:- recovered_frame = self.cam.get_frame()- if recovered_frame is not None:- while True:- try:- print("Restarting HLS pipeline after ffmpeg exit...")- self.start()- return- except Exception as e4:- print("Restart attempt failed, will retry:", e4)- time.sleep(retry_delay)- time.sleep(0.5)+ print(f"HLS write failed: {e}, restarting...")+ self._safe_restart()+time.sleep(1 / 30)+ def _safe_restart(self):+ try:+ self.stop()+ except Exception as e:+ print(f"Error during stop: {e}")+ time.sleep(2)+ while True: # 3 second timeout+ if self.cam.get_frame() is not None:+ break+ time.sleep(1)+ try:+ self.start()+ print("HLS streamer restarted successfully")+ except Exception as e:+ print(f"Failed to restart HLS: {e}")+def stop(self):self.running = Falsediff --git a/yolox/tracker/byte_tracker.py b/yolox/tracker/byte_tracker.pyindex 9362413..11067a7 100644--- a/yolox/tracker/byte_tracker.py+++ b/yolox/tracker/byte_tracker.py@@ -3,11 +3,12 @@ import numpy as npfrom .kalman_filter import KalmanFilterfrom yolox.tracker import matchingfrom .basetrack import BaseTrack, TrackState+from collections import defaultdictclass STrack(BaseTrack):shared_kalman = KalmanFilter()def __init__(self, tlwh, score, class_id):-+ self.occurrences = defaultdict(float)# wait activateself._tlwh = np.asarray(tlwh, dtype=np.float64)self.kalman_filter = None@@ -81,7 +82,8 @@ class STrack(BaseTrack):self.is_activated = Trueself.score = new_track.score- self.class_id = new_track.class_id+ self.occurrences[new_track.class_id] += new_track.score+ self.track_id = max(self.occurrences, key=self.occurrences.get) # use highest combined score@property# @jit(nopython=True)