Skip to content

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

more in test

roryclear9cb56549baae
diff --git a/clearcam.py b/clearcam.py
index 299de5b..ab5fb55 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 = draw_predictions(self.last_frame.copy(), filtered_preds)
+ self.annotated_frame = draw_predictions(self.last_frame.copy(), filtered_preds, class_labels, color_dict)
# todo alerts can be sent with the wrong thumbnail if two happen quickly, use map
ts = 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+=1
with self.lock:
self.raw_frame = frame.copy()
- if self.streamer.feeding_frames: self.annotated_frame = draw_predictions(frame.copy(), filtered_preds)
+ if self.streamer.feeding_frames: self.annotated_frame = draw_predictions(frame.copy(), filtered_preds, class_labels, color_dict)
time.sleep(1 / 30)
except Exception as e:
print("Error in capture_loop:", e)
@@ -538,17 +538,17 @@ def is_bright_color(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
+def draw_predictions(frame, preds, class_labels, color_dict):
+ 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"):
@@ -1566,7 +1566,6 @@ if __name__ == "__main__":
start_cam(rtsp=cams[cam_name],cam_name=cam_name,yolo_variant=yolo_variant)
class_labels = fetch('https://raw.githubusercontent.com/pjreddie/darknet/master/data/coco.names').read_text().split("\n")
-
color_dict = {label: tuple((((i+1) * 50) % 256, ((i+1) * 100) % 256, ((i+1) * 150) % 256)) for i, label in enumerate(class_labels)}
#depth, width, ratio = get_variant_multiples(yolo_variant)
if rtsp_url:
diff --git a/test/test.py b/test/test.py
index e35de87..488798d 100644
--- a/test/test.py
+++ b/test/test.py
@@ -2,12 +2,12 @@ import numpy as np
from scipy.optimize import linear_sum_assignment as scipy_linear_sum_assignment
from yolox.tracker.matching import linear_sum_assignment2
import time
-from clearcam import YOLOv9, do_inf, preprocess, SIZES
+from clearcam import YOLOv9, do_inf, preprocess, SIZES, scale_boxes, draw_predictions
from tinygrad.nn.state import safe_load, load_state_dict
from tinygrad import Tensor
import cv2
from collections import defaultdict
-from tinygrad.helpers import Context
+from tinygrad.helpers import Context, fetch
def test_linear_sum_assigment():
for i in range(1,300):
@@ -68,6 +68,8 @@ def test_bytetracker():
def test_tracker():
+ class_labels = fetch('https://raw.githubusercontent.com/pjreddie/darknet/master/data/coco.names').read_text().split("\n")
+ color_dict = {label: tuple((((i+1) * 50) % 256, ((i+1) * 100) % 256, ((i+1) * 150) % 256)) for i, label in enumerate(class_labels)}
yolo_variant = "t"
yolo_infer = YOLOv9(*SIZES[yolo_variant]) if yolo_variant in SIZES else YOLOv9()
class Args:
@@ -79,11 +81,18 @@ def test_tracker():
cap = cv2.VideoCapture("test/videos/MOT16-03.mp4")
while cap.isOpened():
- ret, frame = cap.read()
+ ret, f = cap.read()
if not ret: break
- frame = Tensor(frame)
+ frame = Tensor(f)
pre = preprocess(frame)
preds = do_inf(pre, yolo_infer)[0].numpy()
+ preds = scale_boxes(pre.shape[:2], preds, frame.shape)
+ tracker_preds = tracker.update(preds, [1280,1280], [1280,1280], 0.25) # thresh 0.25
+ preds = []
+ for x in tracker_preds:
+ preds.append(np.array([x.tlwh[0],x.tlwh[1],(x.tlwh[0]+x.tlwh[2]),(x.tlwh[1]+x.tlwh[3]),x.score,x.class_id,x.track_id]))
+ annotated_frame = draw_predictions(f, preds, class_labels, color_dict)
+ print(type(annotated_frame), annotated_frame.shape)
cap.release()
test_tracker()