Add object detection, tracking, mobile notifications, and search to any security camera.
Tests (#100)
Rory Clear555934671bf0
diff --git a/clearcam.py b/clearcam.pyindex 38a7b94..fcddedc 100644--- a/clearcam.py+++ b/clearcam.py@@ -322,7 +322,7 @@ def get_weights_location(yolo_variant: str) -> Path:return f32_weights@TinyJit-def do_inf(im):+def do_inf(im, yolo_infer):im = im.unsqueeze(0)im = im[..., ::-1].permute(0, 3, 1, 2)im = im / 255.0@@ -714,7 +714,7 @@ class VideoCapture:if frame is not None:frame = Tensor(frame)pre = preprocess(frame)- preds = do_inf(pre).numpy()+ preds = do_inf(pre, yolo_infer).numpy()if track:thresh = (self.settings.get("threshold") if self.settings else 0.5) or 0.5 #todo clean!online_targets = tracker.update(preds, [1280,1280], [1280,1280], thresh) #todo, zone in js also hardcoded to 1280diff --git a/test/test.py b/test/test.pyindex f9c34de..2316508 100644--- a/test/test.py+++ b/test/test.py@@ -2,6 +2,11 @@ import numpy as npfrom scipy.optimize import linear_sum_assignment as scipy_linear_sum_assignmentfrom yolox.tracker.matching import linear_sum_assignment2import time+from clearcam import YOLOv8, get_weights_location, get_variant_multiples, do_inf+from tinygrad.nn.state import safe_load, load_state_dict+from tinygrad import Tensor+import cv2+from collections import defaultdictdef test_linear_sum_assigment():for i in range(1,300):@@ -24,9 +29,69 @@ def test_linear_sum_assigment_speed():scipy_linear_sum_assignment(input)t_scipy = time.perf_counter() - sttotal_scipy += t_scipy- print(t / t_scipy, i)- #np.testing.assert_(t < t_scipy*50, f"test_linear_sum_assigment_speed slow from {i}")print("time vs scipy =", f"{(total / total_scipy) * 100:.1f}%")+def test_yolo_infer():+ depth, width, ratio = get_variant_multiples("s")+ yolo_infer = YOLOv8(w=width, r=ratio, d=depth, num_classes=80)+ state_dict = safe_load(get_weights_location("s"))+ load_state_dict(yolo_infer, state_dict)+ frame = cv2.VideoCapture("test/MOT16-03.mp4").read()[1]+ frame = Tensor(frame)+ preds = do_inf(frame, yolo_infer).numpy()+ counts = defaultdict(int)+ for x in preds:+ if x[-2] > 0: counts[int(x[-1])] += 1+ assert {int(k): v for k, v in counts.items()} == {2: 1, 0: 35, 1: 2, 3: 1}++import pickle+from yolox.tracker.byte_tracker import BYTETracker++def test_bytetracker():+ class Args:+ def __init__(self):+ self.track_buffer = 60+ self.mot20 = False+ self.match_thresh = 0.9+ tracker = BYTETracker(Args())++ inputs = pickle.load(open("test/tracker_inputs.pkl", "rb"))+ outputs = pickle.load(open("test/tracker_outputs.pkl", "rb"))+ for i in range(len(inputs)):+ output = tracker.update(inputs[i], [1280,1280], [1280,1280], threshold=0.5)+ assert len(outputs[i]) == len(output)+ for j in range(len(output)):+ np.testing.assert_allclose(output[j]._tlwh, outputs[i][j]._tlwh)++def setup_test_bytetracker():+ depth, width, ratio = get_variant_multiples("s")+ yolo_infer = YOLOv8(w=width, r=ratio, d=depth, num_classes=80)+ state_dict = safe_load(get_weights_location("s"))+ load_state_dict(yolo_infer, state_dict)++ class Args:+ def __init__(self):+ self.track_buffer = 60+ self.mot20 = False+ self.match_thresh = 0.9++ inputs = []+ outputs = []++ tracker = BYTETracker(Args())+ for _ in range(1500):+ cap = cv2.VideoCapture("test/MOT16-03.mp4")+ frame = cap.read()[1]+ frame = Tensor(frame)+ preds = do_inf(frame, yolo_infer).numpy()+ inputs.append(preds)+ preds = tracker.update(preds, [1280,1280], [1280,1280], threshold=0.5)+ outputs.append(preds)+ pickle.dump(inputs, open("test/tracker_inputs.pkl", "wb"))+ pickle.dump(outputs, open("test/tracker_outputs.pkl", "wb"))++#setup_test_bytetracker()+test_bytetracker()+test_yolo_infer()test_linear_sum_assigment()test_linear_sum_assigment_speed()\ No newline at end of filediff --git a/test/tracker_inputs.pkl b/test/tracker_inputs.pklnew file mode 100644index 0000000..84f230fBinary files /dev/null and b/test/tracker_inputs.pkl differdiff --git a/test/tracker_outputs.pkl b/test/tracker_outputs.pklnew file mode 100644index 0000000..a0226f4Binary files /dev/null and b/test/tracker_outputs.pkl differ