| |
| """Merge completed per-video prediction shards in inventory order.""" |
|
|
| from __future__ import annotations |
|
|
| import argparse |
| import hashlib |
| import json |
| from pathlib import Path |
|
|
|
|
| def main() -> None: |
| parser = argparse.ArgumentParser(description=__doc__) |
| parser.add_argument("--inventory", type=Path, required=True) |
| parser.add_argument("--inference-dir", type=Path, required=True) |
| parser.add_argument("--output", type=Path, required=True) |
| parser.add_argument("--allow-incomplete", action="store_true") |
| args = parser.parse_args() |
|
|
| inventory = [json.loads(line) for line in args.inventory.read_text(encoding="utf-8").splitlines() if line.strip()] |
| missing = [] |
| total_frames = 0 |
| temporary = args.output.with_suffix(args.output.suffix + ".part") |
| args.output.parent.mkdir(parents=True, exist_ok=True) |
| with temporary.open("w", encoding="utf-8") as output: |
| for row in inventory: |
| if row.get("decode_status") != "ok": |
| continue |
| relative = str(row["relative_video_path"]) |
| digest = hashlib.sha1(relative.encode("utf-8")).hexdigest() |
| shard = args.inference_dir / "video_shards" / f"{digest}.jsonl" |
| done = args.inference_dir / "done" / f"{digest}.json" |
| if not shard.is_file() or not done.is_file(): |
| missing.append(relative) |
| continue |
| for line in shard.read_text(encoding="utf-8").splitlines(): |
| if line.strip(): |
| output.write(line + "\n") |
| total_frames += 1 |
| if missing and not args.allow_incomplete: |
| temporary.unlink(missing_ok=True) |
| raise SystemExit(f"missing {len(missing)} completed videos; first examples: {missing[:5]}") |
| temporary.replace(args.output) |
| summary = {"videos_in_inventory": len(inventory), "missing_videos": len(missing), "merged_frames": total_frames, "output": str(args.output.resolve())} |
| (args.output.parent / f"{args.output.name}.summary.json").write_text(json.dumps(summary, ensure_ascii=False, indent=2) + "\n", encoding="utf-8") |
| print(json.dumps(summary, ensure_ascii=False, indent=2)) |
|
|
|
|
| if __name__ == "__main__": |
| main() |
|
|
|
|