Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,107 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import AutoImageProcessor, SegformerForSemanticSegmentation, pipeline
|
| 3 |
+
from PIL import Image, ImageOps, ImageFilter
|
| 4 |
+
import numpy as np
|
| 5 |
+
import torch
|
| 6 |
+
|
| 7 |
+
# ----- Load models once -----
|
| 8 |
+
seg_model_id = "nvidia/segformer-b0-finetuned-ade-512-512"
|
| 9 |
+
depth_model_id = "depth-anything/Depth-Anything-V2-Base-hf"
|
| 10 |
+
|
| 11 |
+
seg_processor = AutoImageProcessor.from_pretrained(seg_model_id)
|
| 12 |
+
seg_model = SegformerForSemanticSegmentation.from_pretrained(seg_model_id)
|
| 13 |
+
|
| 14 |
+
depth_pipe = pipeline(
|
| 15 |
+
task="depth-estimation",
|
| 16 |
+
model=depth_model_id,
|
| 17 |
+
device=0 if torch.cuda.is_available() else -1,
|
| 18 |
+
)
|
| 19 |
+
|
| 20 |
+
# ----- Gaussian background blur using segmentation -----
|
| 21 |
+
def gaussian_background_blur(img: Image.Image) -> Image.Image:
|
| 22 |
+
img = ImageOps.fit(img.convert("RGB"), (512, 512), method=Image.BICUBIC)
|
| 23 |
+
|
| 24 |
+
inputs = seg_processor(images=img, return_tensors="pt")
|
| 25 |
+
with torch.no_grad():
|
| 26 |
+
outputs = seg_model(**inputs)
|
| 27 |
+
|
| 28 |
+
logits = outputs.logits
|
| 29 |
+
upsampled = torch.nn.functional.interpolate(
|
| 30 |
+
logits, size=(512, 512), mode="bilinear", align_corners=False
|
| 31 |
+
)
|
| 32 |
+
seg = upsampled.argmax(dim=1)[0].cpu().numpy()
|
| 33 |
+
|
| 34 |
+
id2label = seg_model.config.id2label
|
| 35 |
+
person_ids = [i for i, label in id2label.items() if "person" in label.lower()]
|
| 36 |
+
mask = np.isin(seg, person_ids).astype(np.uint8)
|
| 37 |
+
|
| 38 |
+
mask_pil = Image.fromarray(mask * 255, mode="L")
|
| 39 |
+
blurred_bg = img.filter(ImageFilter.GaussianBlur(radius=15))
|
| 40 |
+
out = Image.composite(img, blurred_bg, mask_pil)
|
| 41 |
+
return out
|
| 42 |
+
|
| 43 |
+
# ----- Depth-based lens blur -----
|
| 44 |
+
def depth_lens_blur(img: Image.Image) -> Image.Image:
|
| 45 |
+
img = ImageOps.fit(img.convert("RGB"), (512, 512), method=Image.BICUBIC)
|
| 46 |
+
|
| 47 |
+
depth_output = depth_pipe(img)
|
| 48 |
+
depth_tensor = depth_output["predicted_depth"]
|
| 49 |
+
depth_np = depth_tensor.squeeze().cpu().numpy()
|
| 50 |
+
|
| 51 |
+
# normalize, then invert so far = more blur, near = sharp
|
| 52 |
+
d_min, d_max = depth_np.min(), depth_np.max()
|
| 53 |
+
depth_norm = (depth_np - d_min) / (d_max - d_min + 1e-8) # [0,1]
|
| 54 |
+
blur_norm = 1.0 - depth_norm # near≈1 -> 0 blur, far≈0 -> 1 blur
|
| 55 |
+
|
| 56 |
+
max_radius = 15.0
|
| 57 |
+
num_levels = 6
|
| 58 |
+
radii = np.linspace(0, max_radius, num_levels)
|
| 59 |
+
blurred_versions = [
|
| 60 |
+
img.filter(ImageFilter.GaussianBlur(radius=float(r))) for r in radii
|
| 61 |
+
]
|
| 62 |
+
blurred_np = [np.array(b) for b in blurred_versions]
|
| 63 |
+
|
| 64 |
+
level_size = 1.0 / (num_levels - 1)
|
| 65 |
+
blur_levels = np.floor(blur_norm / level_size).astype(np.int32)
|
| 66 |
+
blur_levels = np.clip(blur_levels, 0, num_levels - 1)
|
| 67 |
+
|
| 68 |
+
H, W = blur_levels.shape
|
| 69 |
+
out_np = np.zeros((H, W, 3), dtype=np.uint8)
|
| 70 |
+
|
| 71 |
+
for lvl in range(num_levels):
|
| 72 |
+
mask = blur_levels == lvl
|
| 73 |
+
if not np.any(mask):
|
| 74 |
+
continue
|
| 75 |
+
mask_3c = np.repeat(mask[:, :, None], 3, axis=2)
|
| 76 |
+
out_np[mask_3c] = blurred_np[lvl][mask_3c]
|
| 77 |
+
|
| 78 |
+
return Image.fromarray(out_np)
|
| 79 |
+
|
| 80 |
+
# ----- Gradio UI -----
|
| 81 |
+
def apply_effect(img, mode):
|
| 82 |
+
if img is None:
|
| 83 |
+
return None
|
| 84 |
+
if mode == "Gaussian background blur":
|
| 85 |
+
return gaussian_background_blur(img)
|
| 86 |
+
elif mode == "Depth-based lens blur":
|
| 87 |
+
return depth_lens_blur(img)
|
| 88 |
+
else:
|
| 89 |
+
return img
|
| 90 |
+
|
| 91 |
+
demo = gr.Interface(
|
| 92 |
+
fn=apply_effect,
|
| 93 |
+
inputs=[
|
| 94 |
+
gr.Image(type="pil", label="Upload an image"),
|
| 95 |
+
gr.Radio(
|
| 96 |
+
["Gaussian background blur", "Depth-based lens blur"],
|
| 97 |
+
value="Gaussian background blur",
|
| 98 |
+
label="Effect",
|
| 99 |
+
),
|
| 100 |
+
],
|
| 101 |
+
outputs=gr.Image(label="Output"),
|
| 102 |
+
title="Gaussian & Depth-based Lens Blur Demo",
|
| 103 |
+
description="Upload a selfie or scene and choose Gaussian background blur or depth-based lens blur.",
|
| 104 |
+
)
|
| 105 |
+
|
| 106 |
+
if __name__ == "__main__":
|
| 107 |
+
demo.launch()
|