File size: 3,751 Bytes
6e49cf1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# app.py
import gradio as gr
import torch
import numpy as np
from PIL import Image, ImageFilter
from transformers import AutoImageProcessor, AutoModelForSemanticSegmentation, DPTFeatureExtractor, DPTForDepthEstimation
import torchvision.transforms as T

# Load segmentation model
seg_model_name = "nvidia/segformer-b0-finetuned-ade-512-512"
seg_processor = AutoImageProcessor.from_pretrained(seg_model_name)
seg_model = AutoModelForSemanticSegmentation.from_pretrained(seg_model_name)

# Load depth model
depth_model_name = "Intel/dpt-hybrid-midas"
depth_processor = DPTFeatureExtractor.from_pretrained(depth_model_name)
depth_model = DPTForDepthEstimation.from_pretrained(depth_model_name)

def process(image):
    image = image.convert("RGB").resize((512, 512))
    image_np = np.array(image)

    # --- Segmentation ---
    inputs = seg_processor(images=image, return_tensors="pt")
    with torch.no_grad():
        logits = seg_model(**inputs).logits
    upsampled_logits = torch.nn.functional.interpolate(
        logits, size=image.size[::-1], mode="bilinear", align_corners=False
    )
    pred_mask = upsampled_logits.argmax(dim=1)[0]
    foreground_mask = (pred_mask == 12).byte().cpu().numpy() * 255

    # --- Gaussian Blur (Zoom Style) ---
    blurred_image = image.filter(ImageFilter.GaussianBlur(radius=15))
    mask_img = Image.fromarray(foreground_mask.astype(np.uint8)).convert("L")
    gaussian_blur_result = Image.composite(image, blurred_image, mask_img)

    # --- Depth Estimation ---
    inputs = depth_processor(images=image, return_tensors="pt")
    with torch.no_grad():
        depth = depth_model(**inputs).predicted_depth[0]

    depth_resized = torch.nn.functional.interpolate(
        depth.unsqueeze(0).unsqueeze(0), size=image.size[::-1], mode="bicubic", align_corners=False
    ).squeeze().cpu().numpy()
    depth_norm = (depth_resized - depth_resized.min()) / (depth_resized.max() - depth_resized.min())
    depth_norm = 1.0 - depth_norm  # Invert so farther = more blur

    # --- Depth-Based Variable Blur ---
    num_levels = 10
    max_radius = 20
    blurred_layers = []
    for i in range(num_levels):
        r = (i / (num_levels - 1)) * max_radius
        blurred = image.filter(ImageFilter.GaussianBlur(radius=r))
        blurred_layers.append(np.array(blurred, dtype=np.float32))

    depth_indices = depth_norm * (num_levels - 1)
    output = np.zeros_like(image_np, dtype=np.float32)
    mask_np = np.array(mask_img)

    for y in range(image_np.shape[0]):
        for x in range(image_np.shape[1]):
            if mask_np[y, x] > 128:
                output[y, x] = image_np[y, x]  # preserve foreground
            else:
                d = depth_indices[y, x]
                low = int(np.floor(d))
                high = min(low + 1, num_levels - 1)
                alpha = d - low
                pixel = (1 - alpha) * blurred_layers[low][y, x] + alpha * blurred_layers[high][y, x]
                output[y, x] = pixel

    depth_blur_result = Image.fromarray(np.uint8(np.clip(output, 0, 255)))

    return image, mask_img, gaussian_blur_result, depth_blur_result

# Gradio Interface
iface = gr.Interface(
    fn=process,
    inputs=gr.Image(type="pil", label="Upload Image"),
    outputs=[
        gr.Image(type="pil", label="Original Image"),
        gr.Image(type="pil", label="Foreground Mask"),
        gr.Image(type="pil", label="Gaussian Background Blur"),
        gr.Image(type="pil", label="Depth-Based Lens Blur")
    ],
    title="Image Blur Effects Demo",
    description="Upload an image to apply Gaussian background blur and depth-based lens blur using Hugging Face models."
)

iface.launch()