Spaces:
Sleeping
Sleeping
added lens blur
Browse files- app.py +60 -22
- requirements.txt +1 -2
app.py
CHANGED
@@ -1,18 +1,28 @@
|
|
1 |
import streamlit as st
|
2 |
-
from transformers import MobileViTFeatureExtractor, MobileViTForSemanticSegmentation
|
3 |
-
from PIL import Image
|
4 |
import numpy as np
|
5 |
-
import cv2
|
6 |
-
import torch
|
7 |
|
8 |
# Function to apply Gaussian Blur
|
9 |
-
def apply_gaussian_blur(image, sigma=15):
|
10 |
-
|
11 |
-
|
12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
13 |
|
14 |
# Function to load and process image for segmentation
|
15 |
-
def
|
16 |
feature_extractor = MobileViTFeatureExtractor.from_pretrained("apple/mobilevit-small")
|
17 |
model = MobileViTForSemanticSegmentation.from_pretrained("apple/mobilevit-small")
|
18 |
|
@@ -21,11 +31,35 @@ def segment_image(image):
|
|
21 |
|
22 |
# Get segmentation mask
|
23 |
logits = outputs.logits
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
29 |
|
30 |
# Streamlit interface
|
31 |
st.title("Image Segmentation and Blur Effects")
|
@@ -35,15 +69,19 @@ uploaded_file = st.file_uploader("Upload an Image (PNG, JPG, JPEG)", type=["png"
|
|
35 |
|
36 |
if uploaded_file:
|
37 |
image = Image.open(uploaded_file)
|
38 |
-
|
|
|
|
|
|
|
39 |
|
40 |
# Apply Gaussian Blur
|
41 |
sigma = st.slider("Gaussian Blur Intensity", 5, 50, 15)
|
42 |
-
blurred_image = apply_gaussian_blur(image, sigma)
|
43 |
-
st.image(blurred_image, caption="Gaussian Blurred Image",
|
44 |
|
45 |
-
# Perform
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
|
|
|
1 |
import streamlit as st
|
2 |
+
from transformers import MobileViTFeatureExtractor, MobileViTForSemanticSegmentation, pipeline
|
3 |
+
from PIL import Image, ImageFilter, ImageOps
|
4 |
import numpy as np
|
|
|
|
|
5 |
|
6 |
# Function to apply Gaussian Blur
|
7 |
+
def apply_gaussian_blur(image, predicted_mask, sigma=15):
|
8 |
+
print(f"Predicted Mask: {predicted_mask}")
|
9 |
+
mask = Image.fromarray(
|
10 |
+
(predicted_mask.cpu().numpy() * 255).astype(np.uint8)
|
11 |
+
).resize(image.size).convert('L')
|
12 |
+
|
13 |
+
foreground = Image.composite(
|
14 |
+
image, Image.new('RGB', image.size), mask
|
15 |
+
)
|
16 |
+
background = Image.composite(
|
17 |
+
image.filter(ImageFilter.GaussianBlur(sigma)), image,
|
18 |
+
ImageOps.invert(mask)
|
19 |
+
)
|
20 |
+
|
21 |
+
final_image = Image.composite(foreground, background, mask)
|
22 |
+
return final_image
|
23 |
|
24 |
# Function to load and process image for segmentation
|
25 |
+
def get_segmentation_mask(image):
|
26 |
feature_extractor = MobileViTFeatureExtractor.from_pretrained("apple/mobilevit-small")
|
27 |
model = MobileViTForSemanticSegmentation.from_pretrained("apple/mobilevit-small")
|
28 |
|
|
|
31 |
|
32 |
# Get segmentation mask
|
33 |
logits = outputs.logits
|
34 |
+
predicted_mask = logits.argmax(1).squeeze(0)
|
35 |
+
return predicted_mask
|
36 |
+
|
37 |
+
def get_depth_mask(image):
|
38 |
+
pipe = pipeline(task="depth-estimation", model="Intel/dpt-beit-base-384")
|
39 |
+
|
40 |
+
result = pipe(image)
|
41 |
+
depth_map = result["depth"]
|
42 |
+
|
43 |
+
return np.array(depth_map)
|
44 |
+
|
45 |
+
def add_depth_based_blur(depth_array, image):
|
46 |
+
depth_normalized = (depth_array - depth_array.min()) / (depth_array.max() - depth_array.min()) * 15
|
47 |
+
|
48 |
+
image_array = np.array(image)
|
49 |
+
|
50 |
+
blurred_images = [
|
51 |
+
np.array(image.filter(ImageFilter.GaussianBlur(radius)))
|
52 |
+
for radius in range(16)
|
53 |
+
]
|
54 |
+
|
55 |
+
depth_blurred_array = np.zeros_like(image_array)
|
56 |
+
|
57 |
+
for i in range(depth_normalized.shape[0]):
|
58 |
+
for j in range(depth_normalized.shape[1]):
|
59 |
+
blur_lvl = 15 - int(depth_normalized[i, j])
|
60 |
+
depth_blurred_array[i, j] = blurred_images[blur_lvl][i, j]
|
61 |
+
|
62 |
+
return Image.fromarray(depth_blurred_array.astype(np.uint8))
|
63 |
|
64 |
# Streamlit interface
|
65 |
st.title("Image Segmentation and Blur Effects")
|
|
|
69 |
|
70 |
if uploaded_file:
|
71 |
image = Image.open(uploaded_file)
|
72 |
+
image = image.resize((512, 512))
|
73 |
+
st.image(image, caption="Uploaded Image", use_container_width=True)
|
74 |
+
|
75 |
+
predicted_mask = get_segmentation_mask(image)
|
76 |
|
77 |
# Apply Gaussian Blur
|
78 |
sigma = st.slider("Gaussian Blur Intensity", 5, 50, 15)
|
79 |
+
blurred_image = apply_gaussian_blur(image, predicted_mask, sigma)
|
80 |
+
st.image(blurred_image, caption="Gaussian Blurred Image", use_container_width=True)
|
81 |
|
82 |
+
# Perform lens blur
|
83 |
+
st.write("Calculating depth and applying lens blur...")
|
84 |
+
with st.spinner("Applying lens blur... This might take a few moments."):
|
85 |
+
depth_array = get_depth_mask(image)
|
86 |
+
lens_blurred_img = add_depth_based_blur(depth_array, image)
|
87 |
+
st.image(lens_blurred_img, caption="Lens Blur Effect Image", use_container_width=True)
|
requirements.txt
CHANGED
@@ -1,5 +1,4 @@
|
|
1 |
streamlit
|
2 |
transformers
|
3 |
torch
|
4 |
-
pillow
|
5 |
-
opencv-python
|
|
|
1 |
streamlit
|
2 |
transformers
|
3 |
torch
|
4 |
+
pillow
|
|