Spaces:
Sleeping
Sleeping
Upload app.py
Browse files
app.py
CHANGED
@@ -11,7 +11,16 @@ from transformers import pipeline
|
|
11 |
pipe = pipeline(task="depth-estimation", model="depth-anything/Depth-Anything-V2-Small-hf")
|
12 |
|
13 |
|
14 |
-
def
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
15 |
original_image = Image.fromarray(image).convert("RGB")
|
16 |
original_image = original_image.resize((512, 512))
|
17 |
image_np = np.array(original_image)
|
@@ -24,36 +33,65 @@ def apply_depth_aware_blur(image): # Removed blur parameters
|
|
24 |
# Normalize the depth map
|
25 |
normalized_depth_map = (depth - np.min(depth)) / (np.max(depth) - np.min(depth))
|
26 |
|
27 |
-
|
|
|
|
|
|
|
28 |
|
29 |
-
|
30 |
-
max_kernel_size = 35 # Maximum blur kernel size
|
31 |
-
for y in range(512):
|
32 |
-
for x in range(512):
|
33 |
-
blur_amount = normalized_depth_map[y, x]
|
34 |
-
kernel_size = int(blur_amount * max_kernel_size)
|
35 |
-
# Ensure kernel size is odd and at least 1
|
36 |
-
kernel_size = max(1, kernel_size)
|
37 |
-
if kernel_size % 2 == 0:
|
38 |
-
kernel_size += 1
|
39 |
-
|
40 |
-
if kernel_size > 1 and kernel_size <= max_kernel_size:
|
41 |
-
blurred_image[y:y + 1, x:x + 1] = cv2.GaussianBlur(
|
42 |
-
np.array(original_image)[y:y + 1, x:x + 1], (kernel_size, kernel_size), 10
|
43 |
-
)
|
44 |
-
else:
|
45 |
-
blurred_image[y:y + 1, x:x + 1] = np.array(original_image)[y:y + 1, x:x + 1] #Keep original pixel
|
46 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
47 |
|
48 |
return Image.fromarray(blurred_image.astype(np.uint8))
|
49 |
|
50 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
51 |
iface = gr.Interface(
|
52 |
-
fn=
|
53 |
-
inputs=
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
54 |
outputs=gr.Image(label="Blurred Image"),
|
55 |
-
title="Depth-
|
56 |
-
description="Apply
|
|
|
57 |
)
|
58 |
|
59 |
if __name__ == "__main__":
|
|
|
11 |
pipe = pipeline(task="depth-estimation", model="depth-anything/Depth-Anything-V2-Small-hf")
|
12 |
|
13 |
|
14 |
+
def apply_depth_aware_blur_inverse(
|
15 |
+
image,
|
16 |
+
foreground_blur,
|
17 |
+
midground_blur,
|
18 |
+
background_blur,
|
19 |
+
foreground_threshold,
|
20 |
+
midground_lower,
|
21 |
+
midground_upper,
|
22 |
+
background_threshold,
|
23 |
+
):
|
24 |
original_image = Image.fromarray(image).convert("RGB")
|
25 |
original_image = original_image.resize((512, 512))
|
26 |
image_np = np.array(original_image)
|
|
|
33 |
# Normalize the depth map
|
34 |
normalized_depth_map = (depth - np.min(depth)) / (np.max(depth) - np.min(depth))
|
35 |
|
36 |
+
# Create masks (Inverted Logic)
|
37 |
+
foreground_mask = (normalized_depth_map >= foreground_threshold).astype(np.uint8) * 255
|
38 |
+
midground_mask = ((normalized_depth_map < foreground_threshold) & (normalized_depth_map >= background_threshold)).astype(np.uint8) * 255
|
39 |
+
background_mask = (normalized_depth_map < background_threshold).astype(np.uint8) * 255
|
40 |
|
41 |
+
blurred_image = np.copy(np.array(original_image))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
42 |
|
43 |
+
# Apply blur, ensuring kernel size is valid (Inverted Blur)
|
44 |
+
if foreground_blur > 0 and foreground_blur % 2 == 1:
|
45 |
+
blurred_image = np.where(
|
46 |
+
(foreground_mask[..., None] == 255),
|
47 |
+
cv2.GaussianBlur(blurred_image, (foreground_blur, foreground_blur), 10),
|
48 |
+
blurred_image,
|
49 |
+
)
|
50 |
+
if midground_blur > 0 and midground_blur % 2 == 1:
|
51 |
+
blurred_image = np.where(
|
52 |
+
(midground_mask[..., None] == 255),
|
53 |
+
cv2.GaussianBlur(blurred_image, (midground_blur, midground_blur), 8),
|
54 |
+
blurred_image,
|
55 |
+
)
|
56 |
+
if background_blur > 0 and background_blur % 2 == 1:
|
57 |
+
blurred_image = np.where(
|
58 |
+
(background_mask[..., None] == 255),
|
59 |
+
cv2.GaussianBlur(blurred_image, (background_blur, background_blur), 20),
|
60 |
+
blurred_image,
|
61 |
+
)
|
62 |
|
63 |
return Image.fromarray(blurred_image.astype(np.uint8))
|
64 |
|
65 |
|
66 |
+
# Example input values (including defaults)
|
67 |
+
example_image = np.zeros((512, 512, 3), dtype=np.uint8) # Placeholder for an image
|
68 |
+
example_inputs = [
|
69 |
+
example_image,
|
70 |
+
35, # foreground_blur
|
71 |
+
7, # midground_blur
|
72 |
+
15, # background_blur (default)
|
73 |
+
0.6, # foreground_threshold (default)
|
74 |
+
0.6, # midground_lower (default)
|
75 |
+
0.2, # midground_upper (default)
|
76 |
+
0.2, # background_threshold (default)
|
77 |
+
]
|
78 |
+
|
79 |
iface = gr.Interface(
|
80 |
+
fn=apply_depth_aware_blur_inverse, # Changed function name
|
81 |
+
inputs=[
|
82 |
+
gr.Image(label="Input Image"),
|
83 |
+
gr.Slider(1, 51, step=2, label="Foreground Blur Kernel Size"),
|
84 |
+
gr.Slider(1, 51, step=2, label="Midground Blur Kernel Size"),
|
85 |
+
gr.Slider(1, 51, step=2, label="Background Blur Kernel Size"),
|
86 |
+
gr.Slider(0, 1, label="Foreground Threshold"),
|
87 |
+
gr.Slider(0, 1, label="Midground Lower Threshold"),
|
88 |
+
gr.Slider(0, 1, label="Midground Upper Threshold"),
|
89 |
+
gr.Slider(0, 1, label="Background Threshold"),
|
90 |
+
],
|
91 |
outputs=gr.Image(label="Blurred Image"),
|
92 |
+
title="Inverse Depth-Aware Lens Blur App", # Changed title
|
93 |
+
description="Apply inverse depth-based blur to uploaded images using Depth Anything V2. Closer objects are blurred, farther objects are sharper.", # Changed description
|
94 |
+
examples=[example_inputs], # Provide example inputs
|
95 |
)
|
96 |
|
97 |
if __name__ == "__main__":
|