"""Tests for the instructor-frame filter.""" from pathlib import Path import cv2 import numpy as np import pytest from PIL import Image from app.pipeline.filter import ( _CASCADE_SPECS, _Detector, _load_cascades, _load_detector, is_instructor_frame, filter_instructor_frames, ) from app.pipeline.types import SceneFrame from app.pipeline import yunet as yunet_mod def _save(path: Path, color) -> None: path.parent.mkdir(parents=True, exist_ok=True) Image.new("RGB", (320, 180), color).save(path, "JPEG") def test_load_cascades_returns_multiple_detectors(): cascades = _load_cascades() names = {spec.name for spec, _ in cascades} # Frontal face is the baseline; the other two are the additions that # actually catch instructor frames in the wild. assert "frontal" in names assert names & {"profile", "upperbody"}, ( f"need profile or upperbody for angled-face / hands-up coverage; got {names}" ) def test_is_instructor_frame_returns_false_on_uniform_image(tmp_path): # A solid-color frame has no detectable face, no upper body. p = tmp_path / "blank.jpg" _save(p, (255, 255, 255)) assert is_instructor_frame(p, face_area_threshold=0.01) is False def test_is_instructor_frame_handles_unreadable_path(tmp_path): # Missing file: don't crash, treat as non-instructor. assert is_instructor_frame(tmp_path / "does-not-exist.jpg") is False def test_filter_keeps_blank_frames_and_unlinks_dropped(tmp_path): # No real faces here; the filter should keep both frames and the # filesystem should be untouched. a = tmp_path / "a.jpg" b = tmp_path / "b.jpg" _save(a, (255, 255, 255)) _save(b, (10, 10, 10)) frames = [ SceneFrame(0, 0.0, 5.0, a), SceneFrame(1, 5.0, 10.0, b), ] kept, dropped = filter_instructor_frames(frames, face_area_threshold=0.05) assert len(kept) == 2 assert dropped == [] assert a.exists() and b.exists() def _force_instructor_paths(monkeypatch, instructor_paths): """Stub the per-frame face-area measurement so the named paths classify as instructor (ratio above the filter's threshold) and everyone else as clean slide content (ratio of 0). """ targets = {Path(p).resolve() for p in instructor_paths} import app.pipeline.filter as filter_mod def fake_ratio(image_path, detector=None): return 1.0 if Path(image_path).resolve() in targets else 0.0 monkeypatch.setattr(filter_mod, "_face_area_ratio", fake_ratio) def test_filter_drops_instructor_alternates_and_unlinks_them(tmp_path, monkeypatch): # The picker should never offer an instructor alternate. Only the # primary survives; the talking-head alt is removed from disk. primary = tmp_path / "slide.jpg" alt_bad = tmp_path / "talking_head.jpg" alt_good = tmp_path / "slide_alt.jpg" for p in (primary, alt_bad, alt_good): _save(p, (200, 200, 200)) _force_instructor_paths(monkeypatch, [alt_bad]) frame = SceneFrame(0, 0.0, 5.0, primary, alt_image_paths=[alt_bad, alt_good]) kept, dropped = filter_instructor_frames([frame]) assert dropped == [] assert len(kept) == 1 assert kept[0].image_path == primary assert kept[0].alt_image_paths == [alt_good] assert primary.exists() and alt_good.exists() assert not alt_bad.exists() def test_filter_promotes_first_surviving_alt_when_primary_is_instructor( tmp_path, monkeypatch ): # Brightest frame happened to be a talking-head; instead of losing the # whole segment, the first non-instructor alternate gets promoted. primary = tmp_path / "talking_head.jpg" alt_one = tmp_path / "slide_a.jpg" alt_two = tmp_path / "slide_b.jpg" for p in (primary, alt_one, alt_two): _save(p, (200, 200, 200)) _force_instructor_paths(monkeypatch, [primary]) frame = SceneFrame(0, 0.0, 5.0, primary, alt_image_paths=[alt_one, alt_two]) kept, dropped = filter_instructor_frames([frame]) assert dropped == [] assert len(kept) == 1 assert kept[0].image_path == alt_one assert kept[0].alt_image_paths == [alt_two] assert not primary.exists() assert alt_one.exists() and alt_two.exists() def test_filter_drops_frame_only_when_every_candidate_is_instructor( tmp_path, monkeypatch ): primary = tmp_path / "head1.jpg" alt = tmp_path / "head2.jpg" for p in (primary, alt): _save(p, (200, 200, 200)) _force_instructor_paths(monkeypatch, [primary, alt]) frame = SceneFrame(0, 0.0, 5.0, primary, alt_image_paths=[alt]) kept, dropped = filter_instructor_frames([frame]) assert kept == [] assert dropped == [frame] assert not primary.exists() and not alt.exists() def test_threshold_changes_drop_decision(tmp_path): # Build a frame with a synthetic large oval the upperbody cascade is # at least a candidate to fire on — but this is only meant to verify # the threshold knob still gates decisions, NOT the cascade quality. # If detection happens to miss this synthetic shape on the test box, # the test just confirms both calls return False (consistent), which # is still a valid invariant: same input + lower threshold ≠ more # frames kept. p = tmp_path / "shape.jpg" arr = np.full((360, 640, 3), 255, dtype=np.uint8) cv2.ellipse(arr, (320, 220), (90, 130), 0, 0, 360, (60, 60, 60), -1) cv2.imwrite(str(p), arr) detector = _load_detector() strict = is_instructor_frame(p, face_area_threshold=0.001, detector=detector) lenient = is_instructor_frame(p, face_area_threshold=0.99, detector=detector) # Lower threshold can never be MORE permissive than higher threshold. assert not (lenient and not strict) def test_detector_falls_back_to_haar_when_yunet_unavailable(monkeypatch): # Force YuNet load to fail; the filter must still build a working # detector backed by the multi-cascade Haar setup so the pipeline # never crashes on offline / model-missing environments. monkeypatch.setattr(yunet_mod.YuNetDetector, "try_load", classmethod(lambda cls, **kw: None)) detector = _Detector() assert detector.name == "haar" assert detector.cascades is not None # And it can still classify a blank frame without raising. arr = np.full((100, 100, 3), 255, dtype=np.uint8) assert detector.largest_region_area(arr) == 0 def test_yunet_detector_loads_when_model_present(tmp_path, monkeypatch): # Skip cleanly when no model is reachable — networked CI may not have # one, and the bundle is intentionally optional. model = yunet_mod.resolve_model_path(allow_download=False) if model is None: pytest.skip("YuNet model not bundled / downloaded in this environment") detector = yunet_mod.YuNetDetector.try_load(allow_download=False) assert detector is not None arr = np.full((240, 320, 3), 255, dtype=np.uint8) assert detector.largest_face_area(arr) == 0 # blank frame, no faces def test_yunet_resolve_skips_lfs_pointer_stubs(tmp_path, monkeypatch): # An LFS pointer file (~130 bytes) must NOT be accepted as a real model # — that's exactly the failure mode I hit while wiring this up. bundled_dir = tmp_path / "models" bundled_dir.mkdir() pointer = bundled_dir / yunet_mod.MODEL_FILENAME pointer.write_text( "version https://git-lfs.github.com/spec/v1\n" "oid sha256:0000\nsize 232589\n" ) monkeypatch.setattr(yunet_mod, "_model_search_paths", lambda: [pointer]) assert yunet_mod.resolve_model_path(allow_download=False) is None