File size: 4,264 Bytes
aa991fc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#!/usr/bin/env python
"""Build the browsable QC previews for one capture.

Reads (READ ONLY) the tracker's QC output:
    <DEGAS_DATASET>/<PxCy>/hmt_seq_out/grid_f<frame:08d>.jpg     32-view contact sheet
    <DEGAS_DATASET>/<PxCy>/hmt_seq_out/panels/f<frame>_cam<NN>.jpg  3-panel per view

Writes into the HF staging tree:
    previews/<PxCy>_f<frame:08d>.jpg     downscaled contact sheet, one per sampled frame
                                          (this is the `image` column of the data viewer)
    data/<PxCy>/preview.jpg               one representative contact sheet for the capture

Usage:
    python make_previews.py P1C1 --staging /mnt/sdb/degas_project/DREAMS-AVATAR-hf/staging
"""
from __future__ import annotations

import argparse
import json
import sys
from pathlib import Path

from PIL import Image

DATASET_ROOT = Path("/mnt/sdb/degas_project/DEGAS_DATASET")
Image.MAX_IMAGE_PIXELS = None


def _save_resized(src: Path, dst: Path, width: int, quality: int = 88) -> int:
    im = Image.open(src).convert("RGB")
    if im.width > width:
        h = max(1, round(im.height * width / im.width))
        im = im.resize((width, h), Image.LANCZOS)
    dst.parent.mkdir(parents=True, exist_ok=True)
    im.save(dst, "JPEG", quality=quality, optimize=True, progressive=True)
    return dst.stat().st_size


def make_previews(capture: str, dataset_root: Path, staging: Path,
                  grid_width: int = 1920, hero_width: int = 2560) -> dict:
    out_dir = dataset_root / capture / "hmt_seq_out"
    grids = sorted(out_dir.glob("grid_f*.jpg"))
    if not grids:
        raise SystemExit(f"no grid_f*.jpg under {out_dir} -- has the tracker finished {capture}?")

    prev_dir = staging / "previews"
    rows = []
    total = 0
    for g in grids:
        frame = int(g.stem.split("_f")[1])
        dst = prev_dir / f"{capture}_f{frame:08d}.jpg"
        total += _save_resized(g, dst, grid_width)
        rows.append({"file_name": dst.name, "frame": frame})

    # capture-level preview.jpg: the middle sampled frame
    mid = grids[len(grids) // 2]
    cap_prev = staging / "data" / capture / "preview.jpg"
    _save_resized(mid, cap_prev, hero_width)

    print(f"[previews] {capture}: {len(rows)} grids -> {prev_dir} "
          f"({total / 2**20:.1f} MB), preview.jpg from {mid.name}", flush=True)
    return {"capture": capture, "n_previews": len(rows), "rows": rows,
            "preview_source": mid.name, "bytes": total}


def copy_showcase(capture: str, dataset_root: Path, staging: Path,
                  frame: int, cams: list[int], width: int = 2560) -> list[str]:
    """Copy the hand-picked 3-panel views used as the README hero images."""
    panels = dataset_root / capture / "hmt_seq_out" / "panels"
    out = []
    for c in cams:
        src = panels / f"f{frame:08d}_cam{c:02d}.jpg"
        if not src.exists():
            print(f"[showcase] MISSING {src}", file=sys.stderr)
            continue
        dst = staging / "assets" / "showcase" / f"{capture}_f{frame:08d}_cam{c:02d}.jpg"
        _save_resized(src, dst, width, quality=92)
        out.append(str(dst.relative_to(staging)))
        print(f"[showcase] {src.name} -> {dst.relative_to(staging)}", flush=True)
    return out


def main() -> int:
    ap = argparse.ArgumentParser(description=__doc__,
                                 formatter_class=argparse.RawDescriptionHelpFormatter)
    ap.add_argument("capture")
    ap.add_argument("--dataset-root", type=Path, default=DATASET_ROOT)
    ap.add_argument("--staging", type=Path, required=True)
    ap.add_argument("--grid-width", type=int, default=1920)
    ap.add_argument("--showcase-frame", type=int, default=None,
                    help="also copy panels/f<frame>_cam<NN>.jpg into assets/showcase/")
    ap.add_argument("--showcase-cams", type=int, nargs="*", default=[3, 6, 12])
    a = ap.parse_args()

    info = make_previews(a.capture, a.dataset_root, a.staging, a.grid_width)
    if a.showcase_frame is not None:
        info["showcase"] = copy_showcase(a.capture, a.dataset_root, a.staging,
                                         a.showcase_frame, a.showcase_cams)
    print(json.dumps({k: v for k, v in info.items() if k != "rows"}))
    return 0


if __name__ == "__main__":
    sys.exit(main())