Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 19 additions & 3 deletions src/hflow/camera_video.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@
- label ``<topic>/video_frame_count``: how many access units were muxed.

A declared camera with no messages gets only ``<topic>/video_frame_count`` of
0: no MP4 artifact, no ``video_fps``, and no ``video_start_s``.
0: no MP4 artifact, no ``video_fps``, and no ``video_start_s``. A camera with
a single message gets ``<topic>/video_frame_count`` of 1: no MP4 artifact (no
inter-frame interval to mux), no ``video_fps``, but ``video_start_s`` when the
episode records time bounds.

The MP4 plays at a constant rate, so a stream with irregular stamps drifts
from log time by its jitter; the ``timestamp_regularity`` check measures that
Expand All @@ -47,7 +50,9 @@
# artifact names, label keys, or the muxed file's timing model change.
# "2": the per-camera MP4 cache name gained its collision-resistant topic
# digest (#535), so the published artifact URIs change.
CAMERA_VIDEO_VERSION = "2"
# "3": single-frame cameras now record video_frame_count=1 and video_start_s
# without fps or an MP4 artifact instead of failing (#774).
CAMERA_VIDEO_VERSION = "3"

VIDEO_ARTIFACT_PREFIX = "video:"

Expand Down Expand Up @@ -75,6 +80,17 @@ def camera_video(episode: Episode) -> EnrichmentResult:
# record the zero count and publish no video for it.
labels[f"{camera_topic}/video_frame_count"] = 0
continue
if len(channel) == 1:
# A single-frame stream has no inter-frame interval, so fps cannot
# be estimated and no MP4 can be muxed. Record the count and, when
# the episode records time bounds, the offset of that one frame.
labels[f"{camera_topic}/video_frame_count"] = 1
if time_bounds is not None:
first_frame_offset_ns = int(channel.timestamps[0]) - time_bounds.start_ns
labels[f"{camera_topic}/video_start_s"] = (
first_frame_offset_ns / NANOSECONDS_PER_SECOND
)
continue
mp4_path = episode.video(camera_topic)
artifacts[video_artifact_name(camera_topic)] = mp4_path
# The same estimate Episode.video() muxed at, so the label is the
Expand All @@ -83,7 +99,7 @@ def camera_video(episode: Episode) -> EnrichmentResult:
channel.timestamps.tolist(), topic=camera_topic
)
labels[f"{camera_topic}/video_frame_count"] = len(channel)
if time_bounds is not None and len(channel) > 0:
if time_bounds is not None:
first_frame_offset_ns = int(channel.timestamps[0]) - time_bounds.start_ns
labels[f"{camera_topic}/video_start_s"] = first_frame_offset_ns / NANOSECONDS_PER_SECOND
return EnrichmentResult(labels=labels, artifacts=artifacts)
23 changes: 22 additions & 1 deletion tests/test_camera_video.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,27 @@ def test_camera_video_on_a_camera_less_episode_records_nothing(tmp_path: Path) -

report = asyncio.run(app.test(source, verbose=False))

(video_run,) = report.enrichments
(video_run,) = [run for run in report.enrichments if run.enrichment.name == "camera_video"]
assert video_run.status is hflow.CheckStatus.MEASURED, video_run.error
assert video_run.result == hflow.EnrichmentResult()


def test_camera_video_on_a_single_frame_camera_records_count_without_fps_or_mp4(
tmp_path: Path,
) -> None:
app = hflow.App("camera-video-single-frame", data_root=tmp_path / "data")
app.enrich(version=CAMERA_VIDEO_VERSION)(camera_video)
source = synthesize_episode(
tmp_path / "episode.mcap",
SyntheticEpisodeSpec(duration_s=1.0, cameras=("wrist_cam",), image_hz=1.0),
)

report = asyncio.run(app.test(source, verbose=False))

(video_run,) = [run for run in report.enrichments if run.enrichment.name == "camera_video"]
assert video_run.status is hflow.CheckStatus.MEASURED, video_run.error
assert video_run.result is not None
assert video_run.result.labels["/wrist_cam/compressed/video_frame_count"] == 1
assert "/wrist_cam/compressed/video_fps" not in video_run.result.labels
assert "/wrist_cam/compressed/video_start_s" in video_run.result.labels
assert not video_run.result.artifacts
Loading