Spaces:
Running
Running
Download app/render_live.py from imaan3/optorigbench-tools: direct link, hf CLI and curl.
- Browser
- Download file 5.25 kB
-
https://huggingface.co/spaces/imaan3/optorigbench-tools/resolve/main/app/render_live.py
- Command line
-
hf download hf://spaces/imaan3/optorigbench-tools/app/render_live.py
-
curl -L -o render_live.py https://huggingface.co/spaces/imaan3/optorigbench-tools/resolve/main/app/render_live.py
5.25 kB
| """Render a replay movie and write its latest frame and progress to --frames-dir while it renders. | |
| python render_live.py RECORD --out movie.mp4 --frames-dir DIR | |
| """ | |
| from __future__ import annotations | |
| import argparse | |
| import importlib.util | |
| import json | |
| import os | |
| import sys | |
| import time | |
| from pathlib import Path | |
| CODE = Path.cwd() | |
| sys.path.insert(0, str(CODE)) | |
| sys.path.insert(0, str(CODE / "scripts")) | |
| class Progress: | |
| def __init__(self, folder: Path, every_s: float, width: int): | |
| self.dir = folder | |
| self.dir.mkdir(parents=True, exist_ok=True) | |
| self.every_s, self.width = every_s, width | |
| self.t0 = time.time() | |
| self.state = {"phase": "replay", "turn": 0, "turns": 0, "frames": 0, "shown": 0} | |
| self._last_write = 0.0 | |
| self._last_frame = 0.0 | |
| def _atomic(self, name: str, data: bytes): | |
| tmp = self.dir / f".{name}.tmp" | |
| tmp.write_bytes(data) | |
| os.replace(tmp, self.dir / name) | |
| def update(self, force: bool = False, **kw): | |
| self.state.update(kw) | |
| now = time.time() | |
| if force or now - self._last_write >= 0.25: | |
| self._last_write = now | |
| self.state["elapsed"] = round(now - self.t0, 1) | |
| self._atomic("progress.json", json.dumps(self.state).encode()) | |
| def frame(self, im, force: bool = False): | |
| now = time.time() | |
| if not force and now - self._last_frame < self.every_s: | |
| return | |
| self._last_frame = now | |
| import io | |
| small = im.convert("RGB") | |
| if small.width > self.width: | |
| small = small.resize((self.width, round(small.height * self.width / small.width))) | |
| buf = io.BytesIO() | |
| small.save(buf, "JPEG", quality=78) | |
| self._atomic("frame.jpg", buf.getvalue()) | |
| self.update(force=True, shown=self.state["shown"] + 1) | |
| def main(argv=None) -> int: | |
| ap = argparse.ArgumentParser() | |
| ap.add_argument("record") | |
| ap.add_argument("--out", required=True) | |
| ap.add_argument("--frames-dir", required=True) | |
| ap.add_argument("--every-s", type=float, default=0.6) | |
| ap.add_argument("--width", type=int, default=960) | |
| a = ap.parse_args(argv) | |
| P = Progress(Path(a.frames_dir), a.every_s, a.width) | |
| spec = importlib.util.spec_from_file_location("replay_movie", CODE / "scripts" / "replay_movie.py") | |
| rm = importlib.util.module_from_spec(spec) | |
| sys.modules["replay_movie"] = rm | |
| spec.loader.exec_module(rm) | |
| from optobench.harness.runner import read_record | |
| from optobench.tools.env import Episode | |
| record = read_record(Path(a.record)) | |
| n_turns = len(record.get("steps") or []) | |
| P.update(force=True, phase="replay", turn=0, turns=n_turns) | |
| # replay: count the turns as collect() begins each one | |
| begin = Episode.begin_turn | |
| def begin_turn(self, *args, **kw): | |
| P.update(turn=min(P.state["turn"] + 1, n_turns)) | |
| return begin(self, *args, **kw) | |
| Episode.begin_turn = begin_turn | |
| # room camera: count the views as they are path-traced | |
| rig_panels = rm.render_rig_panels | |
| def render_rig_panels(shots, size, spp): | |
| keys = {s.rig for s in shots if s.rig is not None} | |
| keys |= {s.live.rig_pulse for s in shots if s.live is not None and s.live.rig_pulse is not None} | |
| P.update(force=True, phase="rig", turn=0, turns=n_turns, view=0, views=len(keys)) | |
| try: | |
| from optobench.render import rigcam | |
| except ImportError: | |
| return rig_panels(shots, size, spp) | |
| orig = rigcam.render_rig_view | |
| def counted(*args, **kw): | |
| out = orig(*args, **kw) | |
| P.state["view"] = P.state.get("view", 0) + 1 | |
| try: | |
| P.frame(rm._png_image(out), force=True) # show each room-camera view as it is drawn | |
| except Exception: # noqa: BLE001 - a preview only | |
| P.update(force=True) | |
| return out | |
| rigcam.render_rig_view = counted | |
| try: | |
| return rig_panels(shots, size, spp) | |
| finally: | |
| rigcam.render_rig_view = orig | |
| rm.render_rig_panels = render_rig_panels | |
| # frames: the turn of the call being drawn (both layouts pass the shot and the record's turn count) | |
| for name, shot_at in (("compose", 1), ("compose_live", 2)): | |
| fn = getattr(rm, name) | |
| def wrapped(*args, _fn=fn, _i=shot_at, **kw): | |
| shot = args[_i] | |
| P.update(turn=int(getattr(shot, "turn", 0) or 0), call=int(getattr(shot, "index", 0) or 0)) | |
| return _fn(*args, **kw) | |
| setattr(rm, name, wrapped) | |
| write_movie = rm.write_movie | |
| def write_movie_teed(frames, out, size, fps): | |
| P.update(force=True, phase="frames", turn=0, turns=n_turns) | |
| def tee(): | |
| for im, k in frames: | |
| P.state["frames"] += k | |
| P.frame(im) | |
| P.update() | |
| yield im, k | |
| write_movie(tee(), out, size, fps) | |
| rm.write_movie = write_movie_teed | |
| info = rm.render(record, Path(a.out), record_path=Path(a.record), log=lambda s: None) | |
| P.update(force=True, phase="done", turn=n_turns, turns=n_turns) | |
| print(json.dumps(info), flush=True) | |
| return 0 | |
| if __name__ == "__main__": | |
| raise SystemExit(main()) | |