-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
157 lines (137 loc) · 7.99 KB
/
Copy pathapp.py
File metadata and controls
157 lines (137 loc) · 7.99 KB
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
import streamlit as st
import cv2
import os
from PIL import Image
from /content/webapp/app_funcs.py import segment_image, segment_video, segment_live_feed, download_success
st.set_page_config(
page_title="Instance Segmentator",
page_icon="🖼",
layout="centered",
initial_sidebar_state="auto",
)
top_image = Image.open('static/banner_top.png')
bottom_image = Image.open('static/banner_bottom.png')
main_image = Image.open('static/main_banner.png')
upload_path = "uploads/"
download_path = "downloads/"
st.sidebar.image(top_image,use_column_width='auto')
segmentation_type = st.sidebar.selectbox('Select segmentation type 🎯',["Image","Video","Live Feed"])
st.sidebar.image(bottom_image,use_column_width='auto')
st.image(main_image,use_column_width='auto')
st.title("🖼📷 Instance Segmentator 🏡🏙")
if segmentation_type == "Image":
st.info('✨ Supports all popular image formats 📷 - PNG, JPG, BMP 😉')
uploaded_file = st.file_uploader("Upload Image 🖼", type=["png","jpg","bmp","jpeg"])
if uploaded_file is not None:
with open(os.path.join(upload_path,uploaded_file.name),"wb") as f:
f.write((uploaded_file).getbuffer())
with st.spinner(f"Working... 💫"):
uploaded_image = os.path.abspath(os.path.join(upload_path,uploaded_file.name))
downloaded_image = os.path.abspath(os.path.join(download_path,str("segmented_"+uploaded_file.name)))
segment_image(uploaded_image, downloaded_image)
final_image = Image.open(downloaded_image)
print("Opening ",final_image)
st.markdown("---")
st.image(final_image, caption='This is how your final image looks like 😉')
with open(downloaded_image, "rb") as file:
if uploaded_file.name.endswith('.jpg') or uploaded_file.name.endswith('.JPG'):
if st.download_button(
label="Download Segmented Image 📷",
data=file,
file_name=str("segmented_"+uploaded_file.name),
mime='image/jpg'
):
download_success()
if uploaded_file.name.endswith('.jpeg') or uploaded_file.name.endswith('.JPEG'):
if st.download_button(
label="Download Segmented Image 📷",
data=file,
file_name=str("segmented_"+uploaded_file.name),
mime='image/jpeg'
):
download_success()
if uploaded_file.name.endswith('.png') or uploaded_file.name.endswith('.PNG'):
if st.download_button(
label="Download Segmented Image 📷",
data=file,
file_name=str("segmented_"+uploaded_file.name),
mime='image/png'
):
download_success()
if uploaded_file.name.endswith('.bmp') or uploaded_file.name.endswith('.BMP'):
if st.download_button(
label="Download Segmented Image 📷",
data=file,
file_name=str("segmented_"+uploaded_file.name),
mime='image/bmp'
):
download_success()
else:
st.warning('⚠ Please upload your Image file 😯')
if segmentation_type == "Video":
st.info('✨ Supports all popular video formats 🎥 - MP4, MOV, MKV, AVI 😉')
uploaded_file = st.file_uploader("Upload Video 📽", type=["mp4","avi","mov","mkv"])
if uploaded_file is not None:
with open(os.path.join(upload_path,uploaded_file.name),"wb") as f:
f.write((uploaded_file).getbuffer())
with st.spinner(f"Working... 💫"):
uploaded_video = os.path.abspath(os.path.join(upload_path,uploaded_file.name))
downloaded_video = os.path.abspath(os.path.join(download_path,str("segmented_"+uploaded_file.name)))
segment_video(uploaded_video, downloaded_video)
final_video = open(downloaded_video, 'rb')
video_bytes = final_video.read()
print("Opening ",final_video)
st.markdown("---")
with open(downloaded_video, "rb") as file:
if uploaded_file.name.endswith('.avi') or uploaded_file.name.endswith('.AVI'):
st.success('✅ Your results are ready !! 😲')
if st.download_button(
label="Download Segmented Video 📽",
data=file,
file_name=str("segmented_"+uploaded_file.name),
mime='video/x-msvideo'
):
download_success()
if uploaded_file.name.endswith('.mp4') or uploaded_file.name.endswith('.MP4'):
st.success('✅ Your results are ready !! 😲')
if st.download_button(
label="Download Segmented Video 📽",
data=file,
file_name=str("segmented_"+uploaded_file.name),
mime='video/mp4'
):
download_success()
if uploaded_file.name.endswith('.mov') or uploaded_file.name.endswith('.MOV'):
st.success('✅ Your results are ready !! 😲')
if st.download_button(
label="Download Segmented Video 📽",
data=file,
file_name=str("segmented_"+uploaded_file.name),
mime='video/quicktime'
):
download_success()
if uploaded_file.name.endswith('.mkv') or uploaded_file.name.endswith('.MKV'):
st.success('✅ Your results are ready !! 😲')
if st.download_button(
label="Download Segmented Video 📽",
data=file,
file_name=str("segmented_"+uploaded_file.name),
mime='video/x-matroska'
):
download_success()
else:
st.warning('⚠ Please upload your Video file 😯')
if segmentation_type == "Live Feed":
st.info('✨ The Live Feed from Web-Camera will take some time to load up 🎦')
live_feed = st.checkbox('Start Web-Camera ✅')
FRAME_WINDOW = st.image([])
camera = cv2.VideoCapture(0)
if live_feed:
_, frame = camera.read()
frame = segment_live_feed(frame)
FRAME_WINDOW.image(frame)
else:
camera.release()
cv2.destroyAllWindows()
st.warning('⚠ The Web-Camera is currently disabled. 😯')
st.markdown("<br><hr><center>Made with ❤️ by <a href='mailto:ralhanprateek@gmail.com?subject=Instance Segmentator WebApp!&body=Please specify the issue you are facing with the app.'><strong>Prateek Ralhan</strong></a></center><hr>", unsafe_allow_html=True)