Download scripts/teaser/bossswap2_long.py from teawhite/maliang: direct link, hf CLI and curl.
- Browser
- Download file 6.07 kB
-
https://huggingface.co/teawhite/maliang/resolve/main/scripts/teaser/bossswap2_long.py
- Command line
-
hf download hf://teawhite/maliang/scripts/teaser/bossswap2_long.py
-
curl -L -o bossswap2_long.py https://huggingface.co/teawhite/maliang/resolve/main/scripts/teaser/bossswap2_long.py
6.07 kB
| #!/usr/bin/env python3 | |
| """teaser · Boss 换装第二轮(bossswap2)的因果 DMD 长版(17–21 s):外观保持曲线 + 成片(maliang venv,CPU)。 | |
| 由 bossswap_long.py 复制改名:外观 = long/cases 里实际有的(任意 BOSSES),同一时刻窗 LONG_WIN 换成第二轮底子的。 | |
| 输入 = rollout_dmd.py 写的 $B/long/cases/<底子>__<外观>__dmd/(gen.mp4 4N+1 帧、ids.npz = longmap.py 接出来的长条件)。 | |
| * metrics:每 8 帧取一帧,条件 Boss 掩码(膨胀 2 px,≥ 3000 px 才算)内编辑版 vs 原版的 CIELAB 平均色差 ΔE_t, | |
| keep_t = ΔE_t / ΔE_0(同 81 帧版的 keep)→ $B/long/metrics.json(每条:keep 曲线、每 2 s 的中位数、Boss 不在画面的时段)。 | |
| * compose → $B/showcase/: | |
| long_match_<底子>.mp4 连续匹配剪辑:整条时间线(Boss 在画面里的部分)按外观均分,剪辑点同一帧混合 ⇒ 一条 17 s 的连续战斗、 | |
| Boss 每 ~2 s 换一次外观、动作连续; | |
| long_same_<底子>.mp4 同一时刻:每条放同一个 2 s 窗(Boss 离开画面又回来之后,看外观能不能保持); | |
| long_grid_<底子>.mp4 同步宫格(原版 + 各外观 + 共用条件),整条长度。 | |
| 每个都有原生 / 1080p × 无字 / 带字 四个版本(bossswap_compose.Out4)。 | |
| usage:.venv/bin/python scripts/teaser/bossswap_long.py [metrics] [compose] | |
| """ | |
| from __future__ import annotations | |
| import json | |
| import os | |
| import sys | |
| import numpy as np | |
| M = "/opt/dlami/nvme/zhiyangdeng/maliang" | |
| HERE = os.path.dirname(os.path.abspath(__file__)) | |
| sys.path[:0] = [HERE, f"{M}/code/gc25d"] | |
| import bossswap2_compose as BC # noqa: E402 | |
| import compose as C # noqa: E402 | |
| from bossswap2_plan import B, BOSSES, DMD_LONG, bases, long_dir # noqa: E402 | |
| LC = f"{B}/long/cases" | |
| BOSS = (11, 17, 18, 19, 20, 21) | |
| LONG_BOSSES = list(BOSSES) | |
| # 同一时刻窗(帧):candy = 10–12 s(Boss 在 6–7 s 短暂出画后回来),rice = 18–20 s(Boss 在 5–17 s 离开画面后回来) | |
| LONG_WIN = {"venice": 160, "glass": 176, "badlands": 160} | |
| def names_of(short: str) -> list[str]: | |
| """原版 + 有的外观,按 bossswap2_compose.LOOK_ORDER 排(成片里颜色错开)。""" | |
| order = ["orig"] + BC.LOOK_ORDER + [k for k in LONG_BOSSES if k not in BC.LOOK_ORDER] | |
| return [f"{short}__{k}__dmd" for k in order if os.path.exists(f"{LC}/{short}__{k}__dmd/gen.mp4")] | |
| def lab(img): | |
| import cv2 | |
| return cv2.cvtColor(img, cv2.COLOR_RGB2LAB).astype(np.float32) * np.array([100 / 255, 1, 1], np.float32) | |
| def cmd_metrics() -> None: | |
| import cv2 | |
| out = {} | |
| for short in DMD_LONG: | |
| names = names_of(short) | |
| if not names: | |
| continue | |
| with np.load(f"{long_dir(short)}/ids.npz") as z: | |
| ids = z["ids"] | |
| T = list(range(0, len(ids), 8)) | |
| bm = [cv2.dilate(np.isin(ids[t], BOSS).astype(np.uint8), np.ones((5, 5), np.uint8)) > 0 for t in T] | |
| px = [int(np.isin(ids[t], BOSS).sum()) for t in T] | |
| orig = C.read_video(f"{LC}/{names[0]}/gen.mp4") | |
| ol = {t: lab(orig[t]) for t in T} | |
| absent = [t for t, p in zip(T, px) if p < 3000] | |
| base = {"n_frames": len(ids), "sec": round(len(ids) / 16, 2), "t_eval": T, "boss_px": px, | |
| "boss_absent_frames": absent} | |
| cases = {} | |
| for n in names[1:]: | |
| v = C.read_video(f"{LC}/{n}/gen.mp4") | |
| dE = [round(float(np.linalg.norm(lab(v[t])[m] - ol[t][m], axis=-1).mean()), 2) if p >= 3000 else None | |
| for t, m, p in zip(T, bm, px)] | |
| keep = [None if d is None or not dE[0] else round(d / dE[0], 3) for d in dE] | |
| per2s = {} | |
| for t, k in zip(T, keep): | |
| if k is not None: | |
| per2s.setdefault(int(t // 32) * 2, []).append(k) | |
| cases[n] = {"dE_t": dE, "keep_t": keep, "keep_per_2s_median": {f"{s}s": round(float(np.median(x)), 3) | |
| for s, x in sorted(per2s.items())}, | |
| "keep_last": next((k for k in reversed(keep) if k is not None), None)} | |
| print(n, cases[n]["keep_per_2s_median"], flush=True) | |
| out[short] = {**base, "cases": cases} | |
| json.dump(out, open(f"{B}/long/metrics.json", "w"), ensure_ascii=False, indent=1) | |
| print(f"→ {B}/long/metrics.json") | |
| def cmd_compose() -> None: | |
| bs = {b["short"]: b for b in bases()} | |
| for short in DMD_LONG: | |
| names = names_of(short) | |
| if len(names) < 2: | |
| continue | |
| b = dict(bs[short]) | |
| key = f"{short}L" | |
| b["short"], b["ids"] = key, f"{long_dir(short)}/ids.npz" | |
| alias = [f"{key}__{n.split('__')[1]}" for n in names] | |
| BC._VID.clear() | |
| BC._COND.pop(key, None) | |
| for a_, n in zip(alias, names): | |
| BC._VID[a_] = C.read_video(f"{LC}/{n}/gen.mp4") | |
| with np.load(b["ids"]) as z: | |
| BC._VID[f"{key}__cond"] = C.colorize(z["ids"]) | |
| nf = len(BC._VID[alias[0]]) | |
| ci = BC.cond_info(b) | |
| px = np.asarray(ci["px"]) | |
| vis = np.nonzero(px >= 3000)[0] | |
| lo, hi = int(vis.min()), int(vis.max()) + 1 | |
| # 连续匹配:只在 Boss 可见的帧上切(Boss 不在画面的时段照常播放当前那条) | |
| n = BC.write_seq(f"{BC.S}/long_match_{short}", BC.seq_match(b, alias[:9], T=6, lo=lo, hi=hi, by_visible=True)) # 原版 + 8 种,每种 ≈ 2 s | |
| print(f"long_match_{short}.mp4 · {n} 帧 = {n / 16:.1f} s", flush=True) | |
| BC.WIN.setdefault(32, {})[key] = LONG_WIN[short] | |
| n = BC.write_seq(f"{BC.S}/long_same_{short}", BC.seq_same(b, alias, L=32, T=6)) | |
| print(f"long_same_{short}.mp4 · {n} 帧", flush=True) | |
| BC.write_grid(f"{BC.S}/long_grid_{short}", alias[:16], loops=1, nf=nf) | |
| BC._VID.clear() | |
| def main(): | |
| cmds = sys.argv[1:] or ["metrics", "compose"] | |
| if "metrics" in cmds: | |
| cmd_metrics() | |
| if "compose" in cmds: | |
| cmd_compose() | |
| if __name__ == "__main__": | |
| main() | |