Update app.py
Browse files
app.py
CHANGED
@@ -1,58 +1,88 @@
|
|
1 |
import gradio as gr
|
2 |
from transformers import pipeline
|
3 |
-
from PIL import Image
|
4 |
import numpy as np
|
|
|
|
|
5 |
|
6 |
-
# Load
|
7 |
-
segmentation_model = pipeline("image-segmentation", model="nvidia/segformer-b1-finetuned-cityscapes-1024-1024")
|
8 |
depth_estimator = pipeline("depth-estimation", model="Intel/zoedepth-nyu-kitti")
|
9 |
|
10 |
-
def process_image(
|
11 |
-
#
|
12 |
-
|
13 |
-
foreground_mask = segmentation_results[-1]["mask"]
|
14 |
|
15 |
-
#
|
16 |
-
|
17 |
-
segmented_output = Image.composite(image, blurred_background, foreground_mask)
|
18 |
-
|
19 |
-
# Step 3: Perform depth estimation
|
20 |
-
depth_results = depth_estimator(image)
|
21 |
depth_map = depth_results["depth"]
|
22 |
|
23 |
-
#
|
24 |
depth_array = np.array(depth_map)
|
25 |
-
normalized_depth = (depth_array - np.min(depth_array)) / (np.max(depth_array) - np.min(depth_array))
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
44 |
fn=process_image,
|
45 |
-
inputs=
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
],
|
50 |
-
outputs=[
|
51 |
-
gr.Image(type="pil", label="Segmented Output with Background Blur"),
|
52 |
-
gr.Image(type="pil", label="Depth Map Visualization"),
|
53 |
-
gr.Image(type="pil", label="Final Output with Selected Blur")
|
54 |
-
],
|
55 |
-
title="Vision Transformer Segmentation & Depth-Based Blur Effects",
|
56 |
-
description="Upload an image and select the type of blur effect (Gaussian or Lens). Adjust the blur intensity using the slider."
|
57 |
)
|
58 |
-
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
from transformers import pipeline
|
3 |
+
from PIL import Image
|
4 |
import numpy as np
|
5 |
+
from scipy.ndimage import gaussian_filter
|
6 |
+
import matplotlib.pyplot as plt
|
7 |
|
8 |
+
# Load the depth estimation model
|
|
|
9 |
depth_estimator = pipeline("depth-estimation", model="Intel/zoedepth-nyu-kitti")
|
10 |
|
11 |
+
def process_image(input_image):
|
12 |
+
# Convert Gradio input (numpy array) to PIL Image
|
13 |
+
input_image = Image.fromarray(input_image.astype('uint8'), 'RGB')
|
|
|
14 |
|
15 |
+
# Perform depth estimation
|
16 |
+
depth_results = depth_estimator(input_image)
|
|
|
|
|
|
|
|
|
17 |
depth_map = depth_results["depth"]
|
18 |
|
19 |
+
# Convert depth map to numpy array and normalize to [0, 1]
|
20 |
depth_array = np.array(depth_map)
|
21 |
+
normalized_depth = (depth_array - np.min(depth_array)) / (np.max(depth_array) - np.min(depth_array))
|
22 |
+
|
23 |
+
# Convert input image to numpy array
|
24 |
+
img_array = np.array(input_image)
|
25 |
+
|
26 |
+
# Create variable blur effect
|
27 |
+
max_blur = 5.0 # Maximum blur radius
|
28 |
+
min_blur = 0.5 # Minimum blur radius to avoid completely sharp areas
|
29 |
+
n_steps = 10 # Number of blur levels
|
30 |
+
|
31 |
+
# Create output array
|
32 |
+
blurred_array = np.zeros_like(img_array, dtype=np.float32)
|
33 |
+
|
34 |
+
# Apply variable blur by processing the image with multiple blur levels
|
35 |
+
for i in range(n_steps):
|
36 |
+
sigma = min_blur + (max_blur - min_blur) * i / (n_steps - 1)
|
37 |
+
# Apply Gaussian blur with current sigma to the whole image
|
38 |
+
blurred_r = gaussian_filter(img_array[:,:,0], sigma=sigma)
|
39 |
+
blurred_g = gaussian_filter(img_array[:,:,1], sigma=sigma)
|
40 |
+
blurred_b = gaussian_filter(img_array[:,:,2], sigma=sigma)
|
41 |
+
blurred_temp = np.stack([blurred_r, blurred_g, blurred_b], axis=2)
|
42 |
+
|
43 |
+
# Create a mask for this blur level
|
44 |
+
lower_bound = i / n_steps
|
45 |
+
upper_bound = (i + 1) / n_steps
|
46 |
+
mask = (normalized_depth >= lower_bound) & (normalized_depth < upper_bound)
|
47 |
+
mask = mask[..., np.newaxis] # Add channel dimension
|
48 |
+
|
49 |
+
# Apply this blur level to the appropriate regions
|
50 |
+
blurred_array = np.where(mask, blurred_temp, blurred_array)
|
51 |
+
|
52 |
+
# Convert back to uint8
|
53 |
+
blurred_image = Image.fromarray(blurred_array.astype('uint8'))
|
54 |
+
|
55 |
+
# Create side-by-side visualization
|
56 |
+
fig, axes = plt.subplots(1, 3, figsize=(15, 5))
|
57 |
+
axes[0].imshow(input_image)
|
58 |
+
axes[0].set_title("Input Image")
|
59 |
+
axes[0].axis("off")
|
60 |
+
|
61 |
+
axes[1].imshow(depth_map, cmap="gray")
|
62 |
+
axes[1].set_title("Depth Map")
|
63 |
+
axes[1].axis("off")
|
64 |
+
|
65 |
+
axes[2].imshow(blurred_image)
|
66 |
+
axes[2].set_title("Variable Blur Output")
|
67 |
+
axes[2].axis("off")
|
68 |
+
|
69 |
+
plt.tight_layout()
|
70 |
+
|
71 |
+
# Save the figure to a temporary file or buffer to display in Gradio
|
72 |
+
output_path = "output.png"
|
73 |
+
plt.savefig(output_path, bbox_inches='tight')
|
74 |
+
plt.close()
|
75 |
+
|
76 |
+
return output_path
|
77 |
+
|
78 |
+
# Define Gradio interface
|
79 |
+
interface = gr.Interface(
|
80 |
fn=process_image,
|
81 |
+
inputs=gr.Image(label="Upload an Image"),
|
82 |
+
outputs=gr.Image(label="Processed Output"),
|
83 |
+
title="Depth-Based Variable Blur App",
|
84 |
+
description="Upload an image to apply a variable blur effect based on depth estimation."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
85 |
)
|
86 |
+
|
87 |
+
# Launch the app
|
88 |
+
interface.launch()
|