Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,109 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from PIL import Image, ImageFilter
|
3 |
+
import numpy as np
|
4 |
+
import torch
|
5 |
+
import cv2
|
6 |
+
from transformers import AutoImageProcessor, AutoModelForDepthEstimation
|
7 |
+
|
8 |
+
# Load depth estimation model
|
9 |
+
image_processor = AutoImageProcessor.from_pretrained("depth-anything/Depth-Anything-V2-Small-hf")
|
10 |
+
model = AutoModelForDepthEstimation.from_pretrained("depth-anything/Depth-Anything-V2-Small-hf")
|
11 |
+
|
12 |
+
def apply_gaussian_blur(image, mask):
|
13 |
+
|
14 |
+
# Ensure mask is grayscale and resized to match image dimensions
|
15 |
+
mask_pil = Image.fromarray(mask, mode='L')
|
16 |
+
mask_pil = mask_pil.resize(image.size)
|
17 |
+
mask_array = np.array(mask_pil)
|
18 |
+
|
19 |
+
# Create a blurred background
|
20 |
+
blurred_background = image.filter(ImageFilter.GaussianBlur(radius=15))
|
21 |
+
|
22 |
+
# Convert images to NumPy arrays
|
23 |
+
img_array = np.array(image)
|
24 |
+
blurred_array = np.array(blurred_background)
|
25 |
+
|
26 |
+
# Create a boolean mask (foreground = True, background = False)
|
27 |
+
foreground_mask = mask_array > 0
|
28 |
+
foreground_mask_3d = np.stack([foreground_mask] * 3, axis=-1)
|
29 |
+
|
30 |
+
# Blend the original image with the blurred background
|
31 |
+
final_image_array = np.where(foreground_mask_3d, img_array, blurred_array)
|
32 |
+
final_image = Image.fromarray(final_image_array.astype(np.uint8))
|
33 |
+
|
34 |
+
return final_image
|
35 |
+
|
36 |
+
def apply_lens_blur(image):
|
37 |
+
"""Applies depth-based lens blur using a pre-trained model."""
|
38 |
+
|
39 |
+
# Resize image to 512x512 for processing
|
40 |
+
resized_image = image.resize((512, 512))
|
41 |
+
image_np = np.array(resized_image)
|
42 |
+
|
43 |
+
# Prepare image for the model
|
44 |
+
inputs = image_processor(images=resized_image, return_tensors="pt")
|
45 |
+
|
46 |
+
with torch.no_grad():
|
47 |
+
outputs = model(**inputs)
|
48 |
+
predicted_depth = outputs.predicted_depth
|
49 |
+
|
50 |
+
# Interpolate depth map to match the image size
|
51 |
+
prediction = torch.nn.functional.interpolate(
|
52 |
+
predicted_depth.unsqueeze(1),
|
53 |
+
size=resized_image.size[::-1],
|
54 |
+
mode="bicubic",
|
55 |
+
align_corners=False,
|
56 |
+
).squeeze()
|
57 |
+
|
58 |
+
# Convert prediction to a NumPy array
|
59 |
+
depth_map = prediction.cpu().numpy()
|
60 |
+
|
61 |
+
# Normalize the depth map
|
62 |
+
depth_norm = (depth_map - np.min(depth_map)) / (np.max(depth_map) - np.min(depth_map))
|
63 |
+
|
64 |
+
num_blur_levels = 5
|
65 |
+
blurred_layers = []
|
66 |
+
for i in range(num_blur_levels):
|
67 |
+
sigma = i * 0.5
|
68 |
+
if sigma == 0:
|
69 |
+
blurred = image_np
|
70 |
+
else:
|
71 |
+
blurred = cv2.GaussianBlur(image_np, (15, 15), sigmaX=sigma, sigmaY=sigma, borderType=cv2.BORDER_REPLICATE)
|
72 |
+
blurred_layers.append(blurred)
|
73 |
+
|
74 |
+
depth_indices = ((1 - depth_norm) * (num_blur_levels - 1)).astype(np.uint8)
|
75 |
+
|
76 |
+
final_blurred_image = np.zeros_like(image_np)
|
77 |
+
for y in range(image_np.shape[0]):
|
78 |
+
for x in range(image_np.shape[1]):
|
79 |
+
depth_index = depth_indices[y, x]
|
80 |
+
final_blurred_image[y, x] = blurred_layers[depth_index][y, x]
|
81 |
+
|
82 |
+
# Convert the final blurred image back to a PIL Image
|
83 |
+
final_blurred_pil_image = Image.fromarray(final_blurred_image)
|
84 |
+
|
85 |
+
return final_blurred_pil_image
|
86 |
+
|
87 |
+
def process_image(image, mask, blur_type):
|
88 |
+
"""Processes the image based on the selected blur type."""
|
89 |
+
if blur_type == "Gaussian Blur":
|
90 |
+
return apply_gaussian_blur(image, mask)
|
91 |
+
elif blur_type == "Lens Blur":
|
92 |
+
return apply_lens_blur(image)
|
93 |
+
else:
|
94 |
+
return image
|
95 |
+
|
96 |
+
interface = gr.Interface(
|
97 |
+
fn=process_image,
|
98 |
+
inputs=[
|
99 |
+
gr.Image(type="pil", label="Upload an Image"),
|
100 |
+
gr.Image(type="numpy", tool="sketch", shape=(256, 256), label="Draw Mask (Only for Gaussian Blur)"),
|
101 |
+
gr.Radio(["Gaussian Blur", "Lens Blur"], label="Choose Blur Effect")
|
102 |
+
],
|
103 |
+
outputs=gr.Image(type="pil"),
|
104 |
+
title="Gaussian & Lens Blur Effects",
|
105 |
+
description="Upload an image and select either Gaussian blur (with mask) or depth-based lens blur."
|
106 |
+
)
|
107 |
+
|
108 |
+
if __name__ == "__main__":
|
109 |
+
interface.launch()
|