File size: 2,639 Bytes
e8055cf | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 | """Tests for harness/A/frames.py -- uniform/selective frame sampling."""
import numpy as np
import pytest
from harness.A import frames as frame_sampling
def test_sample_frames_rejects_unknown_selection(tmp_path):
video = tmp_path / "scene.mp4"
video.write_bytes(b"not a real video")
with pytest.raises(ValueError):
frame_sampling.sample_frames(str(video), 8, "random")
def test_sample_frames_rejects_nonpositive_frame_count(tmp_path):
video = tmp_path / "scene.mp4"
video.write_bytes(b"not a real video")
with pytest.raises(ValueError):
frame_sampling.sample_frames(str(video), 0, "uniform")
def test_sample_frames_rejects_missing_video(tmp_path):
with pytest.raises(FileNotFoundError):
frame_sampling.sample_frames(str(tmp_path / "missing.mp4"), 8, "uniform")
def test_sample_frames_returns_pil_images_in_order(tmp_path, monkeypatch):
video = tmp_path / "scene.mp4"
video.write_bytes(b"not a real video")
fake_frames = np.stack(
[np.full((4, 4, 3), value, dtype=np.uint8) for value in (10, 20, 30)]
)
monkeypatch.setattr(
frame_sampling,
"_sample_video_frames",
lambda path, count, selection: (fake_frames, np.array([0.0, 1.0, 2.0])),
)
class _UnreadableCapture:
def get(self, prop):
return 0.0
def release(self):
pass
monkeypatch.setattr(
frame_sampling.cv2, "VideoCapture", lambda path: _UnreadableCapture()
)
result, timestamps, indices = frame_sampling.sample_frames(str(video), 3, "uniform")
assert len(result) == 3
assert np.array(result[0])[0, 0, 0] == 10
assert np.array(result[2])[0, 0, 0] == 30
assert timestamps == [0.0, 1.0, 2.0]
# fps falls back to 1.0 for the fake (unreadable) video, so index == round(t * 1.0) == t.
assert indices == [0, 1, 2]
def test_sample_frames_derives_indices_from_real_fps(tmp_path, monkeypatch):
video = tmp_path / "scene.mp4"
video.write_bytes(b"not a real video")
fake_frames = np.stack([np.full((2, 2, 3), 1, dtype=np.uint8)] * 3)
monkeypatch.setattr(
frame_sampling,
"_sample_video_frames",
lambda path, count, selection: (fake_frames, np.array([0.0, 0.5, 1.0])),
)
class _FakeCapture:
def get(self, prop):
return 30.0
def release(self):
pass
monkeypatch.setattr(frame_sampling.cv2, "VideoCapture", lambda path: _FakeCapture())
_, timestamps, indices = frame_sampling.sample_frames(str(video), 3, "uniform")
assert timestamps == [0.0, 0.5, 1.0]
assert indices == [0, 15, 30]
|