File size: 3,018 Bytes
212a439
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import cv2
import numpy as np
import gradio as gr
from PIL import Image

# Dummy segmentation function: replace with your actual segmentation model inference if available.
def segment_foreground(img):
    # Convert input image to a NumPy array
    np_img = np.array(img.convert("RGB"))
    h, w, _ = np_img.shape
    # Create a circular mask as a dummy example
    mask = np.zeros((h, w), dtype=np.uint8)
    center = (w // 2, h // 2)
    radius = min(center) - 10
    cv2.circle(mask, center, radius, (255), thickness=-1)
    return mask

# Function to apply Gaussian blur to the background using the segmentation mask.
def gaussian_blur_background(img, sigma=15):
    mask = segment_foreground(img)
    np_img = np.array(img.convert("RGB"))
    # Apply Gaussian blur to the entire image
    blurred = cv2.GaussianBlur(np_img, (0, 0), sigma)
    # Prepare the mask in 3 channels
    mask_3d = np.stack([mask] * 3, axis=-1) / 255.0
    # Combine the original (foreground) with the blurred (background)
    combined = np_img * mask_3d + blurred * (1 - mask_3d)
    return Image.fromarray(combined.astype(np.uint8))

# Dummy depth estimation function: replace with your actual depth estimation inference.
def estimate_depth(img):
    np_img = np.array(img.convert("RGB"))
    h, w, _ = np_img.shape
    # Create a gradient depth map: top of the image is close (0), bottom is far (1)
    depth = np.tile(np.linspace(0, 1, h)[:, None], (1, w))
    return depth

# Function to apply depth-based lens blur.
def depth_based_blur(img, max_sigma=20):
    depth = estimate_depth(img)
    np_img = np.array(img.convert("RGB"))
    output = np.zeros_like(np_img)
    
    # Normalize the depth map to [0, 1]
    depth_norm = (depth - depth.min()) / (depth.max() - depth.min() + 1e-8)
    
    # Apply a variable Gaussian blur to each row based on the depth value (using the first column as representative)
    for i in range(np_img.shape[0]):
        sigma = max_sigma * depth_norm[i, 0]
        row = cv2.GaussianBlur(np_img[i:i+1, :, :], (0, 0), sigma)
        output[i, :, :] = row
    return Image.fromarray(output.astype(np.uint8))

# Function that dispatches the processing based on user selection.
def process_image(img, effect):
    if effect == "Gaussian Blur Background":
        return gaussian_blur_background(img)
    elif effect == "Depth-based Lens Blur":
        return depth_based_blur(img)
    else:
        return img

# Create the Gradio interface with an image input and a radio button to select the effect.
iface = gr.Interface(
    fn=process_image,
    inputs=[
        gr.inputs.Image(type="pil", label="Input Image"),
        gr.inputs.Radio(["Gaussian Blur Background", "Depth-based Lens Blur"], label="Select Effect")
    ],
    outputs=gr.outputs.Image(type="pil", label="Output Image"),
    title="Blur Effects Demo",
    description="Upload an image and choose an effect to apply either a Gaussian Blur to the background or a Depth-based Lens Blur."
)

if __name__ == "__main__":
    iface.launch()