Files

115 lines
3.6 KiB
Python

"""Frame geometry pinned to FCP's 1280x720 analysis space."""
import cv2
import numpy as np
import src.detection as detection_mod
import src.pipeline as pipeline_mod
import src.tracking as tracking_mod
from src.model_registry import ModelConfig
from src.pipeline import ANALYSIS_SIZE, analysis_size, run_pipeline
def _fake_yolo(recorder):
"""Fake ultralytics YOLO records kwargs of track()/predict()."""
class _Result:
boxes = None
class _FakeYOLO:
names = {"sack": 0, "box": 0, "truck": 0}
def __init__(self, path):
self.path = path
def track(self, frame, **kwargs):
recorder.update(kwargs)
return [_Result()]
def predict(self, frame, **kwargs):
recorder.update(kwargs)
return [_Result()]
return _FakeYOLO
def test_analysis_size_is_always_1280x720():
"""Every clip is analysed in FCP's 1280x720 frame."""
assert ANALYSIS_SIZE == (1280, 720)
assert analysis_size(1920, 1080) == (1280, 720)
assert analysis_size(960, 720) == (1280, 720)
assert analysis_size(1280, 960) == (1280, 720)
def test_tracker_pins_imgsz_640(monkeypatch):
"""Tracking runs at 640x640 (FCP predict.py:467)."""
calls = {}
monkeypatch.setattr(tracking_mod, "YOLO", _fake_yolo(calls))
tracker = tracking_mod.ByteTrackTracker("fake.engine", conf=0.3)
tracker.update(np.zeros((720, 1280, 3), dtype=np.uint8), [])
assert calls["imgsz"] == 640
def test_detector_pins_imgsz_640(monkeypatch):
"""Plain detection (truck detector) runs at 640x640 too."""
calls = {}
monkeypatch.setattr(detection_mod, "YOLO", _fake_yolo(calls))
detector = detection_mod.BaseDetector("fake.engine", conf=0.45)
detector.detect(np.zeros((720, 1280, 3), dtype=np.uint8))
assert calls["imgsz"] == 640
def test_pipeline_analyses_and_writes_at_1280x720(tmp_path, monkeypatch):
"""A 320x240 clip is analysed and written at FCP's 1280x720 (predict.py:826)."""
video_path = str(tmp_path / "clip.mp4")
clip_writer = cv2.VideoWriter(
video_path, cv2.VideoWriter_fourcc(*"mp4v"), 25.0, (320, 240)
)
for _ in range(3):
clip_writer.write(np.zeros((240, 320, 3), dtype=np.uint8))
clip_writer.release()
seen = {}
class _Result:
boxes = None
class _FakeModel:
names = {"sack": 0}
def track(self, frame, **kwargs):
return [_Result()]
def predict(self, frame, **kwargs):
seen["predict_shape"] = frame.shape
return [_Result()]
monkeypatch.setattr(pipeline_mod, "YOLO", lambda path: _FakeModel())
monkeypatch.setattr(tracking_mod, "YOLO", _FakeModel)
monkeypatch.setattr(detection_mod, "YOLO", _FakeModel)
output_path = str(tmp_path / "out.mp4")
run_pipeline(
video_path=video_path,
model_config=ModelConfig(
filename="fake.engine", path="fake.engine", stem="fake",
known_classes=["sack"],
),
truck_model_config=ModelConfig(
filename="fake-truck.engine", path="fake-truck.engine", stem="fake-truck",
known_classes=["truck"],
),
truck_det_interval=1,
output_path=output_path,
preview_enabled=False,
)
# The model sees the resized frame, not the clip's native one.
assert seen["predict_shape"][:2] == (720, 1280)
# The annotated file carries the analysis frame size.
cap = cv2.VideoCapture(output_path)
assert cap.isOpened()
assert int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)) == 1280
assert int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)) == 720
cap.release()