Skip to content

Add object detection, tracking, mobile notifications, and search to any security camera.

remove inference loop again, with live check bug fixed

roryclearaa911c14d5a8
diff --git a/clearcam.py b/clearcam.py
index ee93822..d4f3037 100644
--- a/clearcam.py
+++ b/clearcam.py
@@ -203,7 +203,6 @@ class VideoCapture:
self.lock = threading.Lock()
if not self.vod or not self.output_dir_raw.exists(): threading.Thread(target=self.capture_loop, daemon=True).start()
- if not self.vod: threading.Thread(target=self.inference_loop, daemon=True).start()
def _get_new_stream_dir(self):
timestamp = "video" if self.vod else datetime.now().strftime("%Y-%m-%d")
@@ -343,13 +342,6 @@ class VideoCapture:
self.hls_proc[cam_name], self.proc[cam_name] = self._open_ffmpeg(cam_name)
while self.running:
- if time.time() - last_live_check > 5: # todo, for all cams
- last_live_check = time.time()
- link = database.run_get("links", cam_name)
- if type(link) == list: link = link[0] # todo, flakey?
- if link != self.src[cam_name]:
- self.src[cam_name] = link
- self.hls_proc[cam_name], self.proc[cam_name] = self._open_ffmpeg(cam_name)
try:
if not (BASE_DIR / "cameras" / cam_name).is_dir(): os._exit(1) # deleted cam
if self.vod:
@@ -415,9 +407,15 @@ class VideoCapture:
if (send_det and userID is not None and not self.vod) and time.time() - last_det >= 6: #send 15ish second clip after
export_and_upload(cam_name=self.cam_name, thumbnail=filename, userID=userID, key=key)
send_det = False
- if userID and not self.vod and (time.time() - last_live_check) >= 5:
- last_live_check = time.time()
- threading.Thread(target=check_upload_link, args=(self.cam_name,), daemon=True).start()
+
+ if (time.time() - last_live_check) >= 5:
+ last_live_check = time.time()
+ link = database.run_get("links", cam_name)
+ if type(link) == list: link = link[0] # todo, flakey?
+ if link != self.src[cam_name]:
+ self.src[cam_name] = link
+ self.hls_proc[cam_name], self.proc[cam_name] = self._open_ffmpeg(cam_name)
+ if userID and not self.vod: threading.Thread(target=check_upload_link, args=(self.cam_name,), daemon=True).start()
if (time.time() - last_counter_update) >= 5: #update counter every 5 secs
last_counter_update = time.time()
@@ -460,48 +458,27 @@ class VideoCapture:
self.raw_frame = frame.copy()
if not self.vod: time.sleep(1 / 30)
+ prev_time = time.time()
+ if not any(counter.is_active() for _, counter in self.alert_counters.items()): # don't run inference when no active scheds
+ with self.lock: self.last_preds = [] # to remove annotation when no alerts active
+ else:
+ with self.lock:
+ frame = self.raw_frame.copy() if self.raw_frame is not None else None
+ if frame is not None:
+ preds, frame = self.run_inference(frame, cam_name=cam_name)
+ with self.lock:
+ self.last_preds = preds.copy()
+ self.last_frame[self.cam_name] = frame.numpy().copy()
+ curr_time = time.time()
+ fps = 1 / (curr_time - prev_time)
+ prev_time = curr_time
+ print(f"\rFPS: {fps:.2f}", end="", flush=True)
+
except Exception as e:
print("Error in capture_loop:", e, self.cam_name)
self._open_ffmpeg(self.cam_name)
time.sleep(1)
- def inference_loop(self):
- prev_time = time.time()
- while self.running:
- time.sleep(1/30) # todo
- if not any(counter.is_active() for _, counter in self.alert_counters.items()): # don't run inference when no active scheds
- time.sleep(1)
- with self.lock: self.last_preds = [] # to remove annotation when no alerts active
- continue
- with self.lock:
- frame = self.raw_frame.copy() if self.raw_frame is not None else None
- if frame is not None:
- preds, frame = self.run_inference(frame, cam_name=self.cam_name)
- with self.lock:
- self.last_preds = preds.copy()
- self.last_frame[self.cam_name] = frame.numpy().copy()
-
- '''
- if len(preds) > 0 and time.time() - self.last_shapes_time >= 1 / 24:
- h, w = frame.shape[:2]
- preds[:, [0, 2]] /= w
- preds[:, [1, 3]] /= h
- t = time.time() - self.start_time
- self.det_shapes.append({t: preds.tolist()})
- if len(self.det_shapes) == 1:
- with open(self.det_manifest, 'a') as f:
- f.write(f"{t:.3f}: {self.shape_seg}.json\n")
- if time.time() - self.last_shapes_time >= 4:
- print("4 secs")
- self.last_shapes_time = time.time()
- json.dump(self.det_shapes, open(self.det_path / f"{self.shape_seg}.json", "w"))
- self.shape_seg += 1
- self.det_shapes = []
- '''
- curr_time = time.time()
- fps = 1 / (curr_time - prev_time)
- prev_time = curr_time
- print(f"\rFPS: {fps:.2f}", end="", flush=True)
def reset_vod(self):
self.cap[self.cam_name] = cv2.VideoCapture(self.src[self.cam_name]) # reset video on settings change