Spaces:
Sleeping
Sleeping
final touches added
Browse files
app.py
CHANGED
@@ -82,54 +82,95 @@ def normalize_depth_map(depth_map):
|
|
82 |
depth_min = depth_map.min()
|
83 |
depth_max = depth_map.max()
|
84 |
if depth_min == depth_max:
|
85 |
-
return np.zeros_like(depth_map)
|
86 |
normalized_depth = (depth_map - depth_min) / (depth_max - depth_min)
|
87 |
-
|
|
|
88 |
|
89 |
# Apply depth-based blur
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
90 |
def apply_depth_based_blur(image, depth_map, max_blur=25):
|
91 |
-
"""Apply variable Gaussian blur based on depth with
|
92 |
-
#
|
93 |
-
result =
|
94 |
|
95 |
# Normalize depth map
|
96 |
normalized_depth = normalize_depth_map(depth_map)
|
97 |
|
98 |
-
#
|
99 |
-
#
|
100 |
-
|
101 |
-
normalized_depth = np.power(normalized_depth, gamma)
|
102 |
|
103 |
-
#
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
blurred = cv2.GaussianBlur(image, (blur_size, blur_size), 0)
|
121 |
-
# Add blurred result to output image
|
122 |
-
mask_3d = np.stack([mask] * 3, axis=2)
|
123 |
-
result += (blurred * mask_3d).astype(np.uint8)
|
124 |
-
except Exception as e:
|
125 |
-
print(f"Error applying blur with size {blur_size}: {e}")
|
126 |
continue
|
127 |
-
|
128 |
-
#
|
129 |
-
|
130 |
-
|
|
|
|
|
|
|
131 |
|
132 |
-
return result
|
133 |
|
134 |
|
135 |
# Process function for Gradio
|
|
|
82 |
depth_min = depth_map.min()
|
83 |
depth_max = depth_map.max()
|
84 |
if depth_min == depth_max:
|
85 |
+
return np.zeros_like(depth_map)
|
86 |
normalized_depth = (depth_map - depth_min) / (depth_max - depth_min)
|
87 |
+
# Explicitly invert the depth map - this is the critical fix
|
88 |
+
return 1.0 - normalized_depth
|
89 |
|
90 |
# Apply depth-based blur
|
91 |
+
# def apply_depth_based_blur(image, depth_map, max_blur=25):
|
92 |
+
# """Apply variable Gaussian blur based on depth with enhanced effect"""
|
93 |
+
# # Create output image
|
94 |
+
# result = np.zeros_like(image)
|
95 |
+
|
96 |
+
# # Normalize depth map
|
97 |
+
# normalized_depth = normalize_depth_map(depth_map)
|
98 |
+
|
99 |
+
# # Enhance depth contrast to make the effect more noticeable
|
100 |
+
# # Apply gamma correction to increase contrast between foreground and background
|
101 |
+
# gamma = 0.5 # Values less than 1 will enhance contrast
|
102 |
+
# normalized_depth = np.power(normalized_depth, gamma)
|
103 |
+
|
104 |
+
# # Apply blur with intensity proportional to depth
|
105 |
+
# for blur_size in range(1, max_blur + 1, 2): # Odd numbers for kernel size
|
106 |
+
# # Create a mask for pixels that should receive this blur level
|
107 |
+
# if blur_size == 1:
|
108 |
+
# mask = (normalized_depth <= blur_size / max_blur).astype(np.float32)
|
109 |
+
# else:
|
110 |
+
# lower_bound = (blur_size - 2) / max_blur
|
111 |
+
# upper_bound = blur_size / max_blur
|
112 |
+
# mask = ((normalized_depth > lower_bound) & (normalized_depth <= upper_bound)).astype(np.float32)
|
113 |
+
|
114 |
+
# # Skip if no pixels in this range
|
115 |
+
# if not np.any(mask):
|
116 |
+
# continue
|
117 |
+
|
118 |
+
# # Apply Gaussian blur with current kernel size
|
119 |
+
# if blur_size > 1: # No need to blur with kernel size 1
|
120 |
+
# try:
|
121 |
+
# blurred = cv2.GaussianBlur(image, (blur_size, blur_size), 0)
|
122 |
+
# # Add blurred result to output image
|
123 |
+
# mask_3d = np.stack([mask] * 3, axis=2)
|
124 |
+
# result += (blurred * mask_3d).astype(np.uint8)
|
125 |
+
# except Exception as e:
|
126 |
+
# print(f"Error applying blur with size {blur_size}: {e}")
|
127 |
+
# continue
|
128 |
+
# else:
|
129 |
+
# # For blur_size=1, just copy the original pixels
|
130 |
+
# mask_3d = np.stack([mask] * 3, axis=2)
|
131 |
+
# result += (image * mask_3d).astype(np.uint8)
|
132 |
+
|
133 |
+
# return result
|
134 |
+
|
135 |
def apply_depth_based_blur(image, depth_map, max_blur=25):
|
136 |
+
"""Apply variable Gaussian blur based on depth with foreground in focus"""
|
137 |
+
# Start with a copy of the original image
|
138 |
+
result = image.copy().astype(float)
|
139 |
|
140 |
# Normalize depth map
|
141 |
normalized_depth = normalize_depth_map(depth_map)
|
142 |
|
143 |
+
# The depth map from intel/dpt-large is already set up so closer objects
|
144 |
+
# have lower values. We need to define a threshold to separate foreground from background.
|
145 |
+
foreground_threshold = 0.3 # Adjust this value based on your depth map
|
|
|
146 |
|
147 |
+
# Create increasingly blurred versions of the image
|
148 |
+
blurred_images = []
|
149 |
+
blur_strengths = []
|
150 |
+
|
151 |
+
# Generate progressively blurred versions
|
152 |
+
for blur_size in range(3, max_blur + 1, 4): # Use larger step for efficiency
|
153 |
+
blur_strengths.append(blur_size)
|
154 |
+
blurred = cv2.GaussianBlur(image, (blur_size, blur_size), 0)
|
155 |
+
blurred_images.append(blurred)
|
156 |
+
|
157 |
+
# Apply the appropriate blur level based on depth
|
158 |
+
for y in range(image.shape[0]):
|
159 |
+
for x in range(image.shape[1]):
|
160 |
+
depth_val = normalized_depth[y, x]
|
161 |
+
|
162 |
+
# Keep foreground sharp
|
163 |
+
if depth_val <= foreground_threshold:
|
|
|
|
|
|
|
|
|
|
|
|
|
164 |
continue
|
165 |
+
|
166 |
+
# Apply blur based on depth - background gets more blur
|
167 |
+
relative_depth = (depth_val - foreground_threshold) / (1 - foreground_threshold)
|
168 |
+
blur_index = min(int(relative_depth * len(blurred_images)), len(blurred_images) - 1)
|
169 |
+
|
170 |
+
# Apply the appropriate blur level
|
171 |
+
result[y, x] = blurred_images[blur_index][y, x]
|
172 |
|
173 |
+
return result.astype(np.uint8)
|
174 |
|
175 |
|
176 |
# Process function for Gradio
|