Spaces:
Sleeping
Sleeping
Update app.py
Browse filesAdd webcam capture, keep original resolution as default model input (remove resize)
app.py
CHANGED
|
@@ -83,20 +83,15 @@ def infer(image: Image.Image, want_metrics: bool, sizing_mode: str, show_L: bool
|
|
| 83 |
w,h = pil.size
|
| 84 |
was_color = not is_grayscale(pil)
|
| 85 |
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
else:
|
| 89 |
-
proc = cv2.resize(rgb, (256,256), interpolation=cv2.INTER_CUBIC); back = (w,h)
|
| 90 |
|
| 91 |
L = to_L(proc)
|
| 92 |
with torch.no_grad():
|
| 93 |
ab = model(L)
|
| 94 |
out = lab_to_rgb(L, ab)
|
| 95 |
|
| 96 |
-
|
| 97 |
-
out = out[:back[1], :back[0]]
|
| 98 |
-
else:
|
| 99 |
-
out = cv2.resize(out, back, interpolation=cv2.INTER_CUBIC)
|
| 100 |
|
| 101 |
# Metrics (Gradio-native numbers)
|
| 102 |
mae = psnr = ssim = None
|
|
@@ -152,24 +147,19 @@ def make_theme():
|
|
| 152 |
THEME = make_theme()
|
| 153 |
|
| 154 |
# ---------------- UI ----------------
|
| 155 |
-
with gr.Blocks(theme=THEME, title="
|
| 156 |
-
gr.Markdown("# 🎨
|
| 157 |
|
| 158 |
with gr.Row():
|
| 159 |
with gr.Column(scale=5):
|
| 160 |
img_in = gr.Image(
|
| 161 |
-
label="Upload grayscale
|
| 162 |
type="pil",
|
| 163 |
image_mode="RGB",
|
| 164 |
height=320,
|
| 165 |
-
sources=["upload", "clipboard"]
|
| 166 |
)
|
| 167 |
with gr.Row():
|
| 168 |
-
sizing = gr.Radio(
|
| 169 |
-
["Resize to 256", "Pad to keep size"],
|
| 170 |
-
value="Resize to 256",
|
| 171 |
-
label="Sizing"
|
| 172 |
-
)
|
| 173 |
show_L = gr.Checkbox(label="Show L-channel", value=False)
|
| 174 |
show_m = gr.Checkbox(label="Show metrics", value=True)
|
| 175 |
with gr.Row():
|
|
@@ -199,14 +189,14 @@ with gr.Blocks(theme=THEME, title="Neural Colorizer") as demo:
|
|
| 199 |
extras = gr.HTML()
|
| 200 |
|
| 201 |
def _go(image, want_metrics, sizing_mode, show_L):
|
| 202 |
-
o, c, mae, psnr, ssim, cmp_html, extra = infer(image, want_metrics,
|
| 203 |
if not want_metrics:
|
| 204 |
mae = psnr = ssim = None
|
| 205 |
return o, c, mae, psnr, ssim, cmp_html, extra
|
| 206 |
|
| 207 |
run.click(
|
| 208 |
_go,
|
| 209 |
-
inputs=[img_in, show_m,
|
| 210 |
outputs=[orig, out, mae_box, psnr_box, ssim_box, compare, extras]
|
| 211 |
)
|
| 212 |
|
|
|
|
| 83 |
w,h = pil.size
|
| 84 |
was_color = not is_grayscale(pil)
|
| 85 |
|
| 86 |
+
|
| 87 |
+
proc, (oh, ow) = pad_to_multiple(rgb, 16); back = (ow, oh)
|
|
|
|
|
|
|
| 88 |
|
| 89 |
L = to_L(proc)
|
| 90 |
with torch.no_grad():
|
| 91 |
ab = model(L)
|
| 92 |
out = lab_to_rgb(L, ab)
|
| 93 |
|
| 94 |
+
out = out[:back[1], :back[0]]
|
|
|
|
|
|
|
|
|
|
| 95 |
|
| 96 |
# Metrics (Gradio-native numbers)
|
| 97 |
mae = psnr = ssim = None
|
|
|
|
| 147 |
THEME = make_theme()
|
| 148 |
|
| 149 |
# ---------------- UI ----------------
|
| 150 |
+
with gr.Blocks(theme=THEME, title="Image Colorizer") as demo:
|
| 151 |
+
gr.Markdown("# 🎨 Image Colorizer")
|
| 152 |
|
| 153 |
with gr.Row():
|
| 154 |
with gr.Column(scale=5):
|
| 155 |
img_in = gr.Image(
|
| 156 |
+
label="Upload grayscale image",
|
| 157 |
type="pil",
|
| 158 |
image_mode="RGB",
|
| 159 |
height=320,
|
| 160 |
+
sources=["upload", "webcam", "clipboard"]
|
| 161 |
)
|
| 162 |
with gr.Row():
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 163 |
show_L = gr.Checkbox(label="Show L-channel", value=False)
|
| 164 |
show_m = gr.Checkbox(label="Show metrics", value=True)
|
| 165 |
with gr.Row():
|
|
|
|
| 189 |
extras = gr.HTML()
|
| 190 |
|
| 191 |
def _go(image, want_metrics, sizing_mode, show_L):
|
| 192 |
+
o, c, mae, psnr, ssim, cmp_html, extra = infer(image, want_metrics, show_L)
|
| 193 |
if not want_metrics:
|
| 194 |
mae = psnr = ssim = None
|
| 195 |
return o, c, mae, psnr, ssim, cmp_html, extra
|
| 196 |
|
| 197 |
run.click(
|
| 198 |
_go,
|
| 199 |
+
inputs=[img_in, show_m, show_L],
|
| 200 |
outputs=[orig, out, mae_box, psnr_box, ssim_box, compare, extras]
|
| 201 |
)
|
| 202 |
|