Spaces:
Sleeping
Sleeping
update application file
Browse files
app.py
CHANGED
@@ -1,25 +1,49 @@
|
|
1 |
import streamlit as st
|
|
|
2 |
from PIL import Image
|
3 |
-
import cv2
|
4 |
import numpy as np
|
|
|
|
|
5 |
|
6 |
-
|
|
|
|
|
|
|
|
|
7 |
|
8 |
-
|
9 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
10 |
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
|
15 |
-
|
16 |
-
sigma = st.slider("Gaussian Blur Intensity", 5, 50, 15)
|
17 |
-
blurred_image = cv2.GaussianBlur(np.array(image), (0, 0), sigma)
|
18 |
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
st.
|
25 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
+
image_cv = cv2.cvtColor(np.array(image), cv2.COLOR_RGB2BGR)
|
11 |
+
blurred = cv2.GaussianBlur(image_cv, (0, 0), sigma)
|
12 |
+
return Image.fromarray(cv2.cvtColor(blurred, cv2.COLOR_BGR2RGB))
|
13 |
|
14 |
+
# Function to load and process image for segmentation
|
15 |
+
def segment_image(image):
|
16 |
+
feature_extractor = MobileViTFeatureExtractor.from_pretrained("apple/mobilevit-small")
|
17 |
+
model = MobileViTForSemanticSegmentation.from_pretrained("apple/mobilevit-small")
|
18 |
+
|
19 |
+
inputs = feature_extractor(images=image, return_tensors="pt")
|
20 |
+
outputs = model(**inputs)
|
21 |
+
|
22 |
+
# Get segmentation mask
|
23 |
+
logits = outputs.logits
|
24 |
+
upsampled_logits = torch.nn.functional.interpolate(
|
25 |
+
logits, size=image.size[::-1], mode="bilinear", align_corners=False
|
26 |
+
)
|
27 |
+
segmentation = upsampled_logits.argmax(dim=1).squeeze().detach().cpu().numpy()
|
28 |
+
return segmentation
|
29 |
|
30 |
+
# Streamlit interface
|
31 |
+
st.title("Image Segmentation and Blur Effects")
|
32 |
+
st.write("Upload an image to apply segmentation, Gaussian blur, and depth-based blur.")
|
33 |
|
34 |
+
uploaded_file = st.file_uploader("Upload an Image (PNG, JPG, JPEG)", type=["png", "jpg", "jpeg"])
|
|
|
|
|
35 |
|
36 |
+
if uploaded_file:
|
37 |
+
image = Image.open(uploaded_file)
|
38 |
+
st.image(image, caption="Uploaded Image", use_column_width=True)
|
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", use_column_width=True)
|
44 |
+
|
45 |
+
# Perform segmentation
|
46 |
+
if st.button("Perform Segmentation"):
|
47 |
+
st.write("Segmenting the image...")
|
48 |
+
segmentation = segment_image(image)
|
49 |
+
st.image(segmentation, caption="Segmentation Mask", use_column_width=True)
|