moray1234 commited on
Commit
6996904
·
1 Parent(s): dda5ca9

update application file

Browse files
Files changed (1) hide show
  1. app.py +41 -17
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
- st.title("Segmentation and Blur Effects")
 
 
 
 
7
 
8
- uploaded_image = st.file_uploader("Upload an Image", type=["png", "jpg"])
9
- uploaded_depth = st.file_uploader("Upload a Depth Map", type=["png", "jpg"])
 
 
 
 
 
 
 
 
 
 
 
 
 
10
 
11
- if uploaded_image and uploaded_depth:
12
- image = Image.open(uploaded_image)
13
- depth_map = Image.open(uploaded_depth)
14
 
15
- # Gaussian Blur
16
- sigma = st.slider("Gaussian Blur Intensity", 5, 50, 15)
17
- blurred_image = cv2.GaussianBlur(np.array(image), (0, 0), sigma)
18
 
19
- # Depth-based Blur (Dummy example)
20
- depth_array = np.array(depth_map).astype(np.float32)
21
- depth_array = cv2.normalize(depth_array, None, 0, sigma, cv2.NORM_MINMAX)
22
- blurred_depth = cv2.GaussianBlur(np.array(image), (0, 0), int(np.max(depth_array)))
23
-
24
- st.image(blurred_image, caption="Gaussian Blur", use_column_width=True)
25
- st.image(blurred_depth, caption="Depth-based Blur", use_column_width=True)
 
 
 
 
 
 
 
 
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)