prevision / apps /renderer /scripts /enhance_replay.py
github-actions[bot]
Automated Deploy: 6d2e55112e3ada832d411bd3301a883ce9eba3a9
c7a38a3
Raw History Blame Contribute Delete
3.17 kB
"""Re-run an "Enhance with AI" dump locally: coverage repair, lighting and compositing.
VISUALIZER_CONFIG=... python scripts/enhance_replay.py <run_dir> [--reuse-answer] [--out <dir>]
Reads the dumped manifest and layer PNGs and the job (photo, bundle, class map) from
this renderer's data directory, so repair and lighting can be tuned without the browser.
With --reuse-answer the dumped AI answer is used instead of a new AI call (no cost).
Nothing is charged either way. Writes a new dump (and its comparison sheet) to --out.
"""
from __future__ import annotations
import argparse
import json
import sys
from pathlib import Path
from unittest import mock
sys.path.insert(0, str(Path(__file__).resolve().parent.parent))
from PIL import Image # noqa: E402
from scripts.enhance_compare_sheet import build_sheet # noqa: E402
from services.enhance import debug_dump # noqa: E402
from services.enhance.job_context import load_job_context # noqa: E402
from services.enhance.pipeline import finish, prepare # noqa: E402
from services.enhance.validate import parse_layers # noqa: E402
def main() -> None:
parser = argparse.ArgumentParser(description=__doc__.split("\n")[0])
parser.add_argument("run", type=Path)
parser.add_argument("--reuse-answer", action="store_true")
parser.add_argument("--out", type=Path)
args = parser.parse_args()
manifest = json.loads((args.run / "manifest.json").read_text())
files = {}
for layer in manifest["layers"]:
for kind in ("layer", "coverage", "planes"):
files[f"{kind}.{layer['id']}"] = (args.run / f"{kind}.{layer['id']}.png").read_bytes()
context = load_job_context(manifest["jobId"])
layers = parse_layers(manifest, files, context)
prepared = prepare(context, layers)
out = args.out or args.run.with_name(args.run.name + "-replay")
out.mkdir(parents=True, exist_ok=True)
debug_dump.dump_prepared(out, prepared, manifest)
timings: dict[str, int] = {}
if args.reuse_answer:
answer_png = args.run / "ai_answer.png"
def reuse(preview, original, surfaces, outside, timings):
with Image.open(answer_png) as opened:
import numpy as np
return np.asarray(opened.convert("RGB")), 0
with mock.patch("services.enhance.pipeline.relight_reference", side_effect=reuse):
result = finish(prepared, timings)
else:
result = finish(prepared, timings)
summary = {
"replayOf": args.run.name,
"lighting": [{"id": item.layer.id, "status": light.status, **light.stats} for item, light in zip(prepared.refined, result.lighting)],
"coverage": [{"id": item.layer.id, "repairedPx": int(item.repaired.sum()), "trimmedPx": int(item.trimmed.sum()),
"restoredPx": int(restored.sum()), "restoredRegions": regions}
for item, restored, regions in zip(prepared.refined, result.restored, result.restored_regions)],
"timings": timings,
}
debug_dump.dump_result(out, result, summary)
print(json.dumps(build_sheet(out, out / "sheet.png"), indent=2))
if __name__ == "__main__":
main()