Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,76 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import cv2
|
2 |
+
import numpy as np
|
3 |
+
import gradio as gr
|
4 |
+
from PIL import Image
|
5 |
+
|
6 |
+
# Dummy segmentation function: replace with your actual segmentation model inference if available.
|
7 |
+
def segment_foreground(img):
|
8 |
+
# Convert input image to a NumPy array
|
9 |
+
np_img = np.array(img.convert("RGB"))
|
10 |
+
h, w, _ = np_img.shape
|
11 |
+
# Create a circular mask as a dummy example
|
12 |
+
mask = np.zeros((h, w), dtype=np.uint8)
|
13 |
+
center = (w // 2, h // 2)
|
14 |
+
radius = min(center) - 10
|
15 |
+
cv2.circle(mask, center, radius, (255), thickness=-1)
|
16 |
+
return mask
|
17 |
+
|
18 |
+
# Function to apply Gaussian blur to the background using the segmentation mask.
|
19 |
+
def gaussian_blur_background(img, sigma=15):
|
20 |
+
mask = segment_foreground(img)
|
21 |
+
np_img = np.array(img.convert("RGB"))
|
22 |
+
# Apply Gaussian blur to the entire image
|
23 |
+
blurred = cv2.GaussianBlur(np_img, (0, 0), sigma)
|
24 |
+
# Prepare the mask in 3 channels
|
25 |
+
mask_3d = np.stack([mask] * 3, axis=-1) / 255.0
|
26 |
+
# Combine the original (foreground) with the blurred (background)
|
27 |
+
combined = np_img * mask_3d + blurred * (1 - mask_3d)
|
28 |
+
return Image.fromarray(combined.astype(np.uint8))
|
29 |
+
|
30 |
+
# Dummy depth estimation function: replace with your actual depth estimation inference.
|
31 |
+
def estimate_depth(img):
|
32 |
+
np_img = np.array(img.convert("RGB"))
|
33 |
+
h, w, _ = np_img.shape
|
34 |
+
# Create a gradient depth map: top of the image is close (0), bottom is far (1)
|
35 |
+
depth = np.tile(np.linspace(0, 1, h)[:, None], (1, w))
|
36 |
+
return depth
|
37 |
+
|
38 |
+
# Function to apply depth-based lens blur.
|
39 |
+
def depth_based_blur(img, max_sigma=20):
|
40 |
+
depth = estimate_depth(img)
|
41 |
+
np_img = np.array(img.convert("RGB"))
|
42 |
+
output = np.zeros_like(np_img)
|
43 |
+
|
44 |
+
# Normalize the depth map to [0, 1]
|
45 |
+
depth_norm = (depth - depth.min()) / (depth.max() - depth.min() + 1e-8)
|
46 |
+
|
47 |
+
# Apply a variable Gaussian blur to each row based on the depth value (using the first column as representative)
|
48 |
+
for i in range(np_img.shape[0]):
|
49 |
+
sigma = max_sigma * depth_norm[i, 0]
|
50 |
+
row = cv2.GaussianBlur(np_img[i:i+1, :, :], (0, 0), sigma)
|
51 |
+
output[i, :, :] = row
|
52 |
+
return Image.fromarray(output.astype(np.uint8))
|
53 |
+
|
54 |
+
# Function that dispatches the processing based on user selection.
|
55 |
+
def process_image(img, effect):
|
56 |
+
if effect == "Gaussian Blur Background":
|
57 |
+
return gaussian_blur_background(img)
|
58 |
+
elif effect == "Depth-based Lens Blur":
|
59 |
+
return depth_based_blur(img)
|
60 |
+
else:
|
61 |
+
return img
|
62 |
+
|
63 |
+
# Create the Gradio interface with an image input and a radio button to select the effect.
|
64 |
+
iface = gr.Interface(
|
65 |
+
fn=process_image,
|
66 |
+
inputs=[
|
67 |
+
gr.inputs.Image(type="pil", label="Input Image"),
|
68 |
+
gr.inputs.Radio(["Gaussian Blur Background", "Depth-based Lens Blur"], label="Select Effect")
|
69 |
+
],
|
70 |
+
outputs=gr.outputs.Image(type="pil", label="Output Image"),
|
71 |
+
title="Blur Effects Demo",
|
72 |
+
description="Upload an image and choose an effect to apply either a Gaussian Blur to the background or a Depth-based Lens Blur."
|
73 |
+
)
|
74 |
+
|
75 |
+
if __name__ == "__main__":
|
76 |
+
iface.launch()
|