Update app.py
Browse files
app.py
CHANGED
@@ -14,30 +14,86 @@ def preprocess_image(image):
|
|
14 |
image = image.resize((512, 512))
|
15 |
return image
|
16 |
|
17 |
-
def segment_image(image, model_name="
|
18 |
-
"""
|
19 |
-
|
20 |
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
|
25 |
-
|
26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
27 |
|
28 |
# Run inference
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
41 |
|
42 |
def apply_gaussian_blur(image, sigma=15):
|
43 |
"""Apply Gaussian blur to the background."""
|
@@ -78,19 +134,15 @@ def estimate_depth(image, model_name="depth-anything/Depth-Anything-V2-Small-hf"
|
|
78 |
return depth_map
|
79 |
|
80 |
def apply_depth_aware_blur(image, max_sigma=10, min_sigma=0):
|
81 |
-
"""Apply depth-aware blur to the image."""
|
82 |
# Estimate depth
|
83 |
depth_map = estimate_depth(image)
|
84 |
|
85 |
image_array = np.array(image)
|
86 |
blurred = np.zeros_like(image_array, dtype=np.float32)
|
87 |
-
|
88 |
-
inverted_depth_map = 1 - depth_map
|
89 |
|
90 |
-
#
|
91 |
sigmas = np.interp(depth_map, [0, 1], [min_sigma, max_sigma])
|
92 |
-
|
93 |
-
|
94 |
|
95 |
# Precompute blurred layers
|
96 |
blur_stack = {}
|
@@ -117,20 +169,7 @@ def apply_depth_aware_blur(image, max_sigma=10, min_sigma=0):
|
|
117 |
|
118 |
return Image.fromarray(blurred.astype(np.uint8))
|
119 |
|
120 |
-
|
121 |
-
"""Process image based on blur type."""
|
122 |
-
# Preprocess image
|
123 |
-
pil_image = preprocess_image(image)
|
124 |
-
|
125 |
-
# Apply appropriate blur
|
126 |
-
if blur_type == "Gaussian Background Blur":
|
127 |
-
result = apply_gaussian_blur(pil_image, sigma)
|
128 |
-
elif blur_type == "Depth-Aware Lens Blur":
|
129 |
-
result = apply_depth_aware_blur(pil_image, max_sigma=sigma)
|
130 |
-
else:
|
131 |
-
result = pil_image
|
132 |
-
|
133 |
-
return result
|
134 |
|
135 |
# Gradio Interface
|
136 |
def create_blur_app():
|
|
|
14 |
image = image.resize((512, 512))
|
15 |
return image
|
16 |
|
17 |
+
def segment_image(image, model_name="yolov8n-seg"):
|
18 |
+
"""
|
19 |
+
Perform instance segmentation on the input image using YOLO segmentation model.
|
20 |
|
21 |
+
Args:
|
22 |
+
image (PIL.Image): Input image
|
23 |
+
model_name (str): Name of the YOLO segmentation model
|
24 |
|
25 |
+
Returns:
|
26 |
+
numpy.ndarray: Segmentation mask with instance segmentation
|
27 |
+
"""
|
28 |
+
from ultralytics import YOLO
|
29 |
+
import numpy as np
|
30 |
+
import torch
|
31 |
+
|
32 |
+
# Load the YOLO segmentation model
|
33 |
+
model = YOLO(model_name)
|
34 |
|
35 |
# Run inference
|
36 |
+
results = model(image)
|
37 |
+
|
38 |
+
# Create a blank mask
|
39 |
+
mask = np.zeros(image.size[::-1], dtype=np.uint8)
|
40 |
+
|
41 |
+
# Process each detected object
|
42 |
+
for result in results:
|
43 |
+
# Get masks for all detected objects
|
44 |
+
masks = result.masks
|
45 |
+
|
46 |
+
if masks is not None:
|
47 |
+
# Convert masks to numpy and add to the overall mask
|
48 |
+
for single_mask in masks:
|
49 |
+
# Convert mask to numpy and resize if needed
|
50 |
+
mask_array = single_mask.data.cpu().numpy().squeeze()
|
51 |
+
mask_array = (mask_array > 0.5).astype(np.uint8)
|
52 |
+
|
53 |
+
# If mask size doesn't match image, resize
|
54 |
+
if mask_array.shape != mask.shape:
|
55 |
+
from PIL import Image
|
56 |
+
mask_array = np.array(
|
57 |
+
Image.fromarray(mask_array).resize(
|
58 |
+
image.size[::-1],
|
59 |
+
Image.NEAREST
|
60 |
+
)
|
61 |
+
)
|
62 |
+
|
63 |
+
# Add this mask to the overall mask
|
64 |
+
mask = np.maximum(mask, mask_array)
|
65 |
+
|
66 |
+
return mask
|
67 |
+
|
68 |
+
def process_image(image, blur_type, sigma=15):
|
69 |
+
"""Process image based on blur type."""
|
70 |
+
# Preprocess image
|
71 |
+
pil_image = preprocess_image(image)
|
72 |
+
|
73 |
+
# Apply appropriate blur
|
74 |
+
if blur_type == "Gaussian Background Blur":
|
75 |
+
# Get segmentation mask
|
76 |
+
segmentation_mask = segment_image(pil_image)
|
77 |
+
|
78 |
+
# Convert to 3-channel mask
|
79 |
+
mask_3d = np.stack([segmentation_mask] * 3, axis=2)
|
80 |
+
|
81 |
+
# Apply Gaussian blur
|
82 |
+
image_array = np.array(pil_image)
|
83 |
+
blurred = np.zeros_like(image_array)
|
84 |
+
for channel in range(3):
|
85 |
+
blurred[:, :, channel] = gaussian_filter(image_array[:, :, channel], sigma=sigma)
|
86 |
+
|
87 |
+
# Combine original and blurred images
|
88 |
+
result = image_array * mask_3d + blurred * (1 - mask_3d)
|
89 |
+
result = Image.fromarray(result.astype(np.uint8))
|
90 |
+
|
91 |
+
elif blur_type == "Depth-Aware Lens Blur":
|
92 |
+
result = apply_depth_aware_blur(pil_image, max_sigma=sigma)
|
93 |
+
else:
|
94 |
+
result = pil_image
|
95 |
+
|
96 |
+
return result
|
97 |
|
98 |
def apply_gaussian_blur(image, sigma=15):
|
99 |
"""Apply Gaussian blur to the background."""
|
|
|
134 |
return depth_map
|
135 |
|
136 |
def apply_depth_aware_blur(image, max_sigma=10, min_sigma=0):
|
137 |
+
"""Apply depth-aware blur to the image (REVERSED version)."""
|
138 |
# Estimate depth
|
139 |
depth_map = estimate_depth(image)
|
140 |
|
141 |
image_array = np.array(image)
|
142 |
blurred = np.zeros_like(image_array, dtype=np.float32)
|
|
|
|
|
143 |
|
144 |
+
# REVERSED: Now we use depth_map directly (no inversion) so farther objects get more blur
|
145 |
sigmas = np.interp(depth_map, [0, 1], [min_sigma, max_sigma])
|
|
|
|
|
146 |
|
147 |
# Precompute blurred layers
|
148 |
blur_stack = {}
|
|
|
169 |
|
170 |
return Image.fromarray(blurred.astype(np.uint8))
|
171 |
|
172 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
173 |
|
174 |
# Gradio Interface
|
175 |
def create_blur_app():
|