Update app.py
Browse files
app.py
CHANGED
|
@@ -86,25 +86,31 @@ 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,
|
| 90 |
-
"""Apply depth-aware blur
|
|
|
|
|
|
|
| 91 |
|
| 92 |
-
#
|
| 93 |
-
depth_map =
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 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 |
-
|
| 104 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 105 |
|
| 106 |
-
#
|
| 107 |
-
|
|
|
|
|
|
|
| 108 |
|
| 109 |
def process_image(image, blur_type, sigma=15):
|
| 110 |
"""Process image based on blur type."""
|
|
|
|
| 86 |
|
| 87 |
return depth_map
|
| 88 |
|
| 89 |
+
def apply_depth_aware_blur(image, max_sigma=15, min_sigma=0):
|
| 90 |
+
"""Apply depth-aware blur with farther objects more blurred."""
|
| 91 |
+
# Estimate depth (1 = nearest, 0 = farthest)
|
| 92 |
+
depth_map = estimate_depth(image) # Returns 1 for near, 0 for far
|
| 93 |
|
| 94 |
+
# INVERT the depth map (now 1 = farthest, 0 = nearest)
|
| 95 |
+
depth_map = 1 - depth_map
|
| 96 |
+
|
| 97 |
+
image_array = np.array(image)
|
| 98 |
+
|
| 99 |
+
# Create single blurred version at max sigma
|
| 100 |
+
max_blurred = np.zeros_like(image_array, dtype=np.float32)
|
|
|
|
|
|
|
|
|
|
| 101 |
for channel in range(3):
|
| 102 |
+
max_blurred[:, :, channel] = gaussian_filter(
|
| 103 |
+
image_array[:, :, channel].astype(np.float32),
|
| 104 |
+
sigma=max_sigma
|
| 105 |
+
)
|
| 106 |
+
|
| 107 |
+
# Create 3-channel depth map for blending
|
| 108 |
+
depth_3d = np.stack([depth_map] * 3, axis=2)
|
| 109 |
|
| 110 |
+
# Blend: More depth (farther) = more blur
|
| 111 |
+
result = image_array * (1 - depth_3d) + max_blurred * depth_3d
|
| 112 |
+
|
| 113 |
+
return Image.fromarray(result.astype(np.uint8))
|
| 114 |
|
| 115 |
def process_image(image, blur_type, sigma=15):
|
| 116 |
"""Process image based on blur type."""
|