test_tracker.py 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. import sys
  2. import os
  3. sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
  4. import numpy as np
  5. import pytest
  6. from tracker import UltralyticsTracker, TrackedPerson
  7. def test_tracked_person_dataclass():
  8. p = TrackedPerson(track_id=1, bbox=(10, 20, 30, 40), center=(20, 30), confidence=0.9)
  9. assert p.track_id == 1
  10. assert p.class_name == "person"
  11. def test_tracker_yolo_path_returns_person(monkeypatch):
  12. """YOLO 路径:本地若无 GPU/模型,mock model 行为验证过滤逻辑"""
  13. tracker = UltralyticsTracker.__new__(UltralyticsTracker)
  14. tracker.model_type = "yolo"
  15. tracker.conf_threshold = 0.5
  16. tracker.person_threshold = 0.5
  17. tracker.use_gpu = False
  18. tracker.tracker_type = "bytetrack"
  19. class FakeBox:
  20. cls = np.array([0.0])
  21. conf = np.array([0.8])
  22. xyxy = np.array([[10, 20, 30, 40]])
  23. id = None
  24. def __len__(self):
  25. return len(self.cls)
  26. class FakeResult:
  27. names = {0: "person"}
  28. boxes = FakeBox()
  29. def fake_model(frame, **kwargs):
  30. return [FakeResult()]
  31. tracker.model = fake_model
  32. frame = np.zeros((480, 640, 3), dtype=np.uint8)
  33. results = tracker._detect_yolo(frame)
  34. assert len(results) == 1
  35. # 未经过 tracker 关联时,track_id 使用占位值 -1
  36. assert results[0].track_id == -1
  37. def test_resolve_model_fallback():
  38. from tracker import resolve_model
  39. # 不存在的路径应回退到 yolo11n.pt
  40. path, mtype = resolve_model("/not/exist/model.rknn", "auto")
  41. assert path == "yolo11n.pt"
  42. assert mtype == "yolo"