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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -20
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 with farther objects more blurred."""
91
- # Estimate depth (1 = farthest)
92
- depth_map = estimate_depth(image)
93
 
94
- image_array = np.array(image)
95
-
96
- # Create single blurred version at max sigma for efficiency
97
- max_blurred = np.zeros_like(image_array, dtype=np.float32)
 
 
 
 
 
 
98
  for channel in range(3):
99
- max_blurred[:, :, channel] = gaussian_filter(
100
- image_array[:, :, channel].astype(np.float32),
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
- # Blend between original (near) and blurred (far) based on depth
108
- # Higher depth values (farther) get more blur
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."""