Skip to content

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

start mot test

roryclear091b2fa6a80e
diff --git a/test/test.py b/test/test.py
index 3340b3d..268a9c4 100644
--- a/test/test.py
+++ b/test/test.py
@@ -66,38 +66,6 @@ def test_bytetracker():
np.testing.assert_allclose(output[j].class_id, output2[j].class_id)
assert total_time2 <= (total_time * 1.0), "slower"
-
-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:
- def __init__(self):
- self.track_buffer = 60
- self.mot20 = False
- self.match_thresh = 0.9
- tracker = BYTETracker(Args())
-
- cap = cv2.VideoCapture("test/videos/MOT16-03.mp4")
- out = cv2.VideoWriter('output.mp4', cv2.VideoWriter_fourcc(*'mp4v'), 30, (960, 540))
- while cap.isOpened():
- ret, f = cap.read()
- if not ret: break
- 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)
- out.write(annotated_frame)
- cap.release()
- out.release()
-
-test_tracker()
-#test_bytetracker()
-#test_linear_sum_assigment()
-#test_linear_sum_assigment_speed()
\ No newline at end of file
+test_bytetracker()
+test_linear_sum_assigment()
+test_linear_sum_assigment_speed()
\ No newline at end of file
diff --git a/test/test_outputs.py b/test/test_outputs.py
new file mode 100644
index 0000000..cc960ad
--- /dev/null
+++ b/test/test_outputs.py
@@ -0,0 +1,63 @@
+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
+import cv2
+from tinygrad import Tensor
+from tinygrad.dtype import dtypes
+import numpy as np
+from pathlib import Path
+
+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)
+ r = min(new_shape[0] / shape[0], new_shape[1] / shape[1])
+ if not scaleup: r = min(r, 1.0)
+
+ ratio = r, r
+ new_unpad = int(round(shape[1] * r)), int(round(shape[0] * r))
+ dw, dh = new_shape[1] - new_unpad[0], new_shape[0] - new_unpad[1]
+ if auto:
+ dw, dh = np.mod(dw, stride), np.mod(dh, stride)
+ elif scaleFill: # stretch
+ dw, dh = 0.0, 0.0
+ new_unpad = (new_shape[1], new_shape[0])
+ ratio = new_shape[1] / shape[1], new_shape[0] / shape[0]
+
+ dw /= 2
+ dh /= 2
+
+ if shape[::-1] != new_unpad: im = cv2.resize(im, new_unpad, interpolation=cv2.INTER_LINEAR)
+ top, bottom = int(round(dh - 0.1)), int(round(dh + 0.1))
+ left, right = int(round(dw - 0.1)), int(round(dw + 0.1))
+ im = cv2.copyMakeBorder(im, top, bottom, left, right, cv2.BORDER_CONSTANT, value=color)
+ 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)
+ ret, im0 = cap.read()
+ success, buffer = cv2.imencode(".jpg", im0)
+ if not success: raise RuntimeError("Failed to encode frame")
+ source = buffer
+
+ cap.release()
+ 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]
+ 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)
+