| |
| from __future__ import annotations |
|
|
| import json |
| from pathlib import Path |
| from typing import Any, Callable |
|
|
|
|
| KNOWN_DATASET_NAMES = ( |
| "ARX-data", |
| "dex_fold_v2_mix", |
| "droid_lerobot", |
| "libero", |
| "libero_zty50", |
| "VLABench_5", |
| ) |
| KNOWN_VIDEO_SUFFIXES = {".mp4", ".avi", ".mov", ".mkv", ".webm"} |
| PORTABLE_IMAGE_ROOT = Path("_extracted_frames") |
| LEGACY_PORTABLE_IMAGE_ROOTS = ( |
| Path("extracted_frames"), |
| Path("progress_data") / "VLAC_preprocessed_data" / "data", |
| ) |
| GENERATED_MARKER_FILENAME = "_GENERATED" |
| PUBLIC_BENCHMARK_JSON_NAME = "video_progress_benchmark_file.json" |
| PUBLIC_RELEASE_ROOT_PLACEHOLDER = "__VLAC2_RELEASE_ROOT__" |
| PUBLIC_FRAMES_ROOT_PLACEHOLDER = "__VLAC2_FRAMES_ROOT__" |
| PUBLIC_BENCHMARK_DIRNAME = "benchmark_splits" |
| FULL_RELEASE_BENCHMARK_DIRNAME = "benchmark_style_all" |
| LEGACY_BENCHMARK_DIRNAME = "benchmark_json" |
|
|
|
|
| def portable_image_roots() -> tuple[Path, ...]: |
| roots = [PORTABLE_IMAGE_ROOT, *LEGACY_PORTABLE_IMAGE_ROOTS] |
| deduped: list[Path] = [] |
| seen: set[tuple[str, ...]] = set() |
| for root in roots: |
| key = root.parts |
| if key in seen: |
| continue |
| seen.add(key) |
| deduped.append(root) |
| return tuple(deduped) |
|
|
|
|
| def load_json(path: Path) -> Any: |
| return json.loads(path.read_text(encoding="utf-8")) |
|
|
|
|
| def dump_json(path: Path, payload: Any) -> None: |
| path.parent.mkdir(parents=True, exist_ok=True) |
| path.write_text(json.dumps(payload, ensure_ascii=False, indent=2) + "\n", encoding="utf-8") |
|
|
|
|
| def ensure_generated_marker(output_root: Path) -> Path: |
| output_root.mkdir(parents=True, exist_ok=True) |
| marker_path = output_root / GENERATED_MARKER_FILENAME |
| marker_path.write_text( |
| "This directory is generated by the VLAC2 public frame-extraction workflow.\n", |
| encoding="utf-8", |
| ) |
| return marker_path |
|
|
|
|
| def ensure_release_tree(release_root: Path) -> dict[str, Path]: |
| release_root = release_root.resolve() |
| paths = { |
| "release_root": release_root, |
| "raw_root": release_root / "data", |
| "benchmark_root": release_root / PUBLIC_BENCHMARK_DIRNAME, |
| "portable_image_root": release_root / PORTABLE_IMAGE_ROOT, |
| "scripts_root": release_root / "scripts", |
| } |
| for key in ("release_root", "raw_root", "benchmark_root", "scripts_root"): |
| paths[key].mkdir(parents=True, exist_ok=True) |
| return paths |
|
|
|
|
| def _normalized_parts(raw_path: str | Path) -> list[str]: |
| raw = str(raw_path or "").strip().replace("\\", "/") |
| if not raw: |
| raise ValueError("empty path") |
| return [part for part in Path(raw).parts if part not in ("", ".", "/")] |
|
|
|
|
| def dataset_relative_path(raw_path: str | Path) -> Path: |
| parts = _normalized_parts(raw_path) |
|
|
| for idx, part in enumerate(parts): |
| if part in KNOWN_DATASET_NAMES: |
| return Path(*parts[idx:]) |
|
|
| for portable_root in portable_image_roots(): |
| marker_parts = list(portable_root.parts) |
| marker_len = len(marker_parts) |
| for idx in range(max(0, len(parts) - marker_len + 1)): |
| if parts[idx : idx + marker_len] == marker_parts: |
| tail = parts[idx + marker_len :] |
| if not tail: |
| break |
| for tail_idx, part in enumerate(tail): |
| if part in KNOWN_DATASET_NAMES: |
| return Path(*tail[tail_idx:]) |
| return Path(*tail) |
|
|
| return Path(*parts) |
|
|
|
|
| def normalize_main_path(raw_main_path: str | Path) -> Path: |
| rel = dataset_relative_path(raw_main_path) |
| if rel.suffix.lower() in KNOWN_VIDEO_SUFFIXES: |
| return rel.with_suffix("") |
| return rel |
|
|
|
|
| def main_path_from_frame_path(raw_frame_path: str | Path) -> Path: |
| rel = dataset_relative_path(raw_frame_path) |
| if rel.suffix: |
| return rel.parent |
| return rel |
|
|
|
|
| def portable_frame_path_from_any(raw_frame_path: str | Path) -> Path: |
| return PORTABLE_IMAGE_ROOT / dataset_relative_path(raw_frame_path) |
|
|
|
|
| def placeholder_frame_path_from_any(raw_frame_path: str | Path) -> str: |
| return f"{PUBLIC_FRAMES_ROOT_PLACEHOLDER}/{dataset_relative_path(raw_frame_path).as_posix()}" |
|
|
|
|
| def absolute_frame_path_from_any(raw_frame_path: str | Path, frames_root: Path) -> Path: |
| raw = str(raw_frame_path or "").strip() |
| if not raw: |
| raise ValueError("empty frame path") |
| path = Path(raw) |
| if path.is_absolute(): |
| return path |
|
|
| parts = _normalized_parts(raw) |
| if parts and parts[0] in (PUBLIC_FRAMES_ROOT_PLACEHOLDER, PUBLIC_RELEASE_ROOT_PLACEHOLDER): |
| parts = parts[1:] |
|
|
| for portable_root in portable_image_roots(): |
| marker_parts = list(portable_root.parts) |
| if parts[: len(marker_parts)] == marker_parts: |
| return frames_root.resolve() / Path(*parts[len(marker_parts) :]) |
| if parts and parts[0] in KNOWN_DATASET_NAMES: |
| return frames_root.resolve() / Path(*parts) |
| return frames_root.resolve() / Path(*parts) |
|
|
|
|
| def rewrite_benchmark_image_paths( |
| rows: list[dict[str, Any]], |
| path_rewriter: Callable[[str], str], |
| ) -> list[dict[str, Any]]: |
| rewritten_rows: list[dict[str, Any]] = [] |
| for row in rows: |
| rewritten = dict(row) |
|
|
| frame_index = dict(row.get("frame_index") or {}) |
| if frame_index: |
| rewritten_frame_index: dict[str, dict[str, str]] = {} |
| for frame_idx, images in frame_index.items(): |
| image_map = dict(images or {}) |
| rewritten_frame_index[str(frame_idx)] = { |
| str(view): path_rewriter(str(image_path)) |
| for view, image_path in image_map.items() |
| if str(image_path or "").strip() |
| } |
| rewritten["frame_index"] = rewritten_frame_index |
|
|
| reference_context = row.get("reference_context") |
| if isinstance(reference_context, dict): |
| rewritten_ref = dict(reference_context) |
| anchors = list(reference_context.get("reference_anchors") or []) |
| rewritten_anchors: list[dict[str, Any]] = [] |
| for anchor in anchors: |
| rewritten_anchor = dict(anchor) |
| images = dict(anchor.get("images") or {}) |
| if images: |
| rewritten_anchor["images"] = { |
| str(view): path_rewriter(str(image_path)) |
| for view, image_path in images.items() |
| if str(image_path or "").strip() |
| } |
| rewritten_anchors.append(rewritten_anchor) |
| rewritten_ref["reference_anchors"] = rewritten_anchors |
| rewritten["reference_context"] = rewritten_ref |
|
|
| videos = row.get("videos") |
| if isinstance(videos, list): |
| rewritten_videos: list[Any] = [] |
| for item in videos: |
| if isinstance(item, list): |
| rewritten_videos.append( |
| [path_rewriter(str(path)) for path in item if str(path or "").strip()] |
| ) |
| elif isinstance(item, str) and item.strip(): |
| rewritten_videos.append(path_rewriter(item)) |
| rewritten["videos"] = rewritten_videos |
|
|
| images = row.get("images") |
| if isinstance(images, list): |
| rewritten["images"] = [ |
| path_rewriter(str(path)) |
| for path in images |
| if str(path or "").strip() |
| ] |
|
|
| rewritten_rows.append(rewritten) |
| return rewritten_rows |
|
|
|
|
| def infer_main_path_from_row(row: dict[str, Any]) -> Path | None: |
| for meta_key in ("metadata", "_meta", "meta"): |
| meta = row.get(meta_key) |
| if isinstance(meta, dict): |
| raw_main_path = str(meta.get("main_path") or "").strip() |
| if raw_main_path: |
| return normalize_main_path(raw_main_path) |
|
|
| frame_index = row.get("frame_index") |
| if isinstance(frame_index, dict): |
| for images in frame_index.values(): |
| if not isinstance(images, dict): |
| continue |
| for image_path in images.values(): |
| if str(image_path or "").strip(): |
| return main_path_from_frame_path(str(image_path)) |
|
|
| videos = row.get("videos") |
| if isinstance(videos, list) and videos: |
| first = videos[0] |
| if isinstance(first, list) and first: |
| return main_path_from_frame_path(str(first[0])) |
| if isinstance(first, str) and first.strip(): |
| return main_path_from_frame_path(first) |
|
|
| return None |
|
|
|
|
| def discover_benchmark_jsons(benchmark_root: Path) -> list[Path]: |
| benchmark_root = benchmark_root.resolve() |
| candidates: list[Path] = [] |
| seen: set[Path] = set() |
| for path in sorted(benchmark_root.glob(f"**/{PUBLIC_BENCHMARK_JSON_NAME}")): |
| resolved = path.resolve() |
| if resolved in seen: |
| continue |
| seen.add(resolved) |
| candidates.append(resolved) |
| return candidates |
|
|
|
|
| def resolve_benchmark_root(release_root: Path) -> Path: |
| release_root = release_root.resolve() |
| preferred = release_root / PUBLIC_BENCHMARK_DIRNAME |
| full_release = release_root / FULL_RELEASE_BENCHMARK_DIRNAME |
| legacy = release_root / LEGACY_BENCHMARK_DIRNAME |
| if preferred.exists(): |
| return preferred |
| if full_release.exists(): |
| return full_release |
| if legacy.exists(): |
| return legacy |
| return preferred |
|
|