jpjp9292 commited on
Commit
29ab20e
ยท
verified ยท
1 Parent(s): 48e3649

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +153 -28
app.py CHANGED
@@ -1,35 +1,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
- # ์—…์Šค์ผ€์ผ๋œ ์ด๋ฏธ์ง€๋ฅผ PNG ํŒŒ์ผ๋กœ ์ €์žฅ
11
- output_file_path = "/mnt/data/upscaled_image.png"
12
- cv2.imwrite(output_file_path, output_image)
13
- return output_file_path
14
-
15
- # ์ธํ„ฐํŽ˜์ด์Šค ์„ค๋ช…
16
- DESCRIPTION = """
17
- ์ด๋ฏธ์ง€์˜ ํฌ๊ธฐ์™€ ํ’ˆ์งˆ์„ ํ–ฅ์ƒ์‹œ์ผœ ๋ณด์„ธ์š”! (you can increase the size and quality of your images)
18
  """
19
 
20
- # ์—…์Šค์ผ€์ผ ๋ ˆ๋ฒจ ์„ ํƒ ๋ผ๋””์˜ค ๋ฒ„ํŠผ
21
- radio_input = gr.Radio(label="์—…์Šค์ผ€์ผ ๋ ˆ๋ฒจ ์„ ํƒ (Select Upscaling level)", choices=[2, 4, 6, 8, 10], value=2)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
22
 
23
- # Gradio ์ธํ„ฐํŽ˜์ด์Šค ์ •์˜
24
- demo = gr.Interface(
25
- fn=upscale_image,
26
- inputs=[gr.Image(label="์ž…๋ ฅ ์ด๋ฏธ์ง€ (Input Image)", type="numpy"), radio_input],
27
- outputs=gr.File(label="PNG ํŒŒ์ผ ๋‹ค์šด๋กœ๋“œ (Download PNG File)"),
28
- title="Image Upscaler",
29
- description=DESCRIPTION
30
- )
31
 
32
- # ์ธํ„ฐํŽ˜์ด์Šค ์‹คํ–‰
33
- demo.launch(show_api=False)
34
 
 
 
 
 
 
35
 
 
 
 
1
+ import math
2
+ import streamlit as st
3
+ from PIL import Image
4
  import cv2
5
+ import os
6
+ import threading
7
+
8
+ hide_streamlit_style = """
9
+ <style>
10
+ #root > div:nth-child(1) > div > div > div > div > section > div {padding-top: 0rem;
11
+ padding-left: 1%;
12
+ }
13
+ </style>
 
 
 
 
 
 
14
  """
15
 
16
+ st.set_page_config(layout="wide")
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*"&nbsp;"+"PROCEED"+"&nbsp;"*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
+ if st.session_state.disable_download:
154
+ st.button(13*"&nbsp;"+"DOWNLOAD"+"&nbsp;"*13, disabled=True)
155
+ else:
156
+ with open(st.session_state.result_file_path, "rb") as download_file:
157
+ st.download_button(label=13*"&nbsp;"+"DOWNLOAD"+"&nbsp;"*13, data=download_file, file_name='processed_' + file.name.split('.')[0] + ".png", mime="image/png")
158
 
159
+ st.markdown("\n")
160
+ st.markdown("\n")