Add object detection, tracking, mobile notifications, and search to any security camera.
clean
roryclear0aecd0e1b587
diff --git a/clearcam.py b/clearcam.pyindex 0bdf28d..299de5b 100644--- a/clearcam.py+++ b/clearcam.py@@ -388,7 +388,7 @@ class VideoCapture:timestamp = datetime.now().strftime("%Y-%m-%d")filepath = CAMERA_BASE_DIR / f"{self.cam_name}/event_images/{timestamp}"filepath.mkdir(parents=True, exist_ok=True)- self.annotated_frame = self.draw_predictions(self.last_frame.copy(), filtered_preds)+ self.annotated_frame = draw_predictions(self.last_frame.copy(), filtered_preds)# todo alerts can be sent with the wrong thumbnail if two happen quickly, use mapts = int(time.time() - self.streamer.start_time - 10)filename = filepath / f"{ts}_notif.jpg" if alert.is_notif else filepath / f"{ts}.jpg"@@ -451,7 +451,7 @@ class VideoCapture:count+=1with self.lock:self.raw_frame = frame.copy()- if self.streamer.feeding_frames: self.annotated_frame = self.draw_predictions(frame.copy(), filtered_preds)+ if self.streamer.feeding_frames: self.annotated_frame = draw_predictions(frame.copy(), filtered_preds)time.sleep(1 / 30)except Exception as e:print("Error in capture_loop:", e)@@ -519,23 +519,6 @@ class VideoCapture:fps = 1 / (curr_time - prev_time)prev_time = curr_timeprint(f"\rFPS: {fps:.2f}", end="", flush=True)-- def is_bright_color(self,color):- r, g, b = color- brightness = (r * 299 + g * 587 + b * 114) / 1000- return brightness > 127-- def draw_predictions(self, frame, preds):- for x1, y1, x2, y2, conf, cls, _ in preds:- x1, y1, x2, y2 = map(int, [x1, y1, x2, y2])- label = f"{class_labels[int(cls)]}:{conf:.2f}"- color = color_dict[class_labels[int(cls)]]- frame = draw_rectangle_numpy(frame, (x1, y1), (x2, y2), color, 3)- (text_width, text_height), _ = cv2.getTextSize(label, cv2.FONT_HERSHEY_SIMPLEX, 0.5, 1)- font_color = (0, 0, 0) if self.is_bright_color(color) else (255, 255, 255)- frame = draw_rectangle_numpy(frame, (x1, y1 - text_height - 10), (x1 + text_width + 2, y1), color, -1)- cv2.putText(frame, label, (x1, y1 - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, font_color, 1, cv2.LINE_AA)- return framedef get_frame(self):with self.lock:@@ -550,6 +533,23 @@ class VideoCapture:if self.hls_proc:self.hls_proc.kill()+def is_bright_color(color):+ r, g, b = color+ brightness = (r * 299 + g * 587 + b * 114) / 1000+ return brightness > 127++def draw_predictions(frame, preds):+ for x1, y1, x2, y2, conf, cls, _ in preds:+ x1, y1, x2, y2 = map(int, [x1, y1, x2, y2])+ label = f"{class_labels[int(cls)]}:{conf:.2f}"+ color = color_dict[class_labels[int(cls)]]+ frame = draw_rectangle_numpy(frame, (x1, y1), (x2, y2), color, 3)+ (text_width, text_height), _ = cv2.getTextSize(label, cv2.FONT_HERSHEY_SIMPLEX, 0.5, 1)+ font_color = (0, 0, 0) if is_bright_color(color) else (255, 255, 255)+ frame = draw_rectangle_numpy(frame, (x1, y1 - text_height - 10), (x1 + text_width + 2, y1), color, -1)+ cv2.putText(frame, label, (x1, y1 - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, font_color, 1, cv2.LINE_AA)+ return frame+class HLSStreamer:def __init__(self, video_capture, output_dir="streams", segment_time=4, cam_name="clearcampy"):self.cam_name = cam_name