Upload 2 files
Browse files- app.py +92 -0
- requirements.txt +6 -0
app.py
ADDED
@@ -0,0 +1,92 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# app.py
|
2 |
+
import gradio as gr
|
3 |
+
import torch
|
4 |
+
import numpy as np
|
5 |
+
from PIL import Image, ImageFilter
|
6 |
+
from transformers import AutoImageProcessor, AutoModelForSemanticSegmentation, DPTFeatureExtractor, DPTForDepthEstimation
|
7 |
+
import torchvision.transforms as T
|
8 |
+
|
9 |
+
# Load segmentation model
|
10 |
+
seg_model_name = "nvidia/segformer-b0-finetuned-ade-512-512"
|
11 |
+
seg_processor = AutoImageProcessor.from_pretrained(seg_model_name)
|
12 |
+
seg_model = AutoModelForSemanticSegmentation.from_pretrained(seg_model_name)
|
13 |
+
|
14 |
+
# Load depth model
|
15 |
+
depth_model_name = "Intel/dpt-hybrid-midas"
|
16 |
+
depth_processor = DPTFeatureExtractor.from_pretrained(depth_model_name)
|
17 |
+
depth_model = DPTForDepthEstimation.from_pretrained(depth_model_name)
|
18 |
+
|
19 |
+
def process(image):
|
20 |
+
image = image.convert("RGB").resize((512, 512))
|
21 |
+
image_np = np.array(image)
|
22 |
+
|
23 |
+
# --- Segmentation ---
|
24 |
+
inputs = seg_processor(images=image, return_tensors="pt")
|
25 |
+
with torch.no_grad():
|
26 |
+
logits = seg_model(**inputs).logits
|
27 |
+
upsampled_logits = torch.nn.functional.interpolate(
|
28 |
+
logits, size=image.size[::-1], mode="bilinear", align_corners=False
|
29 |
+
)
|
30 |
+
pred_mask = upsampled_logits.argmax(dim=1)[0]
|
31 |
+
foreground_mask = (pred_mask == 12).byte().cpu().numpy() * 255
|
32 |
+
|
33 |
+
# --- Gaussian Blur (Zoom Style) ---
|
34 |
+
blurred_image = image.filter(ImageFilter.GaussianBlur(radius=15))
|
35 |
+
mask_img = Image.fromarray(foreground_mask.astype(np.uint8)).convert("L")
|
36 |
+
gaussian_blur_result = Image.composite(image, blurred_image, mask_img)
|
37 |
+
|
38 |
+
# --- Depth Estimation ---
|
39 |
+
inputs = depth_processor(images=image, return_tensors="pt")
|
40 |
+
with torch.no_grad():
|
41 |
+
depth = depth_model(**inputs).predicted_depth[0]
|
42 |
+
|
43 |
+
depth_resized = torch.nn.functional.interpolate(
|
44 |
+
depth.unsqueeze(0).unsqueeze(0), size=image.size[::-1], mode="bicubic", align_corners=False
|
45 |
+
).squeeze().cpu().numpy()
|
46 |
+
depth_norm = (depth_resized - depth_resized.min()) / (depth_resized.max() - depth_resized.min())
|
47 |
+
depth_norm = 1.0 - depth_norm # Invert so farther = more blur
|
48 |
+
|
49 |
+
# --- Depth-Based Variable Blur ---
|
50 |
+
num_levels = 10
|
51 |
+
max_radius = 20
|
52 |
+
blurred_layers = []
|
53 |
+
for i in range(num_levels):
|
54 |
+
r = (i / (num_levels - 1)) * max_radius
|
55 |
+
blurred = image.filter(ImageFilter.GaussianBlur(radius=r))
|
56 |
+
blurred_layers.append(np.array(blurred, dtype=np.float32))
|
57 |
+
|
58 |
+
depth_indices = depth_norm * (num_levels - 1)
|
59 |
+
output = np.zeros_like(image_np, dtype=np.float32)
|
60 |
+
mask_np = np.array(mask_img)
|
61 |
+
|
62 |
+
for y in range(image_np.shape[0]):
|
63 |
+
for x in range(image_np.shape[1]):
|
64 |
+
if mask_np[y, x] > 128:
|
65 |
+
output[y, x] = image_np[y, x] # preserve foreground
|
66 |
+
else:
|
67 |
+
d = depth_indices[y, x]
|
68 |
+
low = int(np.floor(d))
|
69 |
+
high = min(low + 1, num_levels - 1)
|
70 |
+
alpha = d - low
|
71 |
+
pixel = (1 - alpha) * blurred_layers[low][y, x] + alpha * blurred_layers[high][y, x]
|
72 |
+
output[y, x] = pixel
|
73 |
+
|
74 |
+
depth_blur_result = Image.fromarray(np.uint8(np.clip(output, 0, 255)))
|
75 |
+
|
76 |
+
return image, mask_img, gaussian_blur_result, depth_blur_result
|
77 |
+
|
78 |
+
# Gradio Interface
|
79 |
+
iface = gr.Interface(
|
80 |
+
fn=process,
|
81 |
+
inputs=gr.Image(type="pil", label="Upload Image"),
|
82 |
+
outputs=[
|
83 |
+
gr.Image(type="pil", label="Original Image"),
|
84 |
+
gr.Image(type="pil", label="Foreground Mask"),
|
85 |
+
gr.Image(type="pil", label="Gaussian Background Blur"),
|
86 |
+
gr.Image(type="pil", label="Depth-Based Lens Blur")
|
87 |
+
],
|
88 |
+
title="Image Blur Effects Demo",
|
89 |
+
description="Upload an image to apply Gaussian background blur and depth-based lens blur using Hugging Face models."
|
90 |
+
)
|
91 |
+
|
92 |
+
iface.launch()
|
requirements.txt
ADDED
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
gradio
|
2 |
+
transformers
|
3 |
+
torch
|
4 |
+
torchvision
|
5 |
+
Pillow
|
6 |
+
tqdm
|