Add object detection, tracking, mobile notifications, and search to any security camera.
clean
roryclear80b45621736b
diff --git a/detection/yolov9.py b/detection/yolov9.pyindex ea88945..623cf02 100644--- a/detection/yolov9.py+++ b/detection/yolov9.py@@ -462,72 +462,6 @@ def postprocess(output, max_det=300, conf_threshold=0.25, iou_threshold=0.45):no_overlap_mask = high_iou_mask.sum(axis=1) == 0return boxes * no_overlap_mask.unsqueeze(-1)-def draw_bounding_boxes(orig_img_path, predictions, class_labels):- color_dict = {- label: tuple((((i+1) * 50) % 256, ((i+1) * 100) % 256, ((i+1) * 150) % 256))- for i, label in enumerate(class_labels)- }- font = cv2.FONT_HERSHEY_SIMPLEX-- def is_bright_color(color):- r, g, b = color- brightness = (r * 299 + g * 587 + b * 114) / 1000- return brightness > 127-- orig_img = (- cv2.imread(orig_img_path)- if not isinstance(orig_img_path, np.ndarray)- else cv2.imdecode(orig_img_path, 1)- )-- height, width, _ = orig_img.shape- box_thickness = int((height + width) / 400)- font_scale = (height + width) / 2500- object_count = defaultdict(int)-- for pred in predictions:- if len(pred) == 7: # todo- x1, y1, x2, y2, conf, class_id, _ = pred- else:- x1, y1, x2, y2, conf, class_id = pred- if conf == 0:- continue-- x1, y1, x2, y2, class_id = map(int, (x1, y1, x2, y2, class_id))- color = color_dict[class_labels[class_id]]-- cv2.rectangle(orig_img, (x1, y1), (x2, y2), color, box_thickness)-- label = f"{class_labels[class_id]} {conf:.2f}"- text_size, _ = cv2.getTextSize(label, font, font_scale, 1)- label_y, bg_y = (- (y1 - 4, y1 - text_size[1] - 4)- if y1 - text_size[1] - 4 > 0- else (y1 + text_size[1], y1)- )-- cv2.rectangle(- orig_img,- (x1, bg_y),- (x1 + text_size[0], bg_y + text_size[1]),- color,- -1,- )-- font_color = (0, 0, 0) if is_bright_color(color) else (255, 255, 255)- cv2.putText(- orig_img,- label,- (x1, label_y),- font,- font_scale,- font_color,- 1,- cv2.LINE_AA,- )-- object_count[class_labels[class_id]] += 1- return orig_imgSIZES = {"t": [16, 64, 96, 24, 128, 256, 224, 160, 48, 144, 192, 80, 32, 16, 3, 96, 32, 64, 128, 64, 64, 128,"t"],"s": [32, 128, 192, 48, 256, 512, 448, 320, 96, 288, 384, 128, 64, 32, 3, 192, 64, 64, 128, 128, 128, 256, "s"],diff --git a/test/test_mot.py b/test/test_mot.pyindex 5af1d69..85e4004 100644--- a/test/test_mot.py+++ b/test/test_mot.py@@ -1,6 +1,7 @@from detection.yolov9 import YOLOv9, safe_load, load_state_dict, Sequential, Silence, Conv, RepNCSPELAN4, AConv,\-ADown, CBLinear, CBFuse, SPPELAN, Upsample, Concat, DDetect, postprocess, fetch, draw_bounding_boxes+ADown, CBLinear, CBFuse, SPPELAN, Upsample, Concat, DDetect, postprocess, fetchfrom detection.rfdetr import RFDETR, detr_to_yolo+from utils.helpers import draw_bounding_boxesimport cv2from tinygrad import Tensor, TinyJitfrom tinygrad.dtype import dtypesdiff --git a/utils/helpers.py b/utils/helpers.pyindex 2a502a5..4c6569b 100644--- a/utils/helpers.py+++ b/utils/helpers.py@@ -2,7 +2,7 @@ import uuidimport httpfrom pathlib import Pathimport shutil-from collections import deque+from collections import deque, defaultdictimport timeimport osimport subprocess@@ -11,6 +11,8 @@ import jsonimport structfrom datetime import datetimeimport threading+import cv2+import numpy as npBASE_DIR = Path(__file__).parent.parent / "data"def send_notif(session_token: str, text=None):@@ -47,6 +49,73 @@ def send_notif(session_token: str, text=None):conn.close()+def draw_bounding_boxes(orig_img_path, predictions, class_labels):+ color_dict = {+ label: tuple((((i+1) * 50) % 256, ((i+1) * 100) % 256, ((i+1) * 150) % 256))+ for i, label in enumerate(class_labels)+ }+ font = cv2.FONT_HERSHEY_SIMPLEX++ def is_bright_color(color):+ r, g, b = color+ brightness = (r * 299 + g * 587 + b * 114) / 1000+ return brightness > 127++ orig_img = (+ cv2.imread(orig_img_path)+ if not isinstance(orig_img_path, np.ndarray)+ else cv2.imdecode(orig_img_path, 1)+ )++ height, width, _ = orig_img.shape+ box_thickness = int((height + width) / 400)+ font_scale = (height + width) / 2500+ object_count = defaultdict(int)++ for pred in predictions:+ if len(pred) == 7: # todo+ x1, y1, x2, y2, conf, class_id, _ = pred+ else:+ x1, y1, x2, y2, conf, class_id = pred+ if conf == 0:+ continue++ x1, y1, x2, y2, class_id = map(int, (x1, y1, x2, y2, class_id))+ color = color_dict[class_labels[class_id]]++ cv2.rectangle(orig_img, (x1, y1), (x2, y2), color, box_thickness)++ label = f"{class_labels[class_id]} {conf:.2f}"+ text_size, _ = cv2.getTextSize(label, font, font_scale, 1)+ label_y, bg_y = (+ (y1 - 4, y1 - text_size[1] - 4)+ if y1 - text_size[1] - 4 > 0+ else (y1 + text_size[1], y1)+ )++ cv2.rectangle(+ orig_img,+ (x1, bg_y),+ (x1 + text_size[0], bg_y + text_size[1]),+ color,+ -1,+ )++ font_color = (0, 0, 0) if is_bright_color(color) else (255, 255, 255)+ cv2.putText(+ orig_img,+ label,+ (x1, label_y),+ font,+ font_scale,+ font_color,+ 1,+ cv2.LINE_AA,+ )++ object_count[class_labels[class_id]] += 1+ return orig_img+def export_clip(stream_dir, output_path: Path, live=False, length=5, end=0, start=None):segments = sorted(stream_dir.glob("*.ts"), key=os.path.getmtime)recent_segments = deque()