| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455 |
- import sys
- import os
- sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
- import numpy as np
- import pytest
- from tracker import UltralyticsTracker, TrackedPerson
- def test_tracked_person_dataclass():
- p = TrackedPerson(track_id=1, bbox=(10, 20, 30, 40), center=(20, 30), confidence=0.9)
- assert p.track_id == 1
- assert p.class_name == "person"
- def test_tracker_yolo_path_returns_person(monkeypatch):
- """YOLO 路径:本地若无 GPU/模型,mock model 行为验证过滤逻辑"""
- tracker = UltralyticsTracker.__new__(UltralyticsTracker)
- tracker.model_type = "yolo"
- tracker.conf_threshold = 0.5
- tracker.person_threshold = 0.5
- tracker.use_gpu = False
- tracker.tracker_type = "bytetrack"
- class FakeBox:
- cls = np.array([0.0])
- conf = np.array([0.8])
- xyxy = np.array([[10, 20, 30, 40]])
- id = None
- def __len__(self):
- return len(self.cls)
- class FakeResult:
- names = {0: "person"}
- boxes = FakeBox()
- def fake_model(frame, **kwargs):
- return [FakeResult()]
- tracker.model = fake_model
- frame = np.zeros((480, 640, 3), dtype=np.uint8)
- results = tracker._detect_yolo(frame)
- assert len(results) == 1
- # 未经过 tracker 关联时,track_id 使用占位值 -1
- assert results[0].track_id == -1
- def test_resolve_model_fallback():
- from tracker import resolve_model
- # 不存在的路径应回退到 yolo11n.pt
- path, mtype = resolve_model("/not/exist/model.rknn", "auto")
- assert path == "yolo11n.pt"
- assert mtype == "yolo"
|