Spaces:
Sleeping
Sleeping
Add Relight tab
Browse files
app.py
CHANGED
|
@@ -1,4 +1,8 @@
|
|
| 1 |
-
"""ShadeNet-3.2 Gradio Space (CPU-friendly: fp16 ONNX backend).
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
import os
|
| 3 |
|
| 4 |
import gradio as gr
|
|
@@ -7,8 +11,7 @@ import onnxruntime as ort
|
|
| 7 |
import spaces
|
| 8 |
from PIL import Image
|
| 9 |
|
| 10 |
-
from inference_utils import
|
| 11 |
-
run_ensemble_onnx)
|
| 12 |
|
| 13 |
MODEL_PATH = os.path.join(os.path.dirname(os.path.abspath(__file__)),
|
| 14 |
"onnx", "model_fp16.onnx")
|
|
@@ -17,16 +20,61 @@ IMAGE_SIZE = 512
|
|
| 17 |
_sess = ort.InferenceSession(MODEL_PATH, providers=["CPUExecutionProvider"])
|
| 18 |
|
| 19 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
@spaces.GPU(duration=60)
|
| 21 |
def decompose(image: Image.Image):
|
| 22 |
"""Returns (grid, input, albedo, shading, depth, normal, recon)."""
|
| 23 |
img_rgb = resize_pad(image.convert("RGB"), IMAGE_SIZE)
|
| 24 |
-
out =
|
| 25 |
maps = extract_maps(out, img_rgb)
|
| 26 |
return (build_grid(img_rgb, out), maps["input"], maps["albedo"],
|
| 27 |
maps["shading"], maps["depth"], maps["normal"], maps["recon"])
|
| 28 |
|
| 29 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
EXAMPLES = []
|
| 31 |
_exdir = os.path.join(os.path.dirname(os.path.abspath(__file__)),
|
| 32 |
"assets", "examples")
|
|
@@ -34,7 +82,7 @@ if os.path.isdir(_exdir):
|
|
| 34 |
EXAMPLES = sorted(os.path.join(_exdir, f) for f in os.listdir(_exdir)
|
| 35 |
if f.lower().endswith((".png", ".jpg", ".jpeg")))
|
| 36 |
|
| 37 |
-
|
| 38 |
fn=decompose,
|
| 39 |
inputs=gr.Image(type="pil", label="Input photo"),
|
| 40 |
outputs=[
|
|
@@ -55,5 +103,30 @@ demo = gr.Interface(
|
|
| 55 |
cache_examples=False,
|
| 56 |
)
|
| 57 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
if __name__ == "__main__":
|
| 59 |
demo.launch()
|
|
|
|
| 1 |
+
"""ShadeNet-3.2 Gradio Space (CPU-friendly: fp16 ONNX backend).
|
| 2 |
+
|
| 3 |
+
Tab 1: decompose a photo into albedo/depth/normal/shading.
|
| 4 |
+
Tab 2: relight the albedo with a user-controlled Lambertian light.
|
| 5 |
+
"""
|
| 6 |
import os
|
| 7 |
|
| 8 |
import gradio as gr
|
|
|
|
| 11 |
import spaces
|
| 12 |
from PIL import Image
|
| 13 |
|
| 14 |
+
from inference_utils import build_grid, extract_maps, pil_to_np, resize_pad
|
|
|
|
| 15 |
|
| 16 |
MODEL_PATH = os.path.join(os.path.dirname(os.path.abspath(__file__)),
|
| 17 |
"onnx", "model_fp16.onnx")
|
|
|
|
| 20 |
_sess = ort.InferenceSession(MODEL_PATH, providers=["CPUExecutionProvider"])
|
| 21 |
|
| 22 |
|
| 23 |
+
def _run_model(img_rgb):
|
| 24 |
+
x = pil_to_np(img_rgb).astype(np.float32)
|
| 25 |
+
return _sess.run(None, {"input_rgb": x})[0] # [1, 8, H, W] in [-1, 1]
|
| 26 |
+
|
| 27 |
+
|
| 28 |
@spaces.GPU(duration=60)
|
| 29 |
def decompose(image: Image.Image):
|
| 30 |
"""Returns (grid, input, albedo, shading, depth, normal, recon)."""
|
| 31 |
img_rgb = resize_pad(image.convert("RGB"), IMAGE_SIZE)
|
| 32 |
+
out = _run_model(img_rgb)
|
| 33 |
maps = extract_maps(out, img_rgb)
|
| 34 |
return (build_grid(img_rgb, out), maps["input"], maps["albedo"],
|
| 35 |
maps["shading"], maps["depth"], maps["normal"], maps["recon"])
|
| 36 |
|
| 37 |
|
| 38 |
+
def _relight(albedo01, normal01, azimuth, elevation, intensity, ambient,
|
| 39 |
+
color):
|
| 40 |
+
"""Lambertian relight. Arrays in [0, 1]; normal frame: x=right, y=up,
|
| 41 |
+
z=toward viewer (camera space). Returns relit RGB in [0, 1]."""
|
| 42 |
+
n = normal01.astype(np.float32) * 2.0 - 1.0
|
| 43 |
+
n = n / np.maximum(np.linalg.norm(n, axis=-1, keepdims=True), 1e-6)
|
| 44 |
+
# image y is down; flip to up-positive for the light math
|
| 45 |
+
n = n * np.array([1.0, -1.0, 1.0], dtype=np.float32)
|
| 46 |
+
az, el = np.deg2rad(float(azimuth)), np.deg2rad(float(elevation))
|
| 47 |
+
lv = np.array([np.cos(el) * np.cos(az), np.sin(el),
|
| 48 |
+
np.cos(el) * np.sin(az)], dtype=np.float32)
|
| 49 |
+
lambert = np.clip((n * lv).sum(-1), 0.0, 1.0)
|
| 50 |
+
shade = float(ambient) + (1.0 - float(ambient)) * float(intensity) * lambert
|
| 51 |
+
col = np.array(color, dtype=np.float32) / 255.0
|
| 52 |
+
return np.clip(albedo01.astype(np.float32) * shade[..., None] *
|
| 53 |
+
col[None, None, :], 0.0, 1.0)
|
| 54 |
+
|
| 55 |
+
|
| 56 |
+
@spaces.GPU(duration=60)
|
| 57 |
+
def relight_init(image):
|
| 58 |
+
"""Run inference once; cache albedo+normal; show default relight."""
|
| 59 |
+
if image is None:
|
| 60 |
+
return None, None
|
| 61 |
+
img_rgb = resize_pad(image.convert("RGB"), IMAGE_SIZE)
|
| 62 |
+
out = _run_model(img_rgb)
|
| 63 |
+
alb = ((out[:, 0:3] + 1.0) / 2.0)[0].transpose(1, 2, 0).astype(np.float32)
|
| 64 |
+
nrm = ((out[:, 4:7] + 1.0) / 2.0)[0].transpose(1, 2, 0).astype(np.float32)
|
| 65 |
+
rel = _relight(alb, nrm, 135.0, 45.0, 1.0, 0.25, (255, 255, 255))
|
| 66 |
+
return (alb, nrm), Image.fromarray((rel * 255).astype("uint8"))
|
| 67 |
+
|
| 68 |
+
|
| 69 |
+
def relight_render(state, azimuth, elevation, intensity, ambient, color):
|
| 70 |
+
"""Pure-numpy re-render from cached maps (no inference)."""
|
| 71 |
+
if state is None:
|
| 72 |
+
return None
|
| 73 |
+
alb, nrm = state
|
| 74 |
+
rel = _relight(alb, nrm, azimuth, elevation, intensity, ambient, color)
|
| 75 |
+
return Image.fromarray((rel * 255).astype("uint8"))
|
| 76 |
+
|
| 77 |
+
|
| 78 |
EXAMPLES = []
|
| 79 |
_exdir = os.path.join(os.path.dirname(os.path.abspath(__file__)),
|
| 80 |
"assets", "examples")
|
|
|
|
| 82 |
EXAMPLES = sorted(os.path.join(_exdir, f) for f in os.listdir(_exdir)
|
| 83 |
if f.lower().endswith((".png", ".jpg", ".jpeg")))
|
| 84 |
|
| 85 |
+
tab1 = gr.Interface(
|
| 86 |
fn=decompose,
|
| 87 |
inputs=gr.Image(type="pil", label="Input photo"),
|
| 88 |
outputs=[
|
|
|
|
| 103 |
cache_examples=False,
|
| 104 |
)
|
| 105 |
|
| 106 |
+
with gr.Blocks() as tab2:
|
| 107 |
+
gr.Markdown("## Relight\nRun inference once, then move the light. "
|
| 108 |
+
"Single-bounce Lambertian — no shadows or interreflections.")
|
| 109 |
+
with gr.Row():
|
| 110 |
+
rin = gr.Image(type="pil", label="Input photo")
|
| 111 |
+
rout = gr.Image(type="pil", label="Relit")
|
| 112 |
+
state = gr.State(value=None)
|
| 113 |
+
with gr.Row():
|
| 114 |
+
az = gr.Slider(0, 360, value=135, step=1, label="Azimuth (deg)")
|
| 115 |
+
el = gr.Slider(0, 90, value=45, step=1, label="Elevation (deg)")
|
| 116 |
+
with gr.Row():
|
| 117 |
+
inten = gr.Slider(0.0, 2.0, value=1.0, step=0.01, label="Intensity")
|
| 118 |
+
amb = gr.Slider(0.0, 1.0, value=0.25, step=0.01, label="Ambient")
|
| 119 |
+
col = gr.ColorPicker(value="#ffffff", label="Light color")
|
| 120 |
+
rin.change(relight_init, inputs=rin, outputs=[state, rout])
|
| 121 |
+
for comp in (az, el, inten, amb, col):
|
| 122 |
+
comp.change(relight_render,
|
| 123 |
+
inputs=[state, az, el, inten, amb, col], outputs=rout)
|
| 124 |
+
|
| 125 |
+
demo = gr.TabbedInterface(
|
| 126 |
+
[tab1, tab2],
|
| 127 |
+
["Decompose", "Relight"],
|
| 128 |
+
title="ShadeNet-3.2 (5M): inverse rendering + relighting",
|
| 129 |
+
)
|
| 130 |
+
|
| 131 |
if __name__ == "__main__":
|
| 132 |
demo.launch()
|