Spaces:
Sleeping
Sleeping
File size: 8,736 Bytes
08e91dc | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 | from __future__ import annotations
from pathlib import Path
import os
import tempfile
import gradio as gr
import numpy as np
from PIL import Image
from edge_aware_denoiser import (
add_gaussian_noise,
as_float_image,
denoise,
quality_metrics,
to_uint8,
)
TITLE = "Image Denoiser"
METHOD_NOTES = {
"Perona–Malik": "Edge-preserving diffusion.",
"Total Variation": "Good for strong outlines and flat areas.",
"Bilateral": "Useful for light noise in photos.",
}
def save_png(image: np.ndarray, name: str) -> str:
path = Path(tempfile.mkstemp(prefix=name, suffix=".png")[1])
Image.fromarray(to_uint8(image)).save(path)
return str(path)
def build_report(original, noisy, result, info, has_reference: bool) -> str:
lines = [
"### Run details",
f"- Method: {info.method}",
f"- Estimated noise: {info.estimated_noise_sigma * 255:.2f}",
]
if info.effective_kappa is not None:
lines.append(f"- κ: {info.effective_kappa:.4f}")
if info.iterations_completed is not None:
ending = "stopped early" if info.converged else "completed"
lines.append(f"- Iterations: {info.iterations_completed} ({ending})")
if has_reference:
noisy_scores = quality_metrics(original, noisy)
result_scores = quality_metrics(original, result)
lines.extend([
"",
"| Metric | Noisy | Denoised |",
"|---|---:|---:|",
f"| RMSE | {noisy_scores['rmse']:.4f} | {result_scores['rmse']:.4f} |",
f"| PSNR | {noisy_scores['psnr']:.2f} dB | {result_scores['psnr']:.2f} dB |",
f"| SSIM | {noisy_scores['ssim']:.4f} | {result_scores['ssim']:.4f} |",
f"| Edge similarity | {noisy_scores['edge_similarity']:.1f}% | {result_scores['edge_similarity']:.1f}% |",
])
else:
edge_score = quality_metrics(noisy, result)["edge_similarity"]
lines.extend(["", f"- Edge similarity to input: {edge_score:.1f}%"])
return "\n".join(lines)
def process(
image: np.ndarray | None,
add_noise: bool,
noise_sigma: float,
seed: int,
method: str,
conductivity: str,
auto_kappa: bool,
manual_kappa: float,
kappa_multiplier: float,
iterations: int,
dt: float,
strength: float,
spatial_radius: float,
early_stop_tolerance: float,
):
if image is None:
raise gr.Error("Choose an image first.")
original = as_float_image(image)
use_noise = add_noise and noise_sigma > 0
noisy = (
add_gaussian_noise(original, noise_sigma, int(seed))
if use_noise
else original.copy()
)
try:
result, info = denoise(
noisy,
method=method,
conductivity=conductivity,
auto_kappa=auto_kappa,
manual_kappa=manual_kappa,
kappa_multiplier=kappa_multiplier,
iterations=int(iterations),
dt=dt,
strength=strength,
spatial_radius=spatial_radius,
early_stop_tolerance=early_stop_tolerance,
)
except ValueError as error:
raise gr.Error(str(error)) from error
return (
to_uint8(original),
to_uint8(noisy),
to_uint8(result),
build_report(original, noisy, result, info, use_noise),
save_png(noisy, "noisy_"),
save_png(result, "denoised_"),
)
def update_controls(method: str, auto_kappa: bool):
use_pm = method == "Perona–Malik"
return (
gr.update(visible=use_pm),
gr.update(visible=use_pm),
gr.update(visible=use_pm and not auto_kappa),
gr.update(visible=use_pm),
gr.update(visible=method in {"Total Variation", "Bilateral"}),
gr.update(visible=method == "Bilateral"),
gr.update(value=METHOD_NOTES[method]),
)
def build_app() -> gr.Blocks:
with gr.Blocks(title=TITLE) as app:
gr.Markdown("# Image Denoiser\nUpload an image, choose a method, and run it.")
with gr.Row():
with gr.Column(scale=1, min_width=320):
input_image = gr.Image(
label="Image",
type="numpy",
image_mode="RGB",
)
add_noise = gr.Checkbox(label="Add Gaussian noise", value=False)
noise_sigma = gr.Slider(0, 50, value=10, step=1, label="Noise σ")
seed = gr.Number(value=42, precision=0, label="Seed")
method = gr.Radio(
["Perona–Malik", "Total Variation", "Bilateral"],
value="Perona–Malik",
label="Method",
)
method_note = gr.Markdown(METHOD_NOTES["Perona–Malik"])
conductivity = gr.Dropdown(
["Exponential", "Reciprocal"],
value="Exponential",
label="Conductivity",
)
auto_kappa = gr.Checkbox(label="Estimate κ automatically", value=True)
manual_kappa = gr.Slider(
0.01,
0.30,
value=0.08,
step=0.005,
label="κ",
visible=False,
)
kappa_multiplier = gr.Slider(
1.0,
5.0,
value=2.8,
step=0.1,
label="κ multiplier",
)
iterations = gr.Slider(1, 100, value=25, step=1, label="Iterations")
dt = gr.Slider(0.01, 0.25, value=0.15, step=0.01, label="Time step", visible=True)
strength = gr.Slider(
0.01,
0.35,
value=0.10,
step=0.01,
label="Strength",
visible=False,
)
spatial_radius = gr.Slider(
1,
10,
value=3,
step=1,
label="Spatial radius",
visible=False,
)
early_stop_tolerance = gr.Slider(
0.00001,
0.005,
value=0.0001,
step=0.00001,
label="Early-stop tolerance",
)
run = gr.Button("Run", variant="primary")
with gr.Column(scale=2):
with gr.Row():
original_view = gr.Image(label="Original", interactive=False)
noisy_view = gr.Image(label="Input", interactive=False)
result_view = gr.Image(label="Result", interactive=False)
report = gr.Markdown("Choose an image and run denoising.")
with gr.Row():
noisy_file = gr.File(label="Download input")
result_file = gr.File(label="Download result")
method.change(
update_controls,
inputs=[method, auto_kappa],
outputs=[
conductivity,
auto_kappa,
manual_kappa,
kappa_multiplier,
strength,
spatial_radius,
method_note,
],
)
auto_kappa.change(
update_controls,
inputs=[method, auto_kappa],
outputs=[
conductivity,
auto_kappa,
manual_kappa,
kappa_multiplier,
strength,
spatial_radius,
method_note,
],
)
run.click(
process,
inputs=[
input_image,
add_noise,
noise_sigma,
seed,
method,
conductivity,
auto_kappa,
manual_kappa,
kappa_multiplier,
iterations,
dt,
strength,
spatial_radius,
early_stop_tolerance,
],
outputs=[
original_view,
noisy_view,
result_view,
report,
noisy_file,
result_file,
],
)
return app
if __name__ == "__main__":
port = int(os.getenv("PORT", "7860"))
build_app().queue(default_concurrency_limit=2, max_size=8).launch(
server_name="0.0.0.0",
server_port=port,
inbrowser=os.getenv("LOCAL_BROWSER", "0") == "1",
show_error=True,
)
|