Skip to content

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

1.6 KB
Raw Blame
from detection.yolov9 import YOLOv9, fetch
from utils.helpers import draw_bounding_boxes, jit_infer
import cv2
from tinygrad import Tensor
from tinygrad.dtype import dtypes
import numpy as np
from pathlib import Path

if __name__ == "__main__":
  from ocsort_tracker import ocsort
  ocs_tracker = ocsort.OCSort(max_age=60)
  
  jit_cache = {}
  size = "t"
  draw = True
  class_labels = fetch('https://raw.githubusercontent.com/pjreddie/darknet/master/data/coco.names').read_text().split("\n")
  Path('./test_outputs').mkdir(parents=True, exist_ok=True)
  cap = cv2.VideoCapture("test/videos/MOT16-03.mp4")
  w, h = int(cap.get(3)), int(cap.get(4))
  if draw: out = cv2.VideoWriter(f"test_outputs/out_{size}.mp4", cv2.VideoWriter_fourcc(*"mp4v"), 30, (w, h))

  model = YOLOv9(size, res=960)
  expected = 156

  i = 0
  ppl = set()
  while True:
    ret, im0 = cap.read()
    if not ret: break
    im = im0
    im = Tensor(im).cast(dtypes.float32)
    pred = jit_infer(model, x=im, jit_cache=jit_cache).numpy()
    online_targets = ocs_tracker.update(pred, 0.25)
    preds = []
    for x in online_targets:
      if x.tracklet_len < 1 or x.speed < 2.5: continue
      if x.class_id == 0 and x.track_id not in ppl: ppl.add(x.track_id)
      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]))
    print("ppl =",len(ppl))
    if draw:
      _, buffer = cv2.imencode(".jpg", im0)
      out.write(draw_bounding_boxes(buffer, preds, class_labels))
    
    i+=1
    print("frame",i)
  cap.release()
  if draw: out.release()
  assert len(ppl) == expected