Skip to content

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

no tracker video test

roryclear7e0f73ad3057
diff --git a/detection/yolov9.py b/detection/yolov9.py
index 2dedcbf..a5e6533 100644
--- a/detection/yolov9.py
+++ b/detection/yolov9.py
@@ -455,15 +455,24 @@ def rescale_bounding_boxes(predictions, from_size=None, to_size=None):
rescaled_predictions.append([x1_scaled, y1_scaled, x2_scaled, y2_scaled, conf, class_id])
return rescaled_predictions
-def draw_bounding_boxes_and_save(orig_img_path, output_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)}
+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)
+ 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
@@ -471,24 +480,44 @@ def draw_bounding_boxes_and_save(orig_img_path, output_img_path, predictions, cl
for pred in predictions:
x1, y1, x2, y2, conf, class_id = pred
- if conf == 0: continue
+ 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
+ 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,
+ )
- print("Objects detected:")
- for obj, count in object_count.items():
- print(f"- {obj}: {count}")
+ 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,
+ )
- cv2.imwrite(output_img_path, orig_img)
- print(f'saved detections at {output_img_path}')
+ object_count[class_labels[class_id]] += 1
+ return orig_img
def clip_boxes(boxes, shape):
boxes[..., [0, 2]] = np.clip(boxes[..., [0, 2]], 0, shape[1]) # x1, x2
diff --git a/test/test_outputs.py b/test/test_outputs.py
index cc960ad..b0fd2dd 100644
--- a/test/test_outputs.py
+++ b/test/test_outputs.py
@@ -1,11 +1,14 @@
from detection.yolov9 import YOLOv9, SIZES, safe_load, load_state_dict, Sequential, Silence, Conv, RepNCSPELAN4, AConv,\
-ADown, CBLinear, CBFuse, SPPELAN, Upsample, Concat, DDetect, postprocess, fetch, rescale_bounding_boxes, draw_bounding_boxes_and_save
+ADown, CBLinear, CBFuse, SPPELAN, Upsample, Concat, DDetect, postprocess, fetch, rescale_bounding_boxes, draw_bounding_boxes
import cv2
-from tinygrad import Tensor
+from tinygrad import Tensor, TinyJit
from tinygrad.dtype import dtypes
import numpy as np
from pathlib import Path
+@TinyJit
+def do_inf(im, model): return model(im)
+
def letterbox(im, new_shape=(1280, 1280), color=(114, 114, 114), auto=True, scaleFill=False, scaleup=True, stride=32):
shape = im.shape[:2]
if isinstance(new_shape, int): new_shape = (new_shape, new_shape)
@@ -32,32 +35,32 @@ def letterbox(im, new_shape=(1280, 1280), color=(114, 114, 114), auto=True, scal
return im, ratio, (dw, dh)
if __name__ == "__main__":
- for size in ["t", "s", "m", "c", "e"]:
- weights = f'./yolov9-{size}-tiny.pkl'
- imgsz = (640,640)
- model = YOLOv9(*SIZES[size]) if size in SIZES else YOLOv9()
- state_dict = safe_load(fetch(f'https://huggingface.co/roryclear/yolov9/resolve/main/yolov9-{size}.safetensors'))
- load_state_dict(model, state_dict)
- source = "test/videos/MOT16-03.mp4"
- cap = cv2.VideoCapture(source)
+ size = "t"
+ model = YOLOv9(*SIZES[size]) if size in SIZES else YOLOv9()
+ state_dict = safe_load(fetch(f'https://huggingface.co/roryclear/yolov9/resolve/main/yolov9-{size}.safetensors'))
+ load_state_dict(model, state_dict)
+ cap = cv2.VideoCapture("test/videos/MOT16-03.mp4")
+ w, h = int(cap.get(3)), int(cap.get(4))
+ out = cv2.VideoWriter(f"outputs/out_{size}.mp4", cv2.VideoWriter_fourcc(*"mp4v"), 30, (w, h))
+ class_labels = fetch('https://raw.githubusercontent.com/pjreddie/darknet/master/data/coco.names').read_text().split("\n")
+ Path('./outputs').mkdir(parents=True, exist_ok=True)
+ i = 0
+ while True:
ret, im0 = cap.read()
- success, buffer = cv2.imencode(".jpg", im0)
- if not success: raise RuntimeError("Failed to encode frame")
- source = buffer
-
- cap.release()
+ if not ret: break
im = letterbox(im0, new_shape=(1280, 1280), stride=32, auto=True)[0]
im = im.transpose((2, 0, 1))[::-1]
im = np.ascontiguousarray(im)
im = Tensor(im).cast(dtypes.float32)
im /= 255
- if len(im.shape) == 3: im = im[None]
-
- pred = model(im)
- pred = pred.numpy()[0]
+ if len(im.shape) == 3:
+ im = im[None]
+ pred = do_inf(im, model).numpy()[0]
pred = pred[pred[:, 4] >= 0.25]
- class_labels = fetch('https://raw.githubusercontent.com/pjreddie/darknet/master/data/coco.names').read_text().split("\n")
pred = rescale_bounding_boxes(pred, from_size=(im.shape[2:][::-1]), to_size=im0.shape[:2][::-1])
- Path('./outputs').mkdir(parents=True, exist_ok=True)
- draw_bounding_boxes_and_save(source, f"outputs/out_{size}.jpg", pred, class_labels)
-
+ _, buffer = cv2.imencode(".jpg", im0)
+ out.write(draw_bounding_boxes(buffer, pred, class_labels))
+ i+=1
+ print("frame",i)
+ cap.release()
+ out.release()
\ No newline at end of file