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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
from __future__ import annotations
|
| 3 |
+
|
| 4 |
+
import json
|
| 5 |
+
from pathlib import Path
|
| 6 |
+
from typing import Any, Callable
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
KNOWN_DATASET_NAMES = (
|
| 10 |
+
"ARX-data",
|
| 11 |
+
"dex_fold_v2_mix",
|
| 12 |
+
"droid_lerobot",
|
| 13 |
+
"libero",
|
| 14 |
+
"libero_zty50",
|
| 15 |
+
"VLABench_5",
|
| 16 |
+
)
|
| 17 |
+
KNOWN_VIDEO_SUFFIXES = {".mp4", ".avi", ".mov", ".mkv", ".webm"}
|
| 18 |
+
PORTABLE_IMAGE_ROOT = Path("_extracted_frames")
|
| 19 |
+
LEGACY_PORTABLE_IMAGE_ROOTS = (
|
| 20 |
+
Path("extracted_frames"),
|
| 21 |
+
Path("progress_data") / "VLAC_preprocessed_data" / "data",
|
| 22 |
+
)
|
| 23 |
+
GENERATED_MARKER_FILENAME = "_GENERATED"
|
| 24 |
+
PUBLIC_BENCHMARK_JSON_NAME = "video_progress_benchmark_file.json"
|
| 25 |
+
PUBLIC_RELEASE_ROOT_PLACEHOLDER = "__VLAC2_RELEASE_ROOT__"
|
| 26 |
+
PUBLIC_FRAMES_ROOT_PLACEHOLDER = "__VLAC2_FRAMES_ROOT__"
|
| 27 |
+
PUBLIC_BENCHMARK_DIRNAME = "benchmark_splits"
|
| 28 |
+
FULL_RELEASE_BENCHMARK_DIRNAME = "benchmark_style_all"
|
| 29 |
+
LEGACY_BENCHMARK_DIRNAME = "benchmark_json"
|
| 30 |
+
|
| 31 |
+
|
| 32 |
+
def portable_image_roots() -> tuple[Path, ...]:
|
| 33 |
+
roots = [PORTABLE_IMAGE_ROOT, *LEGACY_PORTABLE_IMAGE_ROOTS]
|
| 34 |
+
deduped: list[Path] = []
|
| 35 |
+
seen: set[tuple[str, ...]] = set()
|
| 36 |
+
for root in roots:
|
| 37 |
+
key = root.parts
|
| 38 |
+
if key in seen:
|
| 39 |
+
continue
|
| 40 |
+
seen.add(key)
|
| 41 |
+
deduped.append(root)
|
| 42 |
+
return tuple(deduped)
|
| 43 |
+
|
| 44 |
+
|
| 45 |
+
def load_json(path: Path) -> Any:
|
| 46 |
+
return json.loads(path.read_text(encoding="utf-8"))
|
| 47 |
+
|
| 48 |
+
|
| 49 |
+
def dump_json(path: Path, payload: Any) -> None:
|
| 50 |
+
path.parent.mkdir(parents=True, exist_ok=True)
|
| 51 |
+
path.write_text(json.dumps(payload, ensure_ascii=False, indent=2) + "\n", encoding="utf-8")
|
| 52 |
+
|
| 53 |
+
|
| 54 |
+
def ensure_generated_marker(output_root: Path) -> Path:
|
| 55 |
+
output_root.mkdir(parents=True, exist_ok=True)
|
| 56 |
+
marker_path = output_root / GENERATED_MARKER_FILENAME
|
| 57 |
+
marker_path.write_text(
|
| 58 |
+
"This directory is generated by the VLAC2 public frame-extraction workflow.\n",
|
| 59 |
+
encoding="utf-8",
|
| 60 |
+
)
|
| 61 |
+
return marker_path
|
| 62 |
+
|
| 63 |
+
|
| 64 |
+
def ensure_release_tree(release_root: Path) -> dict[str, Path]:
|
| 65 |
+
release_root = release_root.resolve()
|
| 66 |
+
paths = {
|
| 67 |
+
"release_root": release_root,
|
| 68 |
+
"raw_root": release_root / "data",
|
| 69 |
+
"benchmark_root": release_root / PUBLIC_BENCHMARK_DIRNAME,
|
| 70 |
+
"portable_image_root": release_root / PORTABLE_IMAGE_ROOT,
|
| 71 |
+
"scripts_root": release_root / "scripts",
|
| 72 |
+
}
|
| 73 |
+
for key in ("release_root", "raw_root", "benchmark_root", "scripts_root"):
|
| 74 |
+
paths[key].mkdir(parents=True, exist_ok=True)
|
| 75 |
+
return paths
|
| 76 |
+
|
| 77 |
+
|
| 78 |
+
def _normalized_parts(raw_path: str | Path) -> list[str]:
|
| 79 |
+
raw = str(raw_path or "").strip().replace("\\", "/")
|
| 80 |
+
if not raw:
|
| 81 |
+
raise ValueError("empty path")
|
| 82 |
+
return [part for part in Path(raw).parts if part not in ("", ".", "/")]
|
| 83 |
+
|
| 84 |
+
|
| 85 |
+
def dataset_relative_path(raw_path: str | Path) -> Path:
|
| 86 |
+
parts = _normalized_parts(raw_path)
|
| 87 |
+
|
| 88 |
+
for idx, part in enumerate(parts):
|
| 89 |
+
if part in KNOWN_DATASET_NAMES:
|
| 90 |
+
return Path(*parts[idx:])
|
| 91 |
+
|
| 92 |
+
for portable_root in portable_image_roots():
|
| 93 |
+
marker_parts = list(portable_root.parts)
|
| 94 |
+
marker_len = len(marker_parts)
|
| 95 |
+
for idx in range(max(0, len(parts) - marker_len + 1)):
|
| 96 |
+
if parts[idx : idx + marker_len] == marker_parts:
|
| 97 |
+
tail = parts[idx + marker_len :]
|
| 98 |
+
if not tail:
|
| 99 |
+
break
|
| 100 |
+
for tail_idx, part in enumerate(tail):
|
| 101 |
+
if part in KNOWN_DATASET_NAMES:
|
| 102 |
+
return Path(*tail[tail_idx:])
|
| 103 |
+
return Path(*tail)
|
| 104 |
+
|
| 105 |
+
return Path(*parts)
|
| 106 |
+
|
| 107 |
+
|
| 108 |
+
def normalize_main_path(raw_main_path: str | Path) -> Path:
|
| 109 |
+
rel = dataset_relative_path(raw_main_path)
|
| 110 |
+
if rel.suffix.lower() in KNOWN_VIDEO_SUFFIXES:
|
| 111 |
+
return rel.with_suffix("")
|
| 112 |
+
return rel
|
| 113 |
+
|
| 114 |
+
|
| 115 |
+
def main_path_from_frame_path(raw_frame_path: str | Path) -> Path:
|
| 116 |
+
rel = dataset_relative_path(raw_frame_path)
|
| 117 |
+
if rel.suffix:
|
| 118 |
+
return rel.parent
|
| 119 |
+
return rel
|
| 120 |
+
|
| 121 |
+
|
| 122 |
+
def portable_frame_path_from_any(raw_frame_path: str | Path) -> Path:
|
| 123 |
+
return PORTABLE_IMAGE_ROOT / dataset_relative_path(raw_frame_path)
|
| 124 |
+
|
| 125 |
+
|
| 126 |
+
def placeholder_frame_path_from_any(raw_frame_path: str | Path) -> str:
|
| 127 |
+
return f"{PUBLIC_FRAMES_ROOT_PLACEHOLDER}/{dataset_relative_path(raw_frame_path).as_posix()}"
|
| 128 |
+
|
| 129 |
+
|
| 130 |
+
def absolute_frame_path_from_any(raw_frame_path: str | Path, frames_root: Path) -> Path:
|
| 131 |
+
raw = str(raw_frame_path or "").strip()
|
| 132 |
+
if not raw:
|
| 133 |
+
raise ValueError("empty frame path")
|
| 134 |
+
path = Path(raw)
|
| 135 |
+
if path.is_absolute():
|
| 136 |
+
return path
|
| 137 |
+
|
| 138 |
+
parts = _normalized_parts(raw)
|
| 139 |
+
if parts and parts[0] in (PUBLIC_FRAMES_ROOT_PLACEHOLDER, PUBLIC_RELEASE_ROOT_PLACEHOLDER):
|
| 140 |
+
parts = parts[1:]
|
| 141 |
+
|
| 142 |
+
for portable_root in portable_image_roots():
|
| 143 |
+
marker_parts = list(portable_root.parts)
|
| 144 |
+
if parts[: len(marker_parts)] == marker_parts:
|
| 145 |
+
return frames_root.resolve() / Path(*parts[len(marker_parts) :])
|
| 146 |
+
if parts and parts[0] in KNOWN_DATASET_NAMES:
|
| 147 |
+
return frames_root.resolve() / Path(*parts)
|
| 148 |
+
return frames_root.resolve() / Path(*parts)
|
| 149 |
+
|
| 150 |
+
|
| 151 |
+
def rewrite_benchmark_image_paths(
|
| 152 |
+
rows: list[dict[str, Any]],
|
| 153 |
+
path_rewriter: Callable[[str], str],
|
| 154 |
+
) -> list[dict[str, Any]]:
|
| 155 |
+
rewritten_rows: list[dict[str, Any]] = []
|
| 156 |
+
for row in rows:
|
| 157 |
+
rewritten = dict(row)
|
| 158 |
+
|
| 159 |
+
frame_index = dict(row.get("frame_index") or {})
|
| 160 |
+
if frame_index:
|
| 161 |
+
rewritten_frame_index: dict[str, dict[str, str]] = {}
|
| 162 |
+
for frame_idx, images in frame_index.items():
|
| 163 |
+
image_map = dict(images or {})
|
| 164 |
+
rewritten_frame_index[str(frame_idx)] = {
|
| 165 |
+
str(view): path_rewriter(str(image_path))
|
| 166 |
+
for view, image_path in image_map.items()
|
| 167 |
+
if str(image_path or "").strip()
|
| 168 |
+
}
|
| 169 |
+
rewritten["frame_index"] = rewritten_frame_index
|
| 170 |
+
|
| 171 |
+
reference_context = row.get("reference_context")
|
| 172 |
+
if isinstance(reference_context, dict):
|
| 173 |
+
rewritten_ref = dict(reference_context)
|
| 174 |
+
anchors = list(reference_context.get("reference_anchors") or [])
|
| 175 |
+
rewritten_anchors: list[dict[str, Any]] = []
|
| 176 |
+
for anchor in anchors:
|
| 177 |
+
rewritten_anchor = dict(anchor)
|
| 178 |
+
images = dict(anchor.get("images") or {})
|
| 179 |
+
if images:
|
| 180 |
+
rewritten_anchor["images"] = {
|
| 181 |
+
str(view): path_rewriter(str(image_path))
|
| 182 |
+
for view, image_path in images.items()
|
| 183 |
+
if str(image_path or "").strip()
|
| 184 |
+
}
|
| 185 |
+
rewritten_anchors.append(rewritten_anchor)
|
| 186 |
+
rewritten_ref["reference_anchors"] = rewritten_anchors
|
| 187 |
+
rewritten["reference_context"] = rewritten_ref
|
| 188 |
+
|
| 189 |
+
videos = row.get("videos")
|
| 190 |
+
if isinstance(videos, list):
|
| 191 |
+
rewritten_videos: list[Any] = []
|
| 192 |
+
for item in videos:
|
| 193 |
+
if isinstance(item, list):
|
| 194 |
+
rewritten_videos.append(
|
| 195 |
+
[path_rewriter(str(path)) for path in item if str(path or "").strip()]
|
| 196 |
+
)
|
| 197 |
+
elif isinstance(item, str) and item.strip():
|
| 198 |
+
rewritten_videos.append(path_rewriter(item))
|
| 199 |
+
rewritten["videos"] = rewritten_videos
|
| 200 |
+
|
| 201 |
+
images = row.get("images")
|
| 202 |
+
if isinstance(images, list):
|
| 203 |
+
rewritten["images"] = [
|
| 204 |
+
path_rewriter(str(path))
|
| 205 |
+
for path in images
|
| 206 |
+
if str(path or "").strip()
|
| 207 |
+
]
|
| 208 |
+
|
| 209 |
+
rewritten_rows.append(rewritten)
|
| 210 |
+
return rewritten_rows
|
| 211 |
+
|
| 212 |
+
|
| 213 |
+
def infer_main_path_from_row(row: dict[str, Any]) -> Path | None:
|
| 214 |
+
for meta_key in ("metadata", "_meta", "meta"):
|
| 215 |
+
meta = row.get(meta_key)
|
| 216 |
+
if isinstance(meta, dict):
|
| 217 |
+
raw_main_path = str(meta.get("main_path") or "").strip()
|
| 218 |
+
if raw_main_path:
|
| 219 |
+
return normalize_main_path(raw_main_path)
|
| 220 |
+
|
| 221 |
+
frame_index = row.get("frame_index")
|
| 222 |
+
if isinstance(frame_index, dict):
|
| 223 |
+
for images in frame_index.values():
|
| 224 |
+
if not isinstance(images, dict):
|
| 225 |
+
continue
|
| 226 |
+
for image_path in images.values():
|
| 227 |
+
if str(image_path or "").strip():
|
| 228 |
+
return main_path_from_frame_path(str(image_path))
|
| 229 |
+
|
| 230 |
+
videos = row.get("videos")
|
| 231 |
+
if isinstance(videos, list) and videos:
|
| 232 |
+
first = videos[0]
|
| 233 |
+
if isinstance(first, list) and first:
|
| 234 |
+
return main_path_from_frame_path(str(first[0]))
|
| 235 |
+
if isinstance(first, str) and first.strip():
|
| 236 |
+
return main_path_from_frame_path(first)
|
| 237 |
+
|
| 238 |
+
return None
|
| 239 |
+
|
| 240 |
+
|
| 241 |
+
def discover_benchmark_jsons(benchmark_root: Path) -> list[Path]:
|
| 242 |
+
benchmark_root = benchmark_root.resolve()
|
| 243 |
+
candidates: list[Path] = []
|
| 244 |
+
seen: set[Path] = set()
|
| 245 |
+
for pattern in ("**/video_progress_benchmark_file.json", "**/intermediate_benchmark_*.json"):
|
| 246 |
+
for path in sorted(benchmark_root.glob(pattern)):
|
| 247 |
+
resolved = path.resolve()
|
| 248 |
+
if resolved in seen:
|
| 249 |
+
continue
|
| 250 |
+
seen.add(resolved)
|
| 251 |
+
candidates.append(resolved)
|
| 252 |
+
return candidates
|
| 253 |
+
|
| 254 |
+
|
| 255 |
+
def resolve_benchmark_root(release_root: Path) -> Path:
|
| 256 |
+
release_root = release_root.resolve()
|
| 257 |
+
preferred = release_root / PUBLIC_BENCHMARK_DIRNAME
|
| 258 |
+
full_release = release_root / FULL_RELEASE_BENCHMARK_DIRNAME
|
| 259 |
+
legacy = release_root / LEGACY_BENCHMARK_DIRNAME
|
| 260 |
+
if preferred.exists():
|
| 261 |
+
return preferred
|
| 262 |
+
if full_release.exists():
|
| 263 |
+
return full_release
|
| 264 |
+
if legacy.exists():
|
| 265 |
+
return legacy
|
| 266 |
+
return preferred
|