sjagird1 commited on
Commit
abd2d5d
·
verified ·
1 Parent(s): 047da18

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -16
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, 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."""
 
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."""