Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,160 +1,30 @@
|
|
1 |
-
import
|
2 |
-
import streamlit as st
|
3 |
-
from PIL import Image
|
4 |
import cv2
|
5 |
-
import
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
#
|
11 |
-
|
12 |
-
|
13 |
-
|
|
|
|
|
|
|
14 |
"""
|
15 |
|
16 |
-
|
17 |
-
|
18 |
-
def loadModel(n):
|
19 |
-
super_res = cv2.dnn_superres.DnnSuperResImpl_create()
|
20 |
-
super_res.readModel('models/ESPCN_x'+n+'.pb')
|
21 |
-
return super_res
|
22 |
-
|
23 |
-
# on removing (show_spinner=False), it will show that function is running on web app
|
24 |
-
@st.experimental_memo(show_spinner=False)
|
25 |
-
def upscale(file, task):
|
26 |
-
with open(file.name, "wb") as f:
|
27 |
-
f.write(file.getbuffer())
|
28 |
-
print('No file found, so added in list files')
|
29 |
-
if isinstance(task, str):
|
30 |
-
super_res = loadModel(task)
|
31 |
-
super_res.setModel('espcn', int(task))
|
32 |
-
if file.type.split('/')[0] == 'image':
|
33 |
-
img = cv2.imread(file.name)
|
34 |
-
upscaled_image = super_res.upsample(img)
|
35 |
-
print('I upscaled up to', task, 'times')
|
36 |
-
output_file_path = "processed_" + file.name.split('.')[0] + ".png"
|
37 |
-
cv2.imwrite(output_file_path, upscaled_image)
|
38 |
-
with st.sidebar:
|
39 |
-
st.success('Done!', icon="โ
")
|
40 |
-
return output_file_path
|
41 |
-
elif file.type.split('/')[0] == 'video':
|
42 |
-
# ์์ ์
์ค์ผ์ผ๋ง ์ฝ๋๋ ์๋ต (ํ์์ ์ถ๊ฐ ๊ฐ๋ฅ)
|
43 |
-
pass
|
44 |
-
return True
|
45 |
-
else:
|
46 |
-
# ์ปค์คํ
์ฌ์ด์ฆ ์ฝ๋ (ํ์์ ์ถ๊ฐ)
|
47 |
-
pass
|
48 |
-
|
49 |
-
return True
|
50 |
-
|
51 |
-
if 'disable_opt2' not in st.session_state:
|
52 |
-
st.session_state.disable_opt2 = True
|
53 |
-
if 'disable_opt1' not in st.session_state:
|
54 |
-
st.session_state.disable_opt1 = False
|
55 |
-
if 'disable_download' not in st.session_state:
|
56 |
-
st.session_state.disable_download = True
|
57 |
-
if 'disable_proceed' not in st.session_state:
|
58 |
-
st.session_state.disable_proceed = False
|
59 |
-
|
60 |
-
st.markdown(hide_streamlit_style, unsafe_allow_html=True)
|
61 |
-
|
62 |
-
col1, _, col2 = st.columns([6, 1, 3], gap="small")
|
63 |
-
|
64 |
-
def toggle_state_opt1():
|
65 |
-
if st.session_state.get("opt1") == True:
|
66 |
-
st.session_state.opt2 = False
|
67 |
-
st.session_state.disable_opt2 = True
|
68 |
-
else:
|
69 |
-
st.session_state.opt2 = True
|
70 |
-
st.session_state.disable_opt2 = False
|
71 |
-
|
72 |
-
def toggle_state_opt2():
|
73 |
-
if st.session_state.get("opt2") == True:
|
74 |
-
st.session_state.opt1 = False
|
75 |
-
st.session_state.disable_opt1 = True
|
76 |
-
else:
|
77 |
-
st.session_state.opt1 = True
|
78 |
-
st.session_state.disable_opt1 = False
|
79 |
-
|
80 |
-
toggle_state_opt2()
|
81 |
-
toggle_state_opt1()
|
82 |
-
options = ["2", "3", "4"]
|
83 |
-
|
84 |
-
with col1:
|
85 |
-
file = st.file_uploader(" ", type=['png', 'jpeg', 'jpg', 'pgm', 'jpe', 'mp4', 'mov'])
|
86 |
-
if file is not None:
|
87 |
-
bytes_data = file.getvalue()
|
88 |
-
file_size = len(bytes_data)
|
89 |
-
print("File size: ", file_size)
|
90 |
-
|
91 |
-
if file.type.split('/')[0] == "image" and file_size > 1550000:
|
92 |
-
st.session_state.disable_proceed = True
|
93 |
-
with st.sidebar:
|
94 |
-
st.info('Sorry, maximum size of image is 1.5MB', icon="โน๏ธ")
|
95 |
-
elif file.type.split('/')[0] == "image":
|
96 |
-
image = Image.open(file)
|
97 |
-
st.session_state.disable_proceed = False
|
98 |
-
st.image(image, caption="Upload Image", use_column_width=True)
|
99 |
-
st.session_state.disable_proceed = False
|
100 |
-
elif file.type.split('/')[0] == 'video' and file_size > 200000000:
|
101 |
-
with st.sidebar:
|
102 |
-
options = ["2", "3"]
|
103 |
-
st.info('Sorry, maximum size of video is 200MB', icon="โน๏ธ")
|
104 |
-
st.session_state.disable_proceed = True
|
105 |
-
elif file.type.split('/')[0] == 'video':
|
106 |
-
video = st.video(file)
|
107 |
-
print(type(video))
|
108 |
-
options = ["2", "3"]
|
109 |
-
st.session_state.disable_proceed = False
|
110 |
-
with st.sidebar:
|
111 |
-
st.info('For custom size, currently I can process video without AI.', icon="โน๏ธ")
|
112 |
-
|
113 |
-
with col2:
|
114 |
-
st.markdown("\n")
|
115 |
-
st.markdown("\n")
|
116 |
-
st.markdown("\n")
|
117 |
-
|
118 |
-
st.subheader(" UPSCALE RESOLUTION UP TO")
|
119 |
-
st.markdown("\n")
|
120 |
-
st.markdown("\n")
|
121 |
-
|
122 |
-
opt1 = st.checkbox("MULTIPLES OF", key="opt1", value=True, on_change=toggle_state_opt1)
|
123 |
-
st.selectbox("SELECT", options, key="opt1_selBox", disabled=st.session_state.disable_opt1)
|
124 |
-
|
125 |
-
st.markdown("\n")
|
126 |
-
st.markdown("\n")
|
127 |
-
opt2 = st.checkbox("CUSTOM SIZE", key="opt2", on_change=toggle_state_opt2)
|
128 |
-
|
129 |
-
st.number_input("Width", step=1, min_value=150, max_value=3840, value=900, key="width", disabled=st.session_state.disable_opt2)
|
130 |
-
st.number_input("Height", step=1, min_value=150, max_value=2160, value=900, key="height", disabled=st.session_state.disable_opt2)
|
131 |
-
|
132 |
-
st.markdown("\n")
|
133 |
-
st.markdown("\n")
|
134 |
-
|
135 |
-
if st.button(15*" "+"PROCEED"+" "*15, disabled=st.session_state.disable_proceed) and file is not None:
|
136 |
-
if st.session_state.get('opt1') == True:
|
137 |
-
task = st.session_state.opt1_selBox
|
138 |
-
else:
|
139 |
-
task = [st.session_state.width, st.session_state.height]
|
140 |
-
print(task)
|
141 |
-
result_file_path = upscale(file, task)
|
142 |
-
|
143 |
-
if result_file_path:
|
144 |
-
st.session_state.disable_download = False
|
145 |
-
st.session_state.result_file_path = result_file_path
|
146 |
-
|
147 |
-
st.markdown("\n")
|
148 |
-
st.markdown("\n")
|
149 |
-
|
150 |
-
if file is None:
|
151 |
-
st.session_state.disable_download = True
|
152 |
|
153 |
-
|
154 |
-
|
155 |
-
|
156 |
-
|
157 |
-
|
|
|
|
|
|
|
158 |
|
159 |
-
|
160 |
-
|
|
|
1 |
+
import gradio as gr
|
|
|
|
|
2 |
import cv2
|
3 |
+
import numpy as np
|
4 |
+
|
5 |
+
# ์ด๋ฏธ์ง ์
์ค์ผ์ผ ํจ์
|
6 |
+
def upscale_image(input_image, radio_input):
|
7 |
+
upscale_factor = int(radio_input) # ์
์ค์ผ์ผ ๋น์จ์ ์ ์ํ์ผ๋ก ๋ณํ
|
8 |
+
# ์
๋ ฅ ์ด๋ฏธ์ง๋ฅผ ์ฃผ์ด์ง ์
์ค์ผ์ผ ๋น์จ๋ก ํฌ๊ธฐ ๋ณ๊ฒฝ (๋ณด๊ฐ๋ฒ: INTER_CUBIC)
|
9 |
+
output_image = cv2.resize(input_image, None, fx=upscale_factor, fy=upscale_factor, interpolation=cv2.INTER_CUBIC)
|
10 |
+
return output_image
|
11 |
+
|
12 |
+
# ์ธํฐํ์ด์ค ์ค๋ช
|
13 |
+
DESCRIPTION = """
|
14 |
+
์ด๋ฏธ์ง์ ํฌ๊ธฐ์ ํ์ง์ ํฅ์์์ผ ๋ณด์ธ์! (you can increase the size and quality of your images)
|
15 |
"""
|
16 |
|
17 |
+
# ์
์ค์ผ์ผ ๋ ๋ฒจ ์ ํ ๋ผ๋์ค ๋ฒํผ
|
18 |
+
radio_input = gr.Radio(label="์
์ค์ผ์ผ ๋ ๋ฒจ์ ์ ํ ํ์ธ์. (Select Upscaling level)", choices=[2, 4, 6, 8], value=2)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
19 |
|
20 |
+
# Gradio ์ธํฐํ์ด์ค ์ ์
|
21 |
+
demo = gr.Interface(
|
22 |
+
fn=upscale_image,
|
23 |
+
inputs=[gr.Image(label="์
๋ ฅ ์ด๋ฏธ์ง (Input Image)", type="numpy"), radio_input],
|
24 |
+
outputs=gr.Image(label="์
์ค์ผ์ผ๋ ์ด๋ฏธ์ง(Upscaled Image)"),
|
25 |
+
title="Image Upscaler",
|
26 |
+
description=DESCRIPTION
|
27 |
+
)
|
28 |
|
29 |
+
# ์ธํฐํ์ด์ค ์คํ
|
30 |
+
demo.launch(show_api=False)
|