Add object detection, tracking, mobile notifications, and search to any security camera.
Egpu7 (#149)
Rory Clear0ac68458f75c
diff --git a/clearcam.py b/clearcam.pyindex 4f9031a..a2eb2af 100644--- a/clearcam.py+++ b/clearcam.py@@ -164,6 +164,8 @@ def _get_stream_resolution(src):class VideoCapture:def __init__(self, src, cam_name="camera", vod=False):+ self.frame_num = -1+ self.last_frame_num = -1self.vod = vodself.output_dir_raw = BASE_DIR / "cameras" / f'{cam_name}' / "streams"self.current_stream_dir_raw = self._get_new_stream_dir()@@ -184,6 +186,7 @@ class VideoCapture:self.annotated_frame = Noneself.last_preds = []self.last_frame = {}+ self.lock = {}self.settings = None@@ -201,10 +204,10 @@ class VideoCapture:self.alert_counters[id] = alert_counterdatabase.run_put("alerts", self.cam_name, alert_counter, id=id)- self.lock = threading.Lock()+ self.lock[self.cam_name] = 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()+ if not self.vod or not self.output_dir_raw.exists(): threading.Thread(target=self.frame_loop, daemon=True).start()def _get_new_stream_dir(self):timestamp = "video" if self.vod else datetime.now().strftime("%Y-%m-%d")@@ -323,11 +326,31 @@ class VideoCapture:cv2.imwrite(str(object_filename), crop)- def capture_loop(self):- self.frame_num = -1- self.last_frame_num = -1+ def frame_loop(self):frame_size = self.width * self.height * 3fail_count = 0+ while self.running:+ try:+ raw_bytes = self.proc[cam_name].stdout.read(frame_size)+ if len(raw_bytes) != frame_size:+ fail_count += 1+ if fail_count > 5:+ print(f"{cam_name} FFmpeg frame read failed (count={fail_count}), restarting stream...{self.src[cam_name]}")+ self.hls_proc[cam_name], self.proc[cam_name] = self._open_ffmpeg(cam_name)+ fail_count = 0+ time.sleep(0.5)+ else:+ fail_count = 0+ with self.lock[self.cam_name]:+ self.raw_frame = np.frombuffer(raw_bytes, np.uint8).reshape((self.height, self.width, 3))+ self.frame_num += 1+ time.sleep(1 / 30)+ except Exception as e:+ print("Error in frame_loop:", e, self.cam_name)+ time.sleep(1)++ def capture_loop(self):+ prev_time = time.time()last_det = -1send_det = Falselast_live_check = time.time()@@ -363,21 +386,30 @@ class VideoCapture:else:self.last_preds, _ = self.run_inference(frame, cam_name=cam_name)database.run_put("analysis_prog", cam_name, {"Tracking":self.cap[cam_name].get(cv2.CAP_PROP_POS_FRAMES)/self.cap[cam_name].get(cv2.CAP_PROP_FRAME_COUNT)*100})++ with self.lock[cam_name]:+ frame_num = self.frame_num+ last_frame_num = self.last_frame_num+ frame = self.raw_frame.copy()+ if frame_num == last_frame_num:+ time.sleep(1 / 30)+ continue++ if not any(counter.is_active() for _, counter in self.alert_counters.items()): # don't run inference when no active scheds+ time.sleep(1 / 30)+ with self.lock[cam_name]: self.last_preds = [] # to remove annotation when no alerts activeelse:- raw_bytes = self.proc[cam_name].stdout.read(frame_size)- if len(raw_bytes) != frame_size:- fail_count += 1- if fail_count > 5:- print(f"{cam_name} FFmpeg frame read failed (count={fail_count}), restarting stream...{self.src[cam_name]}")- self.hls_proc[cam_name], self.proc[cam_name] = self._open_ffmpeg(cam_name)- fail_count = 0- time.sleep(0.5)- continue- else:- fail_count = 0- with self.lock:- self.raw_frame = np.frombuffer(raw_bytes, np.uint8).reshape((self.height, self.width, 3))- self.frame_num += 1+ preds, frame = self.run_inference(frame, cam_name=self.cam_name)+ with self.lock[self.cam_name]:+ self.last_preds = preds.copy()+ self.last_frame[self.cam_name] = frame.numpy().copy()+ self.last_frame_num = self.frame_num++ curr_time = time.time()+ fps = 1 / (curr_time - prev_time)+ prev_time = curr_time+ print(f"\rFPS: {fps:.2f}", end="", flush=True)+filtered_preds = self.last_predsif count > 10:@@ -466,31 +498,6 @@ class VideoCapture:print("Error in capture_loop:", e, self.cam_name)self._open_ffmpeg(self.cam_name)time.sleep(1)-- # todo, capture loop has to run fast, cannot be slown down by inference, it needs to "catch up" with latest frames- 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- frame_num = self.frame_num- last_frame_num = self.last_frame_num- if frame is not None and frame_num != last_frame_num:- 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()- self.last_frame_num = self.frame_num-- 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@@ -544,11 +551,6 @@ class VideoCapture:preds = np.array(preds)return preds, frame- def get_frame(self):- with self.lock:- if self.annotated_frame is not None: return self.annotated_frame.copy()- return None, None-def release(self):self.running = Falseif self.cam_name in self.proc: self.proc[self.cam_name].kill()@@ -1417,4 +1419,3 @@ if __name__ == "__main__":if url:cam.release()server.shutdown()-diff --git a/objects.py b/objects.pyindex 47706da..3fd98b5 100644--- a/objects.py+++ b/objects.py@@ -236,6 +236,7 @@ class ObjectFinder:emb_ret.append(emb)path_ret.append(img_path)if vod: database.run_put("analysis_prog", cam_name, {"Processing":((i + 1)/len(new_image_list))*100})+ if (i+1) % 10 == 0 and i > 0: print(f"Processed {(i+1)} / {len(new_image_list)} new images...")print(f"Processed {len(new_image_list)} new images...")save_embeddings(folder_path, "embeddings.pkl", folder_embeddings, folder_paths)return emb_ret, path_ret