Spaces:
Runtime error
Runtime error
muhammadzain
commited on
Commit
•
1858b2b
1
Parent(s):
1ec58ed
Update app.py
Browse files
app.py
CHANGED
@@ -1,12 +1,10 @@
|
|
1 |
import math
|
2 |
import time
|
3 |
-
import numpy as np
|
4 |
import streamlit as st
|
5 |
from PIL import Image
|
6 |
import cv2
|
7 |
-
|
8 |
-
|
9 |
-
st.set_page_config(layout="wide")
|
10 |
|
11 |
hide_streamlit_style = """
|
12 |
<style>
|
@@ -17,16 +15,36 @@ hide_streamlit_style = """
|
|
17 |
|
18 |
"""
|
19 |
|
20 |
-
st.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
21 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
22 |
def loadModel(n):
|
23 |
super_res = cv2.dnn_superres.DnnSuperResImpl_create()
|
24 |
super_res.readModel('models/ESPCN_x'+n+'.pb')
|
25 |
return super_res
|
26 |
|
27 |
# on removing (show_spinner=False), it will show that fuction is running on web app
|
28 |
-
@st.
|
29 |
-
def upscale(file,task):
|
30 |
with open(file.name, "wb") as f:
|
31 |
f.write(file.getbuffer())
|
32 |
print('No file found, so added in list files')
|
@@ -38,7 +56,37 @@ def upscale(file,task):
|
|
38 |
upscaled_image = super_res.upsample(img)
|
39 |
print('I upscaled upto',task,'times')
|
40 |
cv2.imwrite("processed_"+file.name,upscaled_image)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
41 |
return True
|
|
|
|
|
42 |
else:
|
43 |
req_width,req_height = int(task[0]),int(task[1])
|
44 |
if file.type.split('/')[0] == 'image':
|
@@ -49,6 +97,8 @@ def upscale(file,task):
|
|
49 |
img = cv2.resize(img,(req_width,req_height))
|
50 |
print("I did resizing only!")
|
51 |
cv2.imwrite("processed_" + file.name, img)
|
|
|
|
|
52 |
return True
|
53 |
# rounding off the ratios
|
54 |
w_ratio,h_ratio = math.ceil(w_ratio),math.ceil(h_ratio)
|
@@ -69,9 +119,37 @@ def upscale(file,task):
|
|
69 |
print("I upscale upto", upscale_number , "times and then resize it.")
|
70 |
|
71 |
cv2.imwrite("processed_" + file.name, upscaled_image)
|
72 |
-
|
|
|
73 |
return True
|
74 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
75 |
return "It's second"
|
76 |
|
77 |
|
@@ -81,8 +159,12 @@ if 'disable_opt1' not in st.session_state:
|
|
81 |
st.session_state.disable_opt1 = False
|
82 |
if 'disable_download' not in st.session_state:
|
83 |
st.session_state.disable_download = True
|
|
|
|
|
84 |
|
85 |
|
|
|
|
|
86 |
col1,_,col2 = st.columns([6,1,3],gap="small")
|
87 |
|
88 |
def toggle_state_opt1():
|
@@ -106,19 +188,39 @@ def toggle_state_opt2():
|
|
106 |
# Update the states based on user selection before drawing the widgets in the web page
|
107 |
toggle_state_opt2()
|
108 |
toggle_state_opt1()
|
|
|
|
|
109 |
|
110 |
with col1:
|
111 |
-
file = st.file_uploader("",type=['png','jpeg','jpg','pgm','jpe'])
|
112 |
if file is not None:
|
113 |
# writing file and saving its details in dict for further processing
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
|
|
|
|
|
|
|
|
|
|
118 |
image = Image.open(file)
|
|
|
119 |
st.image(image,caption="Upload Image", use_column_width=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
120 |
elif file.type.split('/')[0] == 'video':
|
121 |
-
st.video(file)
|
|
|
|
|
|
|
|
|
|
|
|
|
122 |
|
123 |
|
124 |
with col2:
|
@@ -131,35 +233,30 @@ with col2:
|
|
131 |
st.markdown("\n")
|
132 |
|
133 |
opt1 = st.checkbox("MULTIPLES OF",key="opt1",value=True,on_change=toggle_state_opt1)
|
134 |
-
st.selectbox("SELECT",
|
135 |
|
136 |
st.markdown("\n")
|
137 |
st.markdown("\n")
|
138 |
opt2 = st.checkbox("CUSTOM SIZE",key="opt2",on_change=toggle_state_opt2)
|
139 |
|
140 |
-
|
141 |
-
|
142 |
-
|
143 |
st.number_input("Width", step=1, min_value=150,max_value=3840, value=900, key="width",disabled=st.session_state.disable_opt2)
|
144 |
|
145 |
-
|
146 |
st.number_input("Height", step=1, min_value=150,max_value=2160, value=900, key="height",disabled=st.session_state.disable_opt2)
|
147 |
|
148 |
st.markdown("\n")
|
149 |
st.markdown("\n")
|
150 |
|
151 |
-
|
152 |
|
153 |
-
|
154 |
|
155 |
-
|
|
|
156 |
if st.session_state.get('opt1') == True:
|
157 |
task = st.session_state.opt1_selBox
|
158 |
else:
|
159 |
task = [st.session_state.width, st.session_state.height]
|
160 |
print(task)
|
161 |
-
st.session_state.disable_download = not upscale(file,task)
|
162 |
-
|
163 |
|
164 |
#print(resulted_file.shape)
|
165 |
|
@@ -170,15 +267,13 @@ with col2:
|
|
170 |
st.session_state.disable_download = True
|
171 |
|
172 |
if st.session_state.disable_download == True:
|
173 |
-
st.button(
|
174 |
else:
|
175 |
with open('processed_'+file.name, "rb") as download_file:
|
176 |
-
st.download_button(label=
|
177 |
file_name= 'processed_'+file.name, mime= "image/png",
|
178 |
-
|
179 |
|
180 |
-
|
181 |
-
st.markdown("\n")
|
182 |
st.markdown("\n")
|
183 |
st.markdown("\n")
|
184 |
st.info("DESCRIPTION : This web app is a free tool designed to upscale or resize image resolution. While the app"+
|
@@ -186,3 +281,4 @@ st.info("DESCRIPTION : This web app is a free tool designed to upscale or res
|
|
186 |
" We welcome your feedback and suggestions, and encourage you to contact us at zain.18j2000@gmail.com "+
|
187 |
"to share your thoughts. Thank you for your interest in our web application, and we look forward "+
|
188 |
"to hearing from you as we continue to work towards making this project a resounding success.")
|
|
|
|
1 |
import math
|
2 |
import time
|
|
|
3 |
import streamlit as st
|
4 |
from PIL import Image
|
5 |
import cv2
|
6 |
+
import os
|
7 |
+
import urllib.request
|
|
|
8 |
|
9 |
hide_streamlit_style = """
|
10 |
<style>
|
|
|
15 |
|
16 |
"""
|
17 |
|
18 |
+
st.set_page_config(layout="wide")
|
19 |
+
|
20 |
+
if os.path.isfile('models/ESPCN_x2.pb'):
|
21 |
+
print(True)
|
22 |
+
else:
|
23 |
+
print("Downloading model x2 ...")
|
24 |
+
urllib.request.urlretrieve("https://github.com/fannymonori/TF-ESPCN/raw/master/export/ESPCN_x2.pb",
|
25 |
+
"models/ESPCN_x2.pb")
|
26 |
+
print("Done!")
|
27 |
+
|
28 |
+
print("Downloading model x3 ...")
|
29 |
+
url2 = "https://github.com/fannymonori/TF-ESPCN/raw/master/export/ESPCN_x3.pb"
|
30 |
+
urllib.request.urlretrieve(url2, "models/ESPCN_x3.pb")
|
31 |
+
print("Done!")
|
32 |
|
33 |
+
print("Downloading model x4 ...")
|
34 |
+
url3 = "https://github.com/fannymonori/TF-ESPCN/raw/master/export/ESPCN_x4.pb"
|
35 |
+
urllib.request.urlretrieve(url3, "models/ESPCN_x4.pb")
|
36 |
+
print("Done!")
|
37 |
+
|
38 |
+
|
39 |
+
@st.cache_resource
|
40 |
def loadModel(n):
|
41 |
super_res = cv2.dnn_superres.DnnSuperResImpl_create()
|
42 |
super_res.readModel('models/ESPCN_x'+n+'.pb')
|
43 |
return super_res
|
44 |
|
45 |
# on removing (show_spinner=False), it will show that fuction is running on web app
|
46 |
+
@st.cache_data(show_spinner=False)
|
47 |
+
def upscale(file,task,_progressBar = None):
|
48 |
with open(file.name, "wb") as f:
|
49 |
f.write(file.getbuffer())
|
50 |
print('No file found, so added in list files')
|
|
|
56 |
upscaled_image = super_res.upsample(img)
|
57 |
print('I upscaled upto',task,'times')
|
58 |
cv2.imwrite("processed_"+file.name,upscaled_image)
|
59 |
+
with st.sidebar:
|
60 |
+
st.success('Done!', icon="✅")
|
61 |
+
return True
|
62 |
+
elif file.type.split('/')[0] == 'video':
|
63 |
+
cap = cv2.VideoCapture(file.name)
|
64 |
+
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
|
65 |
+
fps = int(cap.get(cv2.CAP_PROP_FPS))
|
66 |
+
width = int(cap.get(3))
|
67 |
+
height = int(cap.get(4))
|
68 |
+
writer = cv2.VideoWriter("processed_"+file.name,fourcc,fps,(width*int(task),height*int(task)))
|
69 |
+
length = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
|
70 |
+
step_size = 1.0/length
|
71 |
+
progress = 0
|
72 |
+
|
73 |
+
with st.sidebar:
|
74 |
+
st.info("Operation in progress. Please wait.",icon="ℹ️")
|
75 |
+
my_bar = st.progress(0,text="")
|
76 |
+
|
77 |
+
for percent_complete in range(length):
|
78 |
+
frame = cap.read()[1]
|
79 |
+
frame = super_res.upsample(frame)
|
80 |
+
writer.write(frame)
|
81 |
+
print(progress)
|
82 |
+
progress += step_size
|
83 |
+
my_bar.progress(progress-0.000000001)
|
84 |
+
with st.sidebar:
|
85 |
+
st.success('Done! Thankyou for your patience', icon="✅")
|
86 |
+
return True
|
87 |
return True
|
88 |
+
|
89 |
+
# Second case where custom size is required
|
90 |
else:
|
91 |
req_width,req_height = int(task[0]),int(task[1])
|
92 |
if file.type.split('/')[0] == 'image':
|
|
|
97 |
img = cv2.resize(img,(req_width,req_height))
|
98 |
print("I did resizing only!")
|
99 |
cv2.imwrite("processed_" + file.name, img)
|
100 |
+
with st.sidebar:
|
101 |
+
st.success('Done!', icon="✅")
|
102 |
return True
|
103 |
# rounding off the ratios
|
104 |
w_ratio,h_ratio = math.ceil(w_ratio),math.ceil(h_ratio)
|
|
|
119 |
print("I upscale upto", upscale_number , "times and then resize it.")
|
120 |
|
121 |
cv2.imwrite("processed_" + file.name, upscaled_image)
|
122 |
+
with st.sidebar:
|
123 |
+
st.success('Done!', icon="✅")
|
124 |
return True
|
125 |
|
126 |
+
# If file is video
|
127 |
+
elif file.type.split('/')[0] == 'video':
|
128 |
+
cap = cv2.VideoCapture(file.name)
|
129 |
+
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
|
130 |
+
fps = int(cap.get(cv2.CAP_PROP_FPS))
|
131 |
+
width = int(cap.get(3))
|
132 |
+
height = int(cap.get(4))
|
133 |
+
if height > 2160 or width > 3840:
|
134 |
+
with st.sidebar:
|
135 |
+
st.success("Sorry, I can't processed Video with resolution above 4k. Please select custom size option.!", icon="ℹ️")
|
136 |
+
writer = cv2.VideoWriter("processed_" + file.name, fourcc, fps, (int(task[0]),int(task[1])))
|
137 |
+
length = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
|
138 |
+
step_size = 1.0 / length
|
139 |
+
progress = 0
|
140 |
+
progress_text = "Operation in progress. Please wait."
|
141 |
+
with st.sidebar:
|
142 |
+
st.info("Operation in progress. Please wait.", icon="ℹ️")
|
143 |
+
my_bar = st.progress(0)
|
144 |
+
for percent_complete in range(length):
|
145 |
+
frame = cap.read()[1]
|
146 |
+
frame = cv2.resize(frame, (task[0], task[1]))
|
147 |
+
writer.write(frame)
|
148 |
+
progress += step_size
|
149 |
+
my_bar.progress(progress-0.000000001)
|
150 |
+
with st.sidebar:
|
151 |
+
st.success('Done! Thankyou for your patience', icon="✅")
|
152 |
+
return True
|
153 |
return "It's second"
|
154 |
|
155 |
|
|
|
159 |
st.session_state.disable_opt1 = False
|
160 |
if 'disable_download' not in st.session_state:
|
161 |
st.session_state.disable_download = True
|
162 |
+
if 'disable_proceed' not in st.session_state:
|
163 |
+
st.session_state.disable_proceed = False
|
164 |
|
165 |
|
166 |
+
st.markdown(hide_streamlit_style, unsafe_allow_html=True)
|
167 |
+
|
168 |
col1,_,col2 = st.columns([6,1,3],gap="small")
|
169 |
|
170 |
def toggle_state_opt1():
|
|
|
188 |
# Update the states based on user selection before drawing the widgets in the web page
|
189 |
toggle_state_opt2()
|
190 |
toggle_state_opt1()
|
191 |
+
options = ["2", "3","4"]
|
192 |
+
progressBar = None
|
193 |
|
194 |
with col1:
|
195 |
+
file = st.file_uploader(" ",type=['png','jpeg','jpg','pgm','jpe','mp4','mov'])
|
196 |
if file is not None:
|
197 |
# writing file and saving its details in dict for further processing
|
198 |
+
bytes_data = file.getvalue()
|
199 |
+
file_size = len(bytes_data)
|
200 |
+
print("File size: ",file_size)
|
201 |
+
|
202 |
+
if file.type.split('/')[0] == "image" and file_size > 1550000:
|
203 |
+
st.session_state.disable_proceed = True
|
204 |
+
with st.sidebar:
|
205 |
+
st.info('Sorry, maximum size of image is 1.5MB', icon="ℹ️")
|
206 |
+
elif file.type.split('/')[0] == "image":
|
207 |
image = Image.open(file)
|
208 |
+
st.session_state.disable_proceed = False
|
209 |
st.image(image,caption="Upload Image", use_column_width=True)
|
210 |
+
st.session_state.disable_proceed = False
|
211 |
+
elif file.type.split('/')[0] == 'video' and file_size > 100000000:
|
212 |
+
with st.sidebar:
|
213 |
+
options = ["2", "3"]
|
214 |
+
st.info('Sorry, maximum size of video is 100MB', icon="ℹ️")
|
215 |
+
st.session_state.disable_proceed = True
|
216 |
elif file.type.split('/')[0] == 'video':
|
217 |
+
video = st.video(file)
|
218 |
+
print(type(video))
|
219 |
+
options = ["2", "3"]
|
220 |
+
st.session_state.disable_proceed = False
|
221 |
+
with st.sidebar:
|
222 |
+
st.info('For custom size, currently I can processed video without AI.', icon="ℹ️")
|
223 |
+
|
224 |
|
225 |
|
226 |
with col2:
|
|
|
233 |
st.markdown("\n")
|
234 |
|
235 |
opt1 = st.checkbox("MULTIPLES OF",key="opt1",value=True,on_change=toggle_state_opt1)
|
236 |
+
st.selectbox("SELECT", options,key="opt1_selBox",disabled=st.session_state.disable_opt1)
|
237 |
|
238 |
st.markdown("\n")
|
239 |
st.markdown("\n")
|
240 |
opt2 = st.checkbox("CUSTOM SIZE",key="opt2",on_change=toggle_state_opt2)
|
241 |
|
|
|
|
|
|
|
242 |
st.number_input("Width", step=1, min_value=150,max_value=3840, value=900, key="width",disabled=st.session_state.disable_opt2)
|
243 |
|
|
|
244 |
st.number_input("Height", step=1, min_value=150,max_value=2160, value=900, key="height",disabled=st.session_state.disable_opt2)
|
245 |
|
246 |
st.markdown("\n")
|
247 |
st.markdown("\n")
|
248 |
|
249 |
+
_, dcol, _ = st.columns([1,5,1],gap="small")
|
250 |
|
|
|
251 |
|
252 |
+
|
253 |
+
if st.button(26*" "+"PROCEED"+" "*26,disabled=st.session_state.disable_proceed) and file is not None:
|
254 |
if st.session_state.get('opt1') == True:
|
255 |
task = st.session_state.opt1_selBox
|
256 |
else:
|
257 |
task = [st.session_state.width, st.session_state.height]
|
258 |
print(task)
|
259 |
+
st.session_state.disable_download = not upscale(file,task,progressBar)
|
|
|
260 |
|
261 |
#print(resulted_file.shape)
|
262 |
|
|
|
267 |
st.session_state.disable_download = True
|
268 |
|
269 |
if st.session_state.disable_download == True:
|
270 |
+
st.button(19*" "+"DOWNLOAD FILE"+" "*19,disabled=True)
|
271 |
else:
|
272 |
with open('processed_'+file.name, "rb") as download_file:
|
273 |
+
st.download_button(label=19*" "+"DOWNLOAD FILE"+" "*19, data=download_file,
|
274 |
file_name= 'processed_'+file.name, mime= "image/png",
|
275 |
+
disabled=st.session_state.disable_download)
|
276 |
|
|
|
|
|
277 |
st.markdown("\n")
|
278 |
st.markdown("\n")
|
279 |
st.info("DESCRIPTION : This web app is a free tool designed to upscale or resize image resolution. While the app"+
|
|
|
281 |
" We welcome your feedback and suggestions, and encourage you to contact us at zain.18j2000@gmail.com "+
|
282 |
"to share your thoughts. Thank you for your interest in our web application, and we look forward "+
|
283 |
"to hearing from you as we continue to work towards making this project a resounding success.")
|
284 |
+
|