Add object detection, tracking, mobile notifications, and search to any security camera.
jit cache helper for both detection models
roryclear06759ec51326
diff --git a/clearcam.py b/clearcam.pyindex 4ba9c92..648c470 100644--- a/clearcam.py+++ b/clearcam.py@@ -27,7 +27,7 @@ from utils.db import dbimport multiprocessingimport reimport base64-from utils.helpers import send_notif, find_ffmpeg, export_clip, upload_file, encrypt_file, export_and_upload+from utils.helpers import send_notif, find_ffmpeg, export_clip, upload_file, encrypt_file, export_and_upload, jit_inferimport pickle# RTSP URL@@ -569,7 +569,7 @@ class VideoCapture:def run_inference(self, frame, cam_name):frame = Tensor(frame)- preds = model.jit_infer(frame).numpy()+ preds = jit_infer(model, frame, jit_cache).numpy()thresh = (self.settings[cam_name].get("threshold") if self.settings[cam_name] else 0.5) or 0.5 #todo clean!online_targets = self.tracker[cam_name].update(preds, thresh)online_targets = [p for p in online_targets if (classes is None or str(int(p.class_id)) in classes)]@@ -1345,6 +1345,7 @@ class ThreadedHTTPServer(ThreadingMixIn, HTTPServer):super().server_close()if __name__ == "__main__":+ jit_cache = {}alerts_on = {}multiprocessing.set_start_method("spawn", force=True)database = db()diff --git a/detection/rfdetr.py b/detection/rfdetr.pyindex 543cfa1..881ca10 100644--- a/detection/rfdetr.py+++ b/detection/rfdetr.py@@ -602,7 +602,6 @@ class RFDETR():self.backbone.encoder.encoder.layer[i].mlp.fc2 = nn.Linear(1536, 384)state_dict = safe_load(fetch(f'https://huggingface.co/roryclear/rf-detr/resolve/main/{name}.safetensors'))load_state_dict(self, state_dict)- self.jit_cache = {}# todo, this should be more generic and not in both here and yolodef jit_infer(self, frame):diff --git a/detection/yolov9.py b/detection/yolov9.pyindex 838f8c1..1ead9c3 100644--- a/detection/yolov9.py+++ b/detection/yolov9.py@@ -371,15 +371,6 @@ class YOLOv9():self.model[42] = DDetect(a=256, b=512, c=512, d=256, f=[35, 38, 41])state_dict = safe_load(fetch(f'https://huggingface.co/roryclear/yolov9/resolve/main/yolov9-{size}.safetensors'))load_state_dict(self, state_dict)- self.jit_cache = {}-- def jit_infer(self, frame):- shape = tuple(frame.shape)- if shape not in self.jit_cache:- @TinyJit- def fn(im, model): return model(im)- self.jit_cache[shape] = fn- return self.jit_cache[shape](frame, self)def __call__(self, frame):pre = self.preprocess(frame)diff --git a/utils/helpers.py b/utils/helpers.pyindex e312f15..2eb153c 100644--- a/utils/helpers.py+++ b/utils/helpers.py@@ -14,7 +14,7 @@ import threadingimport cv2import numpy as npBASE_DIR = Path(__file__).parent.parent / "data"-from tinygrad import Tensor+from tinygrad import Tensor, TinyJitdef send_notif(session_token: str, text=None):host = "www.clearcam.org"@@ -204,6 +204,14 @@ def export_and_upload(cam_name, thumbnail, userID, key, start=None, end=0, lengtupload_file(Path(f"{mp4_filename}.aes"), userID)os.unlink(mp4_filename)+def jit_infer(model, x, jit_cache):+ shape = tuple(x.shape)+ if shape not in jit_cache:+ @TinyJit+ def fn(x, model): return model(x)+ jit_cache[shape] = fn+ return jit_cache[shape](x, model)+def find_ffmpeg():ffmpeg_path = shutil.which('ffmpeg')if ffmpeg_path: