| | |
| | from __future__ import annotations |
| |
|
| | import argparse |
| | import ast |
| | import json |
| | from collections import Counter |
| | from pathlib import Path |
| | import sys |
| | from typing import Any |
| |
|
| | import pyarrow as pa |
| | import pyarrow.parquet as pq |
| |
|
| | ROOT = Path(__file__).resolve().parents[1] |
| | if str(ROOT) not in sys.path: |
| | sys.path.insert(0, str(ROOT)) |
| |
|
| | import dataset_catalog as catalog |
| |
|
| |
|
| | def _to_rel(base_dir: Path, value: str) -> str: |
| | text = str(value or "").strip() |
| | if not text: |
| | return "" |
| | p = Path(text) |
| | if not p.is_absolute(): |
| | return p.as_posix() |
| | try: |
| | return p.resolve().relative_to(base_dir).as_posix() |
| | except ValueError: |
| | return p.as_posix() |
| |
|
| |
|
| | def _build_rows(base_dir: Path, dataset: list[dict[str, Any]]) -> list[dict[str, Any]]: |
| | rows: list[dict[str, Any]] = [] |
| | for item in dataset: |
| | row = dict(item) |
| | row["video_rel_path"] = str(item.get("path", "")).strip() |
| | row["image_gt_rel_path"] = _to_rel(base_dir, str(item.get("image_gt", "")).strip()) |
| | image_paths = item.get("image_paths") if isinstance(item.get("image_paths"), list) else [] |
| | row["image_paths_rel"] = [_to_rel(base_dir, str(path)) for path in image_paths] |
| | rows.append(row) |
| | rows.sort(key=lambda r: str(r.get("path", ""))) |
| | return rows |
| |
|
| |
|
| | def _load_default_descriptions(base_dir: Path) -> dict[str, str]: |
| | app_path = base_dir / "app.py" |
| | if not app_path.exists(): |
| | return {} |
| | tree = ast.parse(app_path.read_text(encoding="utf-8")) |
| | for node in tree.body: |
| | if isinstance(node, ast.Assign): |
| | for target in node.targets: |
| | if isinstance(target, ast.Name) and target.id == "DEFAULT_DESCRIPTIONS": |
| | value = ast.literal_eval(node.value) |
| | if isinstance(value, dict): |
| | return {str(k): str(v) for k, v in value.items()} |
| | return {} |
| |
|
| |
|
| | def _validate_rows(base_dir: Path, rows: list[dict[str, Any]]) -> dict[str, Any]: |
| | missing_videos: list[str] = [] |
| | for row in rows: |
| | rel = str(row.get("path", "")).strip() |
| | if not rel: |
| | continue |
| | if not (base_dir / rel).exists(): |
| | missing_videos.append(rel) |
| |
|
| | split_counter = Counter(str(row.get("data_split", "")).strip().lower() for row in rows) |
| | category_counter = Counter(str(row.get("category", "")).strip() for row in rows) |
| | major_counter = Counter(str(row.get("major", "")).strip() for row in rows) |
| |
|
| | return { |
| | "total_rows": len(rows), |
| | "split_counts": dict(sorted(split_counter.items())), |
| | "category_counts": dict(sorted(category_counter.items())), |
| | "major_counts": dict(sorted(major_counter.items())), |
| | "missing_video_paths": missing_videos, |
| | } |
| |
|
| |
|
| | def main() -> int: |
| | parser = argparse.ArgumentParser( |
| | description="Build Parquet aligned with Streamlit app _build_dataset(meta) output." |
| | ) |
| | parser.add_argument( |
| | "--base-dir", |
| | default=str(Path(__file__).resolve().parents[1]), |
| | help="Repository base directory (default: script parent parent).", |
| | ) |
| | parser.add_argument( |
| | "--out-parquet", |
| | default="dist/streamlit_aligned.parquet", |
| | help="Parquet output path relative to base dir.", |
| | ) |
| | parser.add_argument( |
| | "--out-stats", |
| | default="dist/streamlit_aligned_stats.json", |
| | help="Stats output path relative to base dir.", |
| | ) |
| | parser.add_argument( |
| | "--strict-files", |
| | action="store_true", |
| | help="Fail if any video file in rows is missing on disk.", |
| | ) |
| | args = parser.parse_args() |
| |
|
| | base_dir = Path(args.base_dir).resolve() |
| | video_dir = base_dir / "videos" |
| | dataset_test_path = base_dir / "video_dataset.json" |
| | dataset_train_path = base_dir / "video_dataset_train.json" |
| | legacy_path = base_dir / "video_texts.json" |
| |
|
| | out_parquet = Path(args.out_parquet) |
| | if not out_parquet.is_absolute(): |
| | out_parquet = (base_dir / out_parquet).resolve() |
| | out_stats = Path(args.out_stats) |
| | if not out_stats.is_absolute(): |
| | out_stats = (base_dir / out_stats).resolve() |
| | out_parquet.parent.mkdir(parents=True, exist_ok=True) |
| | out_stats.parent.mkdir(parents=True, exist_ok=True) |
| |
|
| | meta, _, _ = catalog.load_meta( |
| | base_dir=base_dir, |
| | dataset_test_path=dataset_test_path, |
| | dataset_train_path=dataset_train_path, |
| | legacy_path=legacy_path, |
| | ) |
| | default_descriptions = _load_default_descriptions(base_dir) |
| | dataset = catalog.build_dataset( |
| | base_dir=base_dir, |
| | video_dir=video_dir, |
| | meta=meta, |
| | default_descriptions=default_descriptions, |
| | ) |
| | rows = _build_rows(base_dir, dataset) |
| | stats = _validate_rows(base_dir, rows) |
| |
|
| | if args.strict_files and stats["missing_video_paths"]: |
| | sample = stats["missing_video_paths"][:20] |
| | raise SystemExit( |
| | f"Missing {len(stats['missing_video_paths'])} video paths. Sample: {sample}" |
| | ) |
| |
|
| | table = pa.Table.from_pylist(rows) |
| | pq.write_table(table, out_parquet, compression="zstd") |
| | out_stats.write_text(json.dumps(stats, ensure_ascii=False, indent=2), encoding="utf-8") |
| |
|
| | print(f"Rows: {stats['total_rows']}") |
| | print(f"Split counts: {stats['split_counts']}") |
| | print(f"Parquet: {out_parquet}") |
| | print(f"Stats: {out_stats}") |
| | if stats["missing_video_paths"]: |
| | print(f"Missing video paths: {len(stats['missing_video_paths'])}") |
| | return 0 |
| |
|
| |
|
| | if __name__ == "__main__": |
| | raise SystemExit(main()) |
| |
|