Skip to content

Commit 56004b7

Browse files
fix(measurements): align rgb_frames sampling with output ticks (#733) (#734)
* fix(measurements): align rgb_frames sampling with output ticks (#733) Add ':round=up' to the fps filter in rgb_frames so that the emitted frame is the one visible at-or-before each output tick, matching the expectation of source_log_times_for_sampled_frames and mediapipe_hands. * test(ffmpeg): verify expected frame count and non-dividing rate in rgb_frames sampling - Check expected number of frames (int(3 * sample_fps)) instead of len(shown) - Add 4.0 fps to parametrize to verify frame selection between source frames - Use floor-based attribution matching source_log_times_for_sampled_frames Refs #733 --------- Co-authored-by: shobhitagnihotri69 <shobhitagnihotri69@users.noreply.github.com>
1 parent 82c4cc7 commit 56004b7

2 files changed

Lines changed: 76 additions & 1 deletion

File tree

‎src/hflow/_video_measurements/_raw_frames.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ def rgb_frames(
170170
)
171171
filters: list[str] = []
172172
if frames_per_second is not None:
173-
filters.append(f"fps={frames_per_second:g}")
173+
filters.append(f"fps={frames_per_second:g}:round=up")
174174
if long_edge_pixels is not None:
175175
filters.append(f"scale={frame_width}:{frame_height}:flags=bilinear")
176176
filters.append("format=pix_fmts=rgb24")

‎tests/test_ffmpeg.py‎

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -793,6 +793,81 @@ def test_rgb_frames_reject_a_non_positive_rate(black_tail_video: Path) -> None:
793793
pass
794794

795795

796+
@pytest.fixture(scope="module")
797+
def numbered_30fps_video(tmp_path_factory: pytest.TempPathFactory) -> Path:
798+
"""3s video at 30 fps (90 frames) with each frame's index encoded as a binary barcode."""
799+
width, height, source_fps, frame_count, bits = 160, 120, 30, 90, 8
800+
801+
def draw_frame(index: int) -> np.ndarray:
802+
plane = np.full((height, width), 128, dtype=np.uint8)
803+
plane[:30] = 16
804+
for bit in range(bits):
805+
if (index >> (bits - 1 - bit)) & 1:
806+
plane[:30, bit * (width // bits) : (bit + 1) * (width // bits)] = 235
807+
return np.stack([plane, plane, plane], axis=-1)
808+
809+
video_path = tmp_path_factory.mktemp("videos") / "numbered_30fps.mp4"
810+
raw_bytes = b"".join(draw_frame(i).tobytes() for i in range(frame_count))
811+
run_ffmpeg(
812+
"-hide_banner",
813+
"-loglevel",
814+
"error",
815+
"-y",
816+
"-f",
817+
"rawvideo",
818+
"-pix_fmt",
819+
"rgb24",
820+
"-s",
821+
f"{width}x{height}",
822+
"-r",
823+
str(source_fps),
824+
"-i",
825+
"-",
826+
"-c:v",
827+
"libx264",
828+
"-pix_fmt",
829+
"yuv420p",
830+
"-g",
831+
"10",
832+
"-preset",
833+
"ultrafast",
834+
str(video_path),
835+
stdin=raw_bytes,
836+
)
837+
return video_path
838+
839+
840+
@pytest.mark.parametrize("sample_fps", [1.0, 3.0, 4.0, 10.0, 30.0])
841+
def test_rgb_frames_sampling_aligns_with_output_ticks(
842+
numbered_30fps_video: Path, sample_fps: float
843+
) -> None:
844+
"""rgb_frames must emit the frame visible at-or-before each output tick (round=up),
845+
matching source_log_times_for_sampled_frames attribution (#733).
846+
"""
847+
width, bits = 160, 8
848+
849+
def read_frame_index(rgb: np.ndarray) -> int:
850+
luma = rgb[:30].astype(np.float32).mean(axis=2)
851+
bit_width = width // bits
852+
return int(
853+
"".join(
854+
str(int(luma[:, bit * bit_width + 4 : (bit + 1) * bit_width - 4].mean() > 128))
855+
for bit in range(bits)
856+
),
857+
2,
858+
)
859+
860+
with rgb_frames(
861+
numbered_30fps_video,
862+
toolchain=resolved_video_measurement_toolchain(),
863+
frames_per_second=sample_fps,
864+
) as frames:
865+
shown = [read_frame_index(f) for f in frames]
866+
867+
expected = [int(i * 30 / sample_fps) for i in range(int(3 * sample_fps))]
868+
assert shown == expected
869+
870+
796871
@pytest.mark.parametrize(
797872
("source_shape", "long_edge_pixels", "expected"),
798873
[

0 commit comments

Comments
 (0)