Spaces:
Running
Running
File size: 5,742 Bytes
29ab20e f42ceb6 29ab20e f42ceb6 29ab20e f42ceb6 29ab20e f42ceb6 29ab20e 48e3649 29ab20e 48e3649 29ab20e |
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 158 159 160 |
import math
import streamlit as st
from PIL import Image
import cv2
import os
import threading
hide_streamlit_style = """
<style>
#root > div:nth-child(1) > div > div > div > div > section > div {padding-top: 0rem;
padding-left: 1%;
}
</style>
"""
st.set_page_config(layout="wide")
def loadModel(n):
super_res = cv2.dnn_superres.DnnSuperResImpl_create()
super_res.readModel('models/ESPCN_x'+n+'.pb')
return super_res
# on removing (show_spinner=False), it will show that function is running on web app
@st.experimental_memo(show_spinner=False)
def upscale(file, task):
with open(file.name, "wb") as f:
f.write(file.getbuffer())
print('No file found, so added in list files')
if isinstance(task, str):
super_res = loadModel(task)
super_res.setModel('espcn', int(task))
if file.type.split('/')[0] == 'image':
img = cv2.imread(file.name)
upscaled_image = super_res.upsample(img)
print('I upscaled up to', task, 'times')
output_file_path = "processed_" + file.name.split('.')[0] + ".png"
cv2.imwrite(output_file_path, upscaled_image)
with st.sidebar:
st.success('Done!', icon="β
")
return output_file_path
elif file.type.split('/')[0] == 'video':
# μμ μ
μ€μΌμΌλ§ μ½λλ μλ΅ (νμμ μΆκ° κ°λ₯)
pass
return True
else:
# 컀μ€ν
μ¬μ΄μ¦ μ½λ (νμμ μΆκ°)
pass
return True
if 'disable_opt2' not in st.session_state:
st.session_state.disable_opt2 = True
if 'disable_opt1' not in st.session_state:
st.session_state.disable_opt1 = False
if 'disable_download' not in st.session_state:
st.session_state.disable_download = True
if 'disable_proceed' not in st.session_state:
st.session_state.disable_proceed = False
st.markdown(hide_streamlit_style, unsafe_allow_html=True)
col1, _, col2 = st.columns([6, 1, 3], gap="small")
def toggle_state_opt1():
if st.session_state.get("opt1") == True:
st.session_state.opt2 = False
st.session_state.disable_opt2 = True
else:
st.session_state.opt2 = True
st.session_state.disable_opt2 = False
def toggle_state_opt2():
if st.session_state.get("opt2") == True:
st.session_state.opt1 = False
st.session_state.disable_opt1 = True
else:
st.session_state.opt1 = True
st.session_state.disable_opt1 = False
toggle_state_opt2()
toggle_state_opt1()
options = ["2", "3", "4"]
with col1:
file = st.file_uploader(" ", type=['png', 'jpeg', 'jpg', 'pgm', 'jpe', 'mp4', 'mov'])
if file is not None:
bytes_data = file.getvalue()
file_size = len(bytes_data)
print("File size: ", file_size)
if file.type.split('/')[0] == "image" and file_size > 1550000:
st.session_state.disable_proceed = True
with st.sidebar:
st.info('Sorry, maximum size of image is 1.5MB', icon="βΉοΈ")
elif file.type.split('/')[0] == "image":
image = Image.open(file)
st.session_state.disable_proceed = False
st.image(image, caption="Upload Image", use_column_width=True)
st.session_state.disable_proceed = False
elif file.type.split('/')[0] == 'video' and file_size > 200000000:
with st.sidebar:
options = ["2", "3"]
st.info('Sorry, maximum size of video is 200MB', icon="βΉοΈ")
st.session_state.disable_proceed = True
elif file.type.split('/')[0] == 'video':
video = st.video(file)
print(type(video))
options = ["2", "3"]
st.session_state.disable_proceed = False
with st.sidebar:
st.info('For custom size, currently I can process video without AI.', icon="βΉοΈ")
with col2:
st.markdown("\n")
st.markdown("\n")
st.markdown("\n")
st.subheader(" UPSCALE RESOLUTION UP TO")
st.markdown("\n")
st.markdown("\n")
opt1 = st.checkbox("MULTIPLES OF", key="opt1", value=True, on_change=toggle_state_opt1)
st.selectbox("SELECT", options, key="opt1_selBox", disabled=st.session_state.disable_opt1)
st.markdown("\n")
st.markdown("\n")
opt2 = st.checkbox("CUSTOM SIZE", key="opt2", on_change=toggle_state_opt2)
st.number_input("Width", step=1, min_value=150, max_value=3840, value=900, key="width", disabled=st.session_state.disable_opt2)
st.number_input("Height", step=1, min_value=150, max_value=2160, value=900, key="height", disabled=st.session_state.disable_opt2)
st.markdown("\n")
st.markdown("\n")
if st.button(15*" "+"PROCEED"+" "*15, disabled=st.session_state.disable_proceed) and file is not None:
if st.session_state.get('opt1') == True:
task = st.session_state.opt1_selBox
else:
task = [st.session_state.width, st.session_state.height]
print(task)
result_file_path = upscale(file, task)
if result_file_path:
st.session_state.disable_download = False
st.session_state.result_file_path = result_file_path
st.markdown("\n")
st.markdown("\n")
if file is None:
st.session_state.disable_download = True
if st.session_state.disable_download:
st.button(13*" "+"DOWNLOAD"+" "*13, disabled=True)
else:
with open(st.session_state.result_file_path, "rb") as download_file:
st.download_button(label=13*" "+"DOWNLOAD"+" "*13, data=download_file, file_name='processed_' + file.name.split('.')[0] + ".png", mime="image/png")
st.markdown("\n")
st.markdown("\n") |