| """Tests for inference/adapters.py -- inference backend registry and adapter dispatch.""" |
|
|
| import numpy as np |
| import pytest |
|
|
| from inference import adapters |
|
|
|
|
| def test_sam3_adapter_accepts_tracking_modes(): |
| assert adapters.get_adapter("SAM3", tracking="tracking").tracking == "tracking" |
| assert ( |
| adapters.get_adapter("SAM3", tracking="no tracking").tracking == "no tracking" |
| ) |
|
|
|
|
| def test_metric_adapter_is_registered(): |
| assert adapters._ADAPTERS["DA3NESTED-GIANT-LARGE-1.1"].depth_variant == "metric" |
|
|
|
|
| def test_relative_adapter_is_registered(): |
| assert adapters._ADAPTERS["DA3-LARGE-1.1"].depth_variant == "relative" |
|
|
|
|
| @pytest.mark.parametrize("algorithm", ["1", "2", "3", "4", "5"]) |
| def test_selector_algorithm_dispatch_is_registered(algorithm): |
| assert algorithm in adapters._SELECTOR_ALGORITHMS |
| assert callable(adapters._SELECTOR_ALGORITHMS[algorithm]) |
|
|
|
|
| def test_selector_algorithm_defaults_to_five(monkeypatch): |
| import importlib |
|
|
| monkeypatch.delenv("VSI_SELECTOR_ALGORITHM", raising=False) |
| reloaded = importlib.reload(adapters) |
| try: |
| assert reloaded.SELECTOR_ALGORITHM == "5" |
| finally: |
| importlib.reload(adapters) |
|
|
|
|
| def test_select_video_frame_indices_rejects_unknown_algorithm(monkeypatch, tmp_path): |
| monkeypatch.setattr(adapters, "SELECTOR_ALGORITHM", "99") |
| monkeypatch.setattr(adapters, "SELECTED_FRAMES_CACHE", tmp_path) |
| video = tmp_path / "scene.mp4" |
| video.write_bytes(b"fake video bytes") |
| with pytest.raises(ValueError, match="unknown selector algorithm"): |
| adapters.select_video_frame_indices(str(video)) |
|
|
|
|
| |
| |
| |
| |
| |
| _ALGORITHM_TUNABLE_CONSTANTS = { |
| "1": [ |
| "REDUNDANCY_SSIM_THRESHOLD", |
| "MINIMUM_ALIGNMENT_MATCHES", |
| "MINIMUM_ALIGNMENT_INLIER_RATIO", |
| "MINIMUM_VALID_OVERLAP_FRACTION", |
| ], |
| "2": [ |
| "BLUR_RELATIVE_MEDIAN_FRACTION", |
| "BLUR_ABSOLUTE_FLOOR", |
| "BLUR_CANONICAL_WIDTH", |
| "DARK_MEAN_THRESHOLD", |
| "BRIGHT_MEAN_THRESHOLD", |
| "LOW_CONTRAST_STD_THRESHOLD", |
| ], |
| "3": [ |
| "BLACK_PIXEL_LUMINANCE_THRESHOLD", |
| "BLACK_FRAME_PIXEL_RATIO_THRESHOLD", |
| "GLITCH_MAX_NEIGHBOR_COVISIBILITY", |
| "GLITCH_THUMBNAIL_WIDTH", |
| "GLITCH_MINIMUM_OWN_KEYPOINTS", |
| "MINIMUM_ALIGNMENT_MATCHES", |
| ], |
| "4": [ |
| "COVISIBILITY_OVERLAP_THRESHOLD", |
| "MINIMUM_ALIGNMENT_MATCHES", |
| "REDUNDANCY_SSIM_THRESHOLD", |
| ], |
| "5": [ |
| "BLUR_RELATIVE_MEDIAN_FRACTION", |
| "BLUR_ABSOLUTE_FLOOR", |
| "BLUR_CANONICAL_WIDTH", |
| "DARK_MEAN_THRESHOLD", |
| "BRIGHT_MEAN_THRESHOLD", |
| "LOW_CONTRAST_STD_THRESHOLD", |
| "COVISIBILITY_OVERLAP_THRESHOLD", |
| "MINIMUM_ALIGNMENT_MATCHES", |
| "REDUNDANCY_SSIM_THRESHOLD", |
| ], |
| } |
|
|
|
|
| @pytest.mark.parametrize( |
| ("algorithm", "constant_name"), |
| [ |
| (algorithm, name) |
| for algorithm, names in _ALGORITHM_TUNABLE_CONSTANTS.items() |
| for name in names |
| ], |
| ) |
| def test_selector_config_reflects_every_tunable_constant( |
| monkeypatch, algorithm, constant_name |
| ): |
| monkeypatch.setattr(adapters, "SELECTOR_ALGORITHM", algorithm) |
| before = adapters._selector_config() |
| original_value = getattr(adapters, constant_name) |
| monkeypatch.setattr(adapters, constant_name, original_value * 2 + 1) |
| after = adapters._selector_config() |
| assert after != before |
|
|
|
|
| def test_cache_is_invalidated_when_selector_config_changes(tmp_path, monkeypatch): |
| monkeypatch.setattr(adapters, "SELECTED_FRAMES_CACHE", tmp_path) |
| monkeypatch.setattr(adapters, "SELECTOR_ALGORITHM", "2") |
| video = tmp_path / "scene.mp4" |
| video.write_bytes(b"fake video bytes") |
|
|
| adapters._cache_selected_frame_indices(str(video), [1, 2, 3]) |
| assert adapters._load_selected_frame_indices(str(video)) == [1, 2, 3] |
|
|
| monkeypatch.setattr( |
| adapters, "BLUR_ABSOLUTE_FLOOR", adapters.BLUR_ABSOLUTE_FLOOR + 5 |
| ) |
| assert adapters._load_selected_frame_indices(str(video)) is None |
|
|
|
|
| def _write_synthetic_video(path, frames, fps=10): |
| cv2 = pytest.importorskip("cv2") |
| height, width = frames[0].shape[:2] |
| writer = cv2.VideoWriter( |
| str(path), cv2.VideoWriter_fourcc(*"mp4v"), fps, (width, height) |
| ) |
| for frame in frames: |
| writer.write(frame) |
| writer.release() |
|
|
|
|
| def _textured_frame(shape=(90, 160, 3), seed=0, circles=40): |
| cv2 = pytest.importorskip("cv2") |
| rng = np.random.default_rng(seed) |
| frame = np.zeros(shape, np.uint8) |
| for _ in range(circles): |
| x, y = int(rng.integers(0, shape[1])), int(rng.integers(0, shape[0])) |
| radius = int(rng.integers(3, 8)) |
| color = tuple(int(value) for value in rng.integers(50, 255, 3)) |
| cv2.circle(frame, (x, y), radius, color, -1) |
| return frame |
|
|
|
|
| def test_algorithm_3_catches_injected_corruption_without_false_positives(tmp_path): |
| cv2 = pytest.importorskip("cv2") |
| if not hasattr(cv2, "VideoWriter"): |
| pytest.skip("real OpenCV video I/O is not installed") |
| base = _textured_frame(seed=0) |
| rng = np.random.default_rng(1) |
| corrupt_frames = {20, 45} |
| frames = [ |
| ( |
| rng.integers(0, 255, base.shape, dtype=np.uint8) |
| if i in corrupt_frames |
| else base |
| ) |
| for i in range(60) |
| ] |
| path = tmp_path / "synthetic_corrupt.mp4" |
| _write_synthetic_video(path, frames) |
|
|
| kept = set(adapters._select_indices_algorithm_3(str(path))) |
| discarded = set(range(len(frames))) - kept |
|
|
| assert corrupt_frames.issubset(discarded) |
| assert discarded - corrupt_frames == set() |
|
|
|
|
| def test_algorithm_4_compresses_static_redundancy_at_least_as_well_as_algorithm_1( |
| tmp_path, |
| ): |
| cv2 = pytest.importorskip("cv2") |
| if not hasattr(cv2, "VideoWriter"): |
| pytest.skip("real OpenCV video I/O is not installed") |
| base = _textured_frame(seed=2) |
| rng = np.random.default_rng(3) |
| |
| |
| frames = [ |
| np.clip(base.astype(np.int16) + rng.integers(-3, 3, base.shape), 0, 255).astype( |
| np.uint8 |
| ) |
| for _ in range(80) |
| ] |
| path = tmp_path / "static_scene.mp4" |
| _write_synthetic_video(path, frames) |
|
|
| kept1 = adapters._select_indices_algorithm_1(str(path)) |
| kept4 = adapters._select_indices_algorithm_4(str(path)) |
|
|
| assert len(kept4) <= len(kept1) |
|
|
|
|
| def test_algorithm_5_output_is_subset_of_algorithm_2_output(tmp_path): |
| cv2 = pytest.importorskip("cv2") |
| if not hasattr(cv2, "VideoWriter"): |
| pytest.skip("real OpenCV video I/O is not installed") |
| base = _textured_frame(seed=4) |
| frames = [base for _ in range(60)] |
| path = tmp_path / "scene.mp4" |
| _write_synthetic_video(path, frames) |
|
|
| kept2 = set(adapters._select_indices_algorithm_2(str(path))) |
| kept5 = set(adapters._select_indices_algorithm_5(str(path))) |
|
|
| assert kept5.issubset(kept2) |
|
|