🐛 Match capture metadata by recording camera and part identity
This commit is contained in:
parent
cc279adf00
commit
554556f19c
@ -141,6 +141,40 @@ def _validate_raw_imu(path: pathlib.Path, start: int, end: int) -> None:
|
||||
raise RuntimeError(f"原始 IMU {kind} 未覆盖视频 PTS 时间范围")
|
||||
|
||||
|
||||
def _segment_identity(filename: str) -> tuple[str, str, int] | None:
|
||||
"""Transcoding changes suffixes, not Capture/camera/part identity."""
|
||||
stem = pathlib.PurePosixPath(filename).stem
|
||||
match = re.fullmatch(r"(.+)_camera_(left|right)_part(\d+)(?:_.*)?", stem, re.IGNORECASE)
|
||||
if not match:
|
||||
return None
|
||||
return match.group(1), match.group(2).lower(), int(match.group(3))
|
||||
|
||||
|
||||
def _meta_frame_count(metadata: dict[str, Any], video_path: str) -> int:
|
||||
identity = _segment_identity(video_path)
|
||||
if identity is None:
|
||||
raise RuntimeError(f"采集 meta 无法识别视频身份: {video_path}")
|
||||
candidates = metadata.get("streams", {}).get("color_" + identity[1], {}).get("segments", [])
|
||||
records = [item for item in candidates if isinstance(item, dict)
|
||||
and str(item.get("index", "")).isdigit() and int(item["index"]) == identity[2]]
|
||||
# Old meta may contain index/count only. Keep that path only when none of
|
||||
# this part's records provides a filename; never use anonymous records to
|
||||
# override an explicit but different Capture/camera/file identity.
|
||||
if any(item.get("file") or item.get("pts_file") for item in records):
|
||||
records = [item for item in records
|
||||
if (item.get("file") or item.get("pts_file"))
|
||||
and all(isinstance(value, str) and _segment_identity(value) == identity
|
||||
for value in (item.get("file"), item.get("pts_file")) if value)]
|
||||
if not records:
|
||||
raise RuntimeError(f"采集 meta 帧数记录匹配失败: {video_path},未找到对应采集、相机和 part")
|
||||
if len(records) != 1:
|
||||
raise RuntimeError(f"采集 meta 帧数记录不唯一: {video_path},匹配 {len(records)} 条")
|
||||
count = records[0].get("frame_count")
|
||||
if not str(count).isdigit() or int(count) <= 0:
|
||||
raise RuntimeError(f"采集 meta 帧数记录无效: {video_path}")
|
||||
return int(count)
|
||||
|
||||
|
||||
def _validate_fallback_pair(path, paired, entries, source_path, output_dir, params, helpers):
|
||||
pts = paired.get("pts", "")
|
||||
raw_human = params.get("PROCESSING_PROFILE") == "raw_human"
|
||||
@ -167,12 +201,7 @@ def _validate_fallback_pair(path, paired, entries, source_path, output_dir, para
|
||||
raise RuntimeError("采集 meta 不唯一")
|
||||
if metas:
|
||||
metadata = json.loads(materialize(metas[0]).read_text(encoding="utf-8-sig"))
|
||||
segment = re.search(r"_part(\d+)", video.stem, re.IGNORECASE)
|
||||
candidates = metadata.get("streams", {}).get("color_" + _camera_side(path), {}).get("segments", [])
|
||||
records = [item for item in candidates if segment and int(item.get("index", -1)) == int(segment.group(1))]
|
||||
if len(records) != 1 or "frame_count" not in records[0]:
|
||||
raise RuntimeError("采集 meta 缺少对应相机、part 的帧数记录")
|
||||
expected = int(records[0]["frame_count"])
|
||||
expected = _meta_frame_count(metadata, path)
|
||||
validation = _validate_transcoded_pts(materialize(path), materialize(pts), expected)
|
||||
if raw_human:
|
||||
if not paired.get("imu"):
|
||||
|
||||
@ -12,3 +12,19 @@ There is no manual frame-order declaration gate. `TRANSCODE_PRESERVES_FRAME_ORDE
|
||||
Original device timestamps are never generated, rescaled or rewritten. Unequal counts, invalid timestamps, decoder errors and inconsistent meta remain blocking errors. Original/exact-name PTS follows the existing downstream validation path. Successful validation records count/timestamp checks only, not a claim that frame order was independently verified.
|
||||
|
||||
Deploy the updated normalizer image; no workflow flag is needed. Existing failed Runs are not automatically retried by an image update.
|
||||
|
||||
## Meta records from multiple recordings
|
||||
|
||||
A meta file may contain several records with the same part index from different
|
||||
Captures. Resolve the selected stream record by Capture filename identity, camera
|
||||
side and part number, ignoring the video's transcoding suffix. When both `file`
|
||||
and `pts_file` are present, both must agree with that identity. Do not select the
|
||||
first record, sum counts, or borrow another Capture's frame count.
|
||||
|
||||
Legacy metadata with only index/count remains supported only when no record for
|
||||
that part provides file identity and exactly one record matches. Missing matches,
|
||||
duplicate matching identities, invalid frame counts and video/PTS/meta count
|
||||
mismatches remain errors. These conditions now have distinct error messages.
|
||||
This change never edits source metadata or timestamps, and does not automatically
|
||||
retry existing failed runs. It requires a normalizer image release; deploying the
|
||||
API/dashboard alone does not update the normalizer used by workflow jobs.
|
||||
|
||||
@ -141,6 +141,40 @@ def _validate_raw_imu(path: pathlib.Path, start: int, end: int) -> None:
|
||||
raise RuntimeError(f"原始 IMU {kind} 未覆盖视频 PTS 时间范围")
|
||||
|
||||
|
||||
def _segment_identity(filename: str) -> tuple[str, str, int] | None:
|
||||
"""Transcoding changes suffixes, not Capture/camera/part identity."""
|
||||
stem = pathlib.PurePosixPath(filename).stem
|
||||
match = re.fullmatch(r"(.+)_camera_(left|right)_part(\d+)(?:_.*)?", stem, re.IGNORECASE)
|
||||
if not match:
|
||||
return None
|
||||
return match.group(1), match.group(2).lower(), int(match.group(3))
|
||||
|
||||
|
||||
def _meta_frame_count(metadata: dict[str, Any], video_path: str) -> int:
|
||||
identity = _segment_identity(video_path)
|
||||
if identity is None:
|
||||
raise RuntimeError(f"采集 meta 无法识别视频身份: {video_path}")
|
||||
candidates = metadata.get("streams", {}).get("color_" + identity[1], {}).get("segments", [])
|
||||
records = [item for item in candidates if isinstance(item, dict)
|
||||
and str(item.get("index", "")).isdigit() and int(item["index"]) == identity[2]]
|
||||
# Old meta may contain index/count only. Keep that path only when none of
|
||||
# this part's records provides a filename; never use anonymous records to
|
||||
# override an explicit but different Capture/camera/file identity.
|
||||
if any(item.get("file") or item.get("pts_file") for item in records):
|
||||
records = [item for item in records
|
||||
if (item.get("file") or item.get("pts_file"))
|
||||
and all(isinstance(value, str) and _segment_identity(value) == identity
|
||||
for value in (item.get("file"), item.get("pts_file")) if value)]
|
||||
if not records:
|
||||
raise RuntimeError(f"采集 meta 帧数记录匹配失败: {video_path},未找到对应采集、相机和 part")
|
||||
if len(records) != 1:
|
||||
raise RuntimeError(f"采集 meta 帧数记录不唯一: {video_path},匹配 {len(records)} 条")
|
||||
count = records[0].get("frame_count")
|
||||
if not str(count).isdigit() or int(count) <= 0:
|
||||
raise RuntimeError(f"采集 meta 帧数记录无效: {video_path}")
|
||||
return int(count)
|
||||
|
||||
|
||||
def _validate_fallback_pair(path, paired, entries, source_path, output_dir, params, helpers):
|
||||
pts = paired.get("pts", "")
|
||||
raw_human = params.get("PROCESSING_PROFILE") == "raw_human"
|
||||
@ -167,12 +201,7 @@ def _validate_fallback_pair(path, paired, entries, source_path, output_dir, para
|
||||
raise RuntimeError("采集 meta 不唯一")
|
||||
if metas:
|
||||
metadata = json.loads(materialize(metas[0]).read_text(encoding="utf-8-sig"))
|
||||
segment = re.search(r"_part(\d+)", video.stem, re.IGNORECASE)
|
||||
candidates = metadata.get("streams", {}).get("color_" + _camera_side(path), {}).get("segments", [])
|
||||
records = [item for item in candidates if segment and int(item.get("index", -1)) == int(segment.group(1))]
|
||||
if len(records) != 1 or "frame_count" not in records[0]:
|
||||
raise RuntimeError("采集 meta 缺少对应相机、part 的帧数记录")
|
||||
expected = int(records[0]["frame_count"])
|
||||
expected = _meta_frame_count(metadata, path)
|
||||
validation = _validate_transcoded_pts(materialize(path), materialize(pts), expected)
|
||||
if raw_human:
|
||||
if not paired.get("imu"):
|
||||
|
||||
@ -63,3 +63,89 @@ def test_raw_profile_bypasses_imu_gate_for_real_part_boundary(tmp_path, monkeypa
|
||||
pairs["imu"] = ""
|
||||
with pytest.raises(RuntimeError, match="缺少 IMU"):
|
||||
validate()
|
||||
|
||||
|
||||
def _meta_fixture(tmp_path, segments, side="left"):
|
||||
name = f"part0001/capture_camera_{side}_part0001_silent_480p_wm.mp4"
|
||||
contents = {
|
||||
name: "video",
|
||||
f"part0001/capture_camera_{side}_part0001_pts.csv": "timestamp_us\n100\n200\n",
|
||||
"part0001/capture_imu_part0001.csv": "original imu",
|
||||
"meta_capture.json": json.dumps({"streams": {f"color_{side}": {"segments": segments}}}),
|
||||
}
|
||||
entries = []
|
||||
for filename, text in contents.items():
|
||||
path = tmp_path / filename
|
||||
path.parent.mkdir(parents=True, exist_ok=True)
|
||||
path.write_text(text)
|
||||
entries.append({"path": filename, "source": {"path": str(path)}})
|
||||
return name, entries, contents
|
||||
|
||||
|
||||
@pytest.mark.parametrize("side", ["left", "right"])
|
||||
@pytest.mark.parametrize("reverse", [False, True])
|
||||
def test_meta_selects_capture_identity_not_duplicate_part_index(tmp_path, monkeypatch, side, reverse):
|
||||
segments = [
|
||||
{"index": 1, "file": f"part0001/previous_camera_{side}_part0001.mp4", "frame_count": 7890},
|
||||
{"index": 1, "file": f"part0001/capture_camera_{side}_part0001.mp4", "pts_file": f"part0001/capture_camera_{side}_part0001_pts.csv", "frame_count": 2},
|
||||
]
|
||||
name, entries, contents = _meta_fixture(tmp_path, segments[::-1] if reverse else segments, side)
|
||||
monkeypatch.setattr(op.subprocess, "run", lambda *a, **k: SimpleNamespace(stdout='{"streams":[{"nb_read_frames":"2"}]}', stderr=""))
|
||||
result = op._validate_fallback_pair(name, op._paired_files(name, entries), entries, str(tmp_path), tmp_path, {"PROCESSING_PROFILE": "raw_human"}, {})
|
||||
assert result["ptsRows"] == 2
|
||||
assert all((tmp_path / file).read_text() == text for file, text in contents.items())
|
||||
|
||||
|
||||
@pytest.mark.parametrize("segments,error", [
|
||||
([{"index": 1, "file": "other_camera_left_part0001.mp4", "frame_count": 2}], "匹配失败"),
|
||||
([{"index": 1, "file": "capture_camera_right_part0001.mp4", "frame_count": 2}], "匹配失败"),
|
||||
([{"index": 1, "file": "capture_camera_left_part0002.mp4", "frame_count": 2}], "匹配失败"),
|
||||
([{"index": 1, "file": "capture_camera_left_part0001.mp4", "pts_file": "other_camera_left_part0001_pts.csv", "frame_count": 2}], "匹配失败"),
|
||||
([{"index": 1, "file": "capture_camera_left_part0001.mp4", "frame_count": 2}] * 2, "不唯一"),
|
||||
([{"index": 1, "frame_count": 2}] * 2, "不唯一"),
|
||||
([{"index": 1, "file": "other_camera_left_part0001.mp4", "frame_count": 2}, {"index": 1, "frame_count": 2}], "匹配失败"),
|
||||
([{"index": 1, "file": "capture_camera_left_part0001.mp4", "frame_count": 3}], "meta 与 PTS"),
|
||||
])
|
||||
def test_meta_does_not_guess_or_bypass_validation(tmp_path, monkeypatch, segments, error):
|
||||
name, entries, _ = _meta_fixture(tmp_path, segments)
|
||||
monkeypatch.setattr(op.subprocess, "run", lambda *a, **k: SimpleNamespace(stdout='{"streams":[{"nb_read_frames":"2"}]}', stderr=""))
|
||||
with pytest.raises(RuntimeError, match=error):
|
||||
op._validate_fallback_pair(name, op._paired_files(name, entries), entries, str(tmp_path), tmp_path, {"PROCESSING_PROFILE": "raw_human"}, {})
|
||||
|
||||
|
||||
def test_meta_identity_with_real_video_decode(tmp_path):
|
||||
import shutil
|
||||
import subprocess
|
||||
if not shutil.which("ffmpeg") or not shutil.which("ffprobe"):
|
||||
pytest.skip("ffmpeg/ffprobe required")
|
||||
name, entries, contents = _meta_fixture(tmp_path, [
|
||||
{"index": 1, "file": "old_camera_left_part0001.mp4", "frame_count": 7890},
|
||||
{"index": 1, "file": "capture_camera_left_part0001.mp4", "frame_count": 2},
|
||||
])
|
||||
subprocess.run(["ffmpeg", "-v", "error", "-y", "-f", "lavfi", "-i", "color=c=black:s=32x32:r=30", "-frames:v", "2", "-c:v", "mpeg4", str(tmp_path / name)], check=True)
|
||||
result = op._validate_fallback_pair(name, op._paired_files(name, entries), entries, str(tmp_path), tmp_path, {"PROCESSING_PROFILE": "raw_human"}, {})
|
||||
assert result["videoFrames"] == result["ptsRows"] == 2
|
||||
for file, text in contents.items():
|
||||
if file != name:
|
||||
assert (tmp_path / file).read_text() == text
|
||||
|
||||
|
||||
def test_meta_pts_identity_and_legacy_compatibility():
|
||||
video = "part0001/capture_camera_left_part0001_silent_480p_wm.mp4"
|
||||
for records in [
|
||||
[{"index": "1", "frame_count": "2"}],
|
||||
[{"index": 1, "pts_file": "part0001/capture_camera_left_part0001_pts.csv", "frame_count": 2}],
|
||||
]:
|
||||
assert op._meta_frame_count({"streams": {"color_left": {"segments": records}}}, video) == 2
|
||||
|
||||
|
||||
@pytest.mark.parametrize("count", [None, True, False, 0, -1, 2.5, "2.5", "bad"])
|
||||
def test_invalid_meta_frame_count_stays_blocking(count):
|
||||
with pytest.raises(RuntimeError, match="帧数记录无效"):
|
||||
op._meta_frame_count({"streams": {"color_left": {"segments": [{"index": 1, "frame_count": count}]}}}, "capture_camera_left_part0001.mp4")
|
||||
|
||||
|
||||
def test_runtime_repository_matches_backend_normalizer():
|
||||
from pathlib import Path
|
||||
root = Path(__file__).resolve().parents[2]
|
||||
assert (root / "operator-repos/builtin/orbbec_data_normalize/src/operator.py").read_bytes() == Path(op.__file__).read_bytes()
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user