Spaces:
Running
Running
Download engine.py from ming0531/FlashReFocus-CPU: direct link, hf CLI and curl.
- Browser
- Download file 2.79 kB
-
https://huggingface.co/spaces/ming0531/FlashReFocus-CPU/resolve/main/engine.py
- Command line
-
hf download hf://spaces/ming0531/FlashReFocus-CPU/engine.py
-
curl -L -o engine.py https://huggingface.co/spaces/ming0531/FlashReFocus-CPU/resolve/main/engine.py
2.79 kB
| """FlashReFocus on the CPU (free Hugging Face CPU Space), on the flashrefocus package (a copy of the repository's). | |
| Photographs that are already sharp only: Stage I (restoration) needs a GPU and is not loaded. Depth Anything V2 runs | |
| once per photograph at its native input size (518 on the short side), and the 1.3M-parameter renderer renders every | |
| edit, both in fp32 with the CPU attention path of flashrefocus/rendering/fused.py (same output as the reference up to | |
| fp32 rounding). The renderer weights come from a private model repository read with the HF_TOKEN secret.""" | |
| import os | |
| from pathlib import Path | |
| import numpy as np | |
| import torch | |
| from huggingface_hub import hf_hub_download | |
| from PIL import Image | |
| HERE = Path(__file__).resolve().parent | |
| WEIGHT_REPO = os.environ.get("FLASHREFOCUS_WEIGHT_REPO", "ming0531/FlashReFocus-weights") | |
| torch.set_num_threads(max(1, os.cpu_count() or 1)) | |
| def fetch_weights(): | |
| if "FLASHREFOCUS_WEIGHTS" in os.environ: | |
| return | |
| w = HERE / "weights" | |
| hf_hub_download("depth-anything/Depth-Anything-V2-Large", "depth_anything_v2_vitl.pth", local_dir=w) | |
| hf_hub_download(WEIGHT_REPO, "renderer.pt", local_dir=w, token=os.environ.get("HF_TOKEN")) | |
| os.environ["FLASHREFOCUS_WEIGHTS"] = str(w) | |
| fetch_weights() | |
| from flashrefocus import Refocuser, Scene # noqa: E402 | |
| from flashrefocus.image_io import image_to_tensor, tensor_to_uint8 # noqa: E402 | |
| from flashrefocus.rendering import depth_edits # noqa: E402 | |
| refocuser = Refocuser.from_pretrained(deblurring=False, device="cpu", fused=True, depth_input_size=518) | |
| def restore(rgb, omega=None): | |
| """HxWx3 uint8 (sides multiples of 16) -> the same image and its depth (HxW float32, 1 = far). No restoration.""" | |
| scene = refocuser.prepare(image_to_tensor(rgb), deblur=False) | |
| return np.asarray(rgb), scene.depth[0].numpy() | |
| def render(rgb, depth, focal_depth, strength): | |
| """Bokeh (HxWx3 uint8) with blur strength K and focal depth D_f; depth may be edited.""" | |
| d = torch.from_numpy(np.ascontiguousarray(depth, dtype=np.float32))[None] | |
| return tensor_to_uint8(refocuser.render(Scene(image_to_tensor(rgb), d), strength, focal_depth)) | |
| def two_focal_planes(depth, focal_depth_a, focal_depth_b): | |
| return depth_edits.two_focal_planes(torch.from_numpy(depth)[None], focal_depth_a, focal_depth_b)[0].numpy() | |
| def tilt_shift(height, width, row, band=0.06): | |
| return depth_edits.tilt_shift(height, width, row, band=band)[0].numpy() | |
| def depth_from_image(image, height, width): | |
| """A user depth map (near is bright, any size) -> D in [0, 1], 1 = far.""" | |
| grey = Image.fromarray(np.asarray(image)).convert("L").resize((width, height), Image.BILINEAR) | |
| return (1.0 - np.asarray(grey, dtype=np.float32) / 255.0).astype(np.float32) | |