Spaces:
Paused
Paused
Download apps/renderer/services/enhance/debug_dump.py from modventures/prevision: direct link, hf CLI and curl.
- Browser
- Download file 2.8 kB
-
https://huggingface.co/spaces/modventures/prevision/resolve/main/apps/renderer/services/enhance/debug_dump.py
- Command line
-
hf download hf://spaces/modventures/prevision/apps/renderer/services/enhance/debug_dump.py
-
curl -L -o debug_dump.py https://huggingface.co/spaces/modventures/prevision/resolve/main/apps/renderer/services/enhance/debug_dump.py
2.8 kB
| """Optional local dump of every enhancement, for scripts/enhance_compare_sheet.py. | |
| Enabled with ENHANCE_DEBUG_DIR; refused on deployed pods (RunPod or a Hugging Face | |
| Space), where it would write shoppers' rooms to disk. | |
| """ | |
| from __future__ import annotations | |
| import json | |
| import logging | |
| import os | |
| import time | |
| from pathlib import Path | |
| import numpy as np | |
| from PIL import Image | |
| from config.gemini import ENHANCE_DEBUG_DIR | |
| logger = logging.getLogger(__name__) | |
| def debug_dir(request_id: str) -> Path | None: | |
| if not ENHANCE_DEBUG_DIR: | |
| return None | |
| if os.environ.get("RUNPOD_POD_ID") or os.environ.get("SPACE_ID"): | |
| logger.warning("ENHANCE_DEBUG_DIR is ignored on a deployed renderer") | |
| return None | |
| path = Path(ENHANCE_DEBUG_DIR) / f"{time.strftime('%Y%m%d-%H%M%S')}-{request_id[:8]}" | |
| path.mkdir(parents=True, exist_ok=True) | |
| return path | |
| def _png(path: Path, array: np.ndarray) -> None: | |
| Image.fromarray(array).save(path, format="PNG") | |
| def dump_prepared(path: Path | None, prepared, manifest: dict) -> None: | |
| if path is None: | |
| return | |
| _png(path / "original.png", prepared.context.original) | |
| _png(path / "editor.png", prepared.editor) | |
| _png(path / "preview.png", prepared.preview) | |
| if prepared.context.seg_map is not None: | |
| np.save(path / "seg_map.npy", prepared.context.seg_map) | |
| for item in prepared.refined: | |
| layer = item.layer | |
| rgba = np.dstack([layer.rgb, (layer.support * 255).astype(np.uint8)]) | |
| _png(path / f"layer.{layer.id}.png", rgba) | |
| _png(path / f"coverage.{layer.id}.png", np.clip(layer.coverage * 255, 0, 255).astype(np.uint8)) | |
| _png(path / f"planes.{layer.id}.png", layer.planes) | |
| _png(path / f"alpha.{layer.id}.png", np.clip(item.alpha * 255, 0, 255).astype(np.uint8)) | |
| _png(path / f"repaired.{layer.id}.png", (item.repaired * 255).astype(np.uint8)) | |
| _png(path / f"trimmed.{layer.id}.png", (item.trimmed * 255).astype(np.uint8)) | |
| (path / "manifest.json").write_text(json.dumps(manifest, indent=2)) | |
| def dump_result(path: Path | None, result=None, summary: dict | None = None) -> None: | |
| if path is None: | |
| return | |
| if result is not None: | |
| _png(path / "ai_answer.png", result.answer) | |
| _png(path / "final.png", result.final) | |
| for index, light in enumerate(result.lighting): | |
| np.save(path / f"gain.{index}.npy", light.gain.astype(np.float16)) | |
| for index, (alpha, restored) in enumerate(zip(result.alphas, result.restored)): | |
| _png(path / f"final_alpha.{index}.png", np.clip(alpha * 255, 0, 255).astype(np.uint8)) | |
| _png(path / f"restored.{index}.png", (restored * 255).astype(np.uint8)) | |
| (path / "summary.json").write_text(json.dumps(summary or {}, indent=2, default=str)) | |