Update app.py
Browse files
app.py
CHANGED
@@ -86,29 +86,25 @@ def estimate_depth(image, model_name="depth-anything/Depth-Anything-V2-Small-hf"
|
|
86 |
|
87 |
return depth_map
|
88 |
|
89 |
-
def apply_depth_aware_blur(image, max_sigma=15, min_sigma=0):
|
90 |
-
"""Apply depth-aware blur
|
91 |
-
# Estimate depth (1 = farthest)
|
92 |
-
depth_map = estimate_depth(image)
|
93 |
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
98 |
for channel in range(3):
|
99 |
-
|
100 |
-
|
101 |
-
sigma=max_sigma
|
102 |
-
)
|
103 |
-
|
104 |
-
# Create 3-channel depth map for blending
|
105 |
-
depth_3d = np.stack([depth_map] * 3, axis=2)
|
106 |
|
107 |
-
#
|
108 |
-
|
109 |
-
result = image_array * (1 - depth_3d) + max_blurred * depth_3d
|
110 |
-
|
111 |
-
return Image.fromarray(result.astype(np.uint8))
|
112 |
|
113 |
def process_image(image, blur_type, sigma=15):
|
114 |
"""Process image based on blur type."""
|
|
|
86 |
|
87 |
return depth_map
|
88 |
|
89 |
+
def apply_depth_aware_blur(image, depth_map, max_sigma=15, min_sigma=0):
|
90 |
+
"""Apply depth-aware blur where farther objects are more blurred."""
|
|
|
|
|
91 |
|
92 |
+
# Ensure depth map is normalized between 0 (near) and 1 (far)
|
93 |
+
depth_map = (depth_map - depth_map.min()) / (depth_map.max() - depth_map.min())
|
94 |
+
|
95 |
+
# Convert image to NumPy array
|
96 |
+
image_array = np.array(image, dtype=np.float32)
|
97 |
+
|
98 |
+
# Initialize blurred image array
|
99 |
+
blurred_image = np.zeros_like(image_array)
|
100 |
+
|
101 |
+
# Apply pixel-wise Gaussian blur based on depth
|
102 |
for channel in range(3):
|
103 |
+
sigma_map = min_sigma + depth_map * (max_sigma - min_sigma) # Map depth to sigma
|
104 |
+
blurred_image[:, :, channel] = gaussian_filter(image_array[:, :, channel], sigma=sigma_map)
|
|
|
|
|
|
|
|
|
|
|
105 |
|
106 |
+
# Convert back to image format
|
107 |
+
return Image.fromarray(blurred_image.astype(np.uint8))
|
|
|
|
|
|
|
108 |
|
109 |
def process_image(image, blur_type, sigma=15):
|
110 |
"""Process image based on blur type."""
|