maliang / scripts /teaser /charfix_feed.py
teawhite's picture
scripts/teaser: teaser / edit / bossswap / infinite / cover code
68e2686 verified
Raw History Blame Contribute Delete
5.48 kB
#!/usr/bin/env python3
"""teaser · 角色二次编辑支线(用户 11-04:「有一些首帧的玩家和 boss 还是火柴人,可以不用重新生成,直接做二次编辑」)。
循环(每 --every 秒)读 prod/picks.jsonl(feed.py 每槽选中的首帧),挑出要修角色的:
* need = gate 判「角色不是活物」(fails 里有 creature:火柴人 / 胶囊人偶 / 被画成石头)——任何画风,修完 prio 4;
* real = 写实 / 电影感画风的槽(--real_looks)另出一版真实角色的 —— 实例 R 的双向验证:真实角色全程保持外观,生成角色约
f60 起会糊成暗影 ⇒ 写实槽两版都推(prio 1),选片时比较。
修法 = f0_realchar.py fix(实例 R:擦掉原图里的角色 / 人偶 / 影子 → 贴源 clip 真实帧 0 的真实角色(SAM2 抠图)→ 角色外圈用
qwen21 融合),输出 <png>_cf.png;修完的追加进 prod/roll_jobs.jsonl(name = <case>__<scene>__rc,tags 加 realchar)。
修过的记在 prod/charfix_done.jsonl(可续跑)。f0_realchar.py 跑在 img env 的子进程里(模型每批加载一次,≈ 20 s)。
usage(maliang venv):
nohup .venv/bin/python scripts/teaser/charfix_feed.py --gpu 3 > outputs/teaser/logs/prod/charfix.log 2>&1 &
"""
import argparse
import json
import os
import subprocess
import sys
import time
M = "/opt/dlami/nvme/zhiyangdeng/maliang"
T = f"{M}/outputs/teaser"
P = f"{T}/prod"
def load_jsonl(p):
if not os.path.exists(p):
return []
with open(p) as f:
return [json.loads(l) for l in f if l.strip()]
def main():
ap = argparse.ArgumentParser()
ap.add_argument("--gpu", default="3")
ap.add_argument("--every", type=int, default=120)
ap.add_argument("--min_batch", type=int, default=20)
ap.add_argument("--real_looks", default="photoreal,cinematic")
ap.add_argument("--once", action="store_true")
a = ap.parse_args()
real_looks = set(a.real_looks.split(","))
env = dict(os.environ, CUDA_VISIBLE_DEVICES=a.gpu, HF_HOME="/opt/dlami/nvme/zhiyangdeng/.cache/huggingface",
HF_HUB_OFFLINE="1", TMPDIR=f"{M}/.tmp")
nb = 0
while True:
slots = {f"{s['case']}|{s['scene']}": s for s in load_jsonl(f"{P}/slots.jsonl")} # 每轮重读(会追加新 wave)
done = {r["png"] for r in load_jsonl(f"{P}/charfix_done.jsonl")}
todo = []
for p in load_jsonl(f"{P}/picks.jsonl"):
if p["png"] in done or p["png"].endswith("_cf.png"):
continue
fails = set(p.get("fails") or [])
good = p.get("tier") in ("A", "B")
# need:角色坏了,且除了角色题没别的硬伤(备选槽的布局本来就不对,修角色不值);real:只给过了门槛的写实槽
why = "need" if "creature" in fails and (good or fails <= {"creature", "extra"}) else \
"real" if good and p["look"] in real_looks else None
if why:
todo.append((why, p))
todo.sort(key=lambda x: (x[0] != "need", not x[1]["hero"], x[1]["wave"]))
if todo and (len(todo) >= a.min_batch or a.once):
batch = todo[:120]
jp = f"{P}/charfix_batches/c{int(time.time())}.jsonl"
os.makedirs(os.path.dirname(jp), exist_ok=True)
with open(jp, "w") as f:
for why, p in batch:
s = slots[p["slot"]]
f.write(json.dumps(dict(png=p["png"], case=p["case"], map_dir=s["map_dir"], scene=p["scene"],
look=p["look"], seed=0)) + "\n")
t0 = time.time()
r = subprocess.run([f"{M}/envs/img/bin/python", f"{M}/scripts/teaser/f0_realchar.py", "fix", "--jobs", jp],
env=env, cwd=M, stdout=open(jp[:-6] + ".log", "w"), stderr=subprocess.STDOUT)
nb += 1
n_ok = 0
with open(f"{P}/charfix_done.jsonl", "a") as fd, open(f"{P}/roll_jobs.jsonl", "a") as fj:
for why, p in batch:
cf = p["png"][:-4] + "_cf.png"
ok = os.path.exists(cf) and os.path.exists(cf[:-4] + ".json")
fd.write(json.dumps(dict(png=p["png"], cf=cf if ok else None, why=why, slot=p["slot"])) + "\n")
if not ok:
continue
n_ok += 1
s = slots[p["slot"]]
md = s["map_dir"]
prio = (4 if why == "need" else 1) + (1 if s["hero"] else 0)
fj.write(json.dumps(dict(name=f"{s['case']}__{s['scene']}__rc", first=cf, ids=f"{md}/ids.npz",
prompts=f"{md}/prompts.json", out=f"{T}/roll", prio=prio, scene=s["scene"],
map=s["theme"], label=f"{s['scene']} | {s['theme']} | realchar",
tags=[s["look"], s["base"], "realchar", f"charfix_{why}", f"wave{s['wave']}"] +
(["hero"] if s["hero"] else [])), ensure_ascii=False) + "\n")
print(f"[charfix] 批 {nb}: {len(batch)} 张(need {sum(w == 'need' for w, _ in batch)}),成功 {n_ok},"
f"{time.time() - t0:.0f}s,rc {r.returncode}", flush=True)
if a.once:
break
time.sleep(a.every)
if __name__ == "__main__":
sys.exit(main())