Spaces:
Running
Running
enhanced depth map constraint
Browse files
app.py
CHANGED
@@ -123,13 +123,18 @@ def normalize_depth_map(depth_map):
|
|
123 |
# return result
|
124 |
|
125 |
def apply_depth_based_blur(image, depth_map, max_blur=25):
|
126 |
-
"""Apply variable Gaussian blur based on depth"""
|
127 |
# Create output image
|
128 |
result = np.zeros_like(image)
|
129 |
|
130 |
# Normalize depth map
|
131 |
normalized_depth = normalize_depth_map(depth_map)
|
132 |
|
|
|
|
|
|
|
|
|
|
|
133 |
# Apply blur with intensity proportional to depth
|
134 |
for blur_size in range(1, max_blur + 1, 2): # Odd numbers for kernel size
|
135 |
# Create a mask for pixels that should receive this blur level
|
@@ -161,6 +166,7 @@ def apply_depth_based_blur(image, depth_map, max_blur=25):
|
|
161 |
|
162 |
return result
|
163 |
|
|
|
164 |
# Process function for Gradio
|
165 |
# def process_image(input_image, blur_effect_type, blur_strength, target_class):
|
166 |
# # Load models if not already loaded
|
@@ -207,7 +213,7 @@ def apply_depth_based_blur(image, depth_map, max_blur=25):
|
|
207 |
# else:
|
208 |
# return image_np # Return original if no effect selected
|
209 |
|
210 |
-
def process_image(input_image, blur_effect_type, blur_strength, target_class):
|
211 |
try:
|
212 |
# Load models if not already loaded
|
213 |
if not hasattr(process_image, "models_loaded"):
|
@@ -234,10 +240,21 @@ def process_image(input_image, blur_effect_type, blur_strength, target_class):
|
|
234 |
# Resize for depth estimation
|
235 |
depth_input = cv2.resize(image_np, (512, 512))
|
236 |
|
|
|
|
|
|
|
237 |
# Get depth map
|
238 |
-
depth_result = process_image.depth_estimator(
|
239 |
depth_map = np.array(depth_result["depth"])
|
240 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
241 |
# Apply depth-based blur
|
242 |
result = apply_depth_based_blur(depth_input, depth_map, max_blur=blur_strength)
|
243 |
|
|
|
123 |
# return result
|
124 |
|
125 |
def apply_depth_based_blur(image, depth_map, max_blur=25):
|
126 |
+
"""Apply variable Gaussian blur based on depth with enhanced effect"""
|
127 |
# Create output image
|
128 |
result = np.zeros_like(image)
|
129 |
|
130 |
# Normalize depth map
|
131 |
normalized_depth = normalize_depth_map(depth_map)
|
132 |
|
133 |
+
# Enhance depth contrast to make the effect more noticeable
|
134 |
+
# Apply gamma correction to increase contrast between foreground and background
|
135 |
+
gamma = 0.5 # Values less than 1 will enhance contrast
|
136 |
+
normalized_depth = np.power(normalized_depth, gamma)
|
137 |
+
|
138 |
# Apply blur with intensity proportional to depth
|
139 |
for blur_size in range(1, max_blur + 1, 2): # Odd numbers for kernel size
|
140 |
# Create a mask for pixels that should receive this blur level
|
|
|
166 |
|
167 |
return result
|
168 |
|
169 |
+
|
170 |
# Process function for Gradio
|
171 |
# def process_image(input_image, blur_effect_type, blur_strength, target_class):
|
172 |
# # Load models if not already loaded
|
|
|
213 |
# else:
|
214 |
# return image_np # Return original if no effect selected
|
215 |
|
216 |
+
def process_image(input_image, blur_effect_type, blur_strength, target_class, show_depth_map=False):
|
217 |
try:
|
218 |
# Load models if not already loaded
|
219 |
if not hasattr(process_image, "models_loaded"):
|
|
|
240 |
# Resize for depth estimation
|
241 |
depth_input = cv2.resize(image_np, (512, 512))
|
242 |
|
243 |
+
# Convert to PIL image for the depth estimator
|
244 |
+
depth_input_pil = Image.fromarray(depth_input)
|
245 |
+
|
246 |
# Get depth map
|
247 |
+
depth_result = process_image.depth_estimator(depth_input_pil)
|
248 |
depth_map = np.array(depth_result["depth"])
|
249 |
|
250 |
+
# If show_depth_map is True, return a visualization of the depth map
|
251 |
+
if show_depth_map:
|
252 |
+
# Normalize depth map for visualization
|
253 |
+
depth_vis = normalize_depth_map(depth_map)
|
254 |
+
# Convert to colormap for better visualization
|
255 |
+
depth_colormap = cv2.applyColorMap((depth_vis * 255).astype(np.uint8), cv2.COLORMAP_PLASMA)
|
256 |
+
return depth_colormap
|
257 |
+
|
258 |
# Apply depth-based blur
|
259 |
result = apply_depth_based_blur(depth_input, depth_map, max_blur=blur_strength)
|
260 |
|