File size: 3,211 Bytes
dda5ca9
bb6f4d2
 
dda5ca9
 
6996904
bb6f4d2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
dda5ca9
6996904
bb6f4d2
6996904
 
 
 
 
 
 
 
bb6f4d2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
dda5ca9
6996904
 
 
dda5ca9
6996904
dda5ca9
6996904
 
bb6f4d2
 
 
 
6996904
 
 
bb6f4d2
 
6996904
bb6f4d2
 
 
 
 
 
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
import streamlit as st
from transformers import MobileViTFeatureExtractor, MobileViTForSemanticSegmentation, pipeline
from PIL import Image, ImageFilter, ImageOps
import numpy as np

# Function to apply Gaussian Blur
def apply_gaussian_blur(image, predicted_mask, sigma=15):
    print(f"Predicted Mask: {predicted_mask}")
    mask = Image.fromarray(
        (predicted_mask.cpu().numpy() * 255).astype(np.uint8)
    ).resize(image.size).convert('L')

    foreground = Image.composite(
        image, Image.new('RGB', image.size), mask
    )
    background = Image.composite(
        image.filter(ImageFilter.GaussianBlur(sigma)), image,
        ImageOps.invert(mask)
    )

    final_image = Image.composite(foreground, background, mask)
    return final_image

# Function to load and process image for segmentation
def get_segmentation_mask(image):
    feature_extractor = MobileViTFeatureExtractor.from_pretrained("apple/mobilevit-small")
    model = MobileViTForSemanticSegmentation.from_pretrained("apple/mobilevit-small")
    
    inputs = feature_extractor(images=image, return_tensors="pt")
    outputs = model(**inputs)
    
    # Get segmentation mask
    logits = outputs.logits
    predicted_mask = logits.argmax(1).squeeze(0)
    return predicted_mask

def get_depth_mask(image):
    pipe = pipeline(task="depth-estimation", model="Intel/dpt-beit-base-384")

    result = pipe(image)
    depth_map = result["depth"]

    return np.array(depth_map)

def add_depth_based_blur(depth_array, image):
    depth_normalized = (depth_array - depth_array.min()) / (depth_array.max() - depth_array.min()) * 15

    image_array = np.array(image)

    blurred_images = [
        np.array(image.filter(ImageFilter.GaussianBlur(radius)))
        for radius in range(16)
    ]

    depth_blurred_array = np.zeros_like(image_array)

    for i in range(depth_normalized.shape[0]):
        for j in range(depth_normalized.shape[1]):
            blur_lvl = 15 - int(depth_normalized[i, j])
            depth_blurred_array[i, j] = blurred_images[blur_lvl][i, j]

    return Image.fromarray(depth_blurred_array.astype(np.uint8))

# Streamlit interface
st.title("Image Segmentation and Blur Effects")
st.write("Upload an image to apply segmentation, Gaussian blur, and depth-based blur.")

uploaded_file = st.file_uploader("Upload an Image (PNG, JPG, JPEG)", type=["png", "jpg", "jpeg"])

if uploaded_file:
    image = Image.open(uploaded_file)
    image = image.resize((512, 512))
    st.image(image, caption="Uploaded Image", use_container_width=True)

    predicted_mask = get_segmentation_mask(image)
    
    # Apply Gaussian Blur
    sigma = st.slider("Gaussian Blur Intensity", 5, 50, 15)
    blurred_image = apply_gaussian_blur(image, predicted_mask, sigma)
    st.image(blurred_image, caption="Gaussian Blurred Image", use_container_width=True)
    
    # Perform lens blur
    st.write("Calculating depth and applying lens blur...")
    with st.spinner("Applying lens blur... This might take a few moments."):
        depth_array = get_depth_mask(image)
        lens_blurred_img = add_depth_based_blur(depth_array, image)
    st.image(lens_blurred_img, caption="Lens Blur Effect Image", use_container_width=True)