-
Notifications
You must be signed in to change notification settings - Fork 0
/
RealESRGAN.py
277 lines (238 loc) · 9.52 KB
/
RealESRGAN.py
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
import gettext
import subprocess
import sys
import tkinter as tk
from pathlib import Path
from threading import Thread
from tkinter.filedialog import askdirectory, askopenfilename, asksaveasfilename
from tkinter.messagebox import askyesno
from UiRealESRGAN import UiRealESRGAN
REALESRGAN_EXEC = Path('realesrgan/realesrgan-ncnn-vulkan')
if 'win' in sys.platform:
REALESRGAN_EXEC = REALESRGAN_EXEC.with_suffix('.exe')
gettext.install(domain='RealESRGAN_GUI', localedir='locale')
FILE_TYPES = (
(_('Image File'), '*.png;*.jpg;*.jpeg;*.webp'),
(_('PNG Image'), '*.png'),
(_('JPEG Image'), '*.jpg;*.jpeg'),
(_('Webp Image'), '*.webp')
)
MODEL = (
"realesrgan-x4plus",
"realesrgan-x4plus-anime",
"realesr-animevideov3-x2",
"realesr-animevideov3-x3",
"realesr-animevideov3-x4"
)
class RealESRGAN(UiRealESRGAN):
def __init__(self, master=None, **kw):
super(RealESRGAN, self).__init__(master=master, **kw)
# set ui configure
self._tta_mode = 0
self.tta_mode.set(self._tta_mode)
self.ComboboxScale.configure(values='2 3 4')
self._scale = 4
self.scale.set(self._scale)
self.ComboboxScale.configure(state='disable')
self.ComboboxFormat.configure(values='png jpg webp')
self._format = 'png'
self.format.set(self._format)
self.ComboboxFormat.configure(state='disabled')
self.ComboboxModel.configure(values=MODEL)
self._model = MODEL[0]
self.model.set(self._model)
self.ComboboxModel.configure(state='disabled')
self.CheckButtonTTAMode.configure(state='disabled')
self.ButtonStart.configure(state='disabled')
self.ButtonStop.configure(state='disable')
self.ButtonOutputFile.configure(state='disabled')
self.ButtonOutputFolder.configure(state='disabled')
self._input_path: None | str | Path = None
self._output_path: None | str | Path = None
self._input_type: None | str | Path = None
self._output_custom = False
self._is_folder = False
self._process = None
if REALESRGAN_EXEC.exists():
self._realesrgan_exec = REALESRGAN_EXEC
else:
self._realesrgan_exec = None
self._check_exec()
def get_input_file(self):
input_file = askopenfilename(title=_('Select Image File'), filetypes=FILE_TYPES)
if input_file:
self._output_custom = False
self._input_path = Path(input_file)
self.input_path.set(self._input_path)
self._input_type = 'file'
self._set_output()
self.ButtonOutputFile.configure(state='normal')
self.ButtonOutputFolder.configure(state='disabled')
self._toggle_start()
def get_input_folder(self):
input_folder = askdirectory(title=_('Select Images Folder'), mustexist=True)
if input_folder:
self._output_custom = False
self._is_folder = True
self._input_path = Path(input_folder)
self.input_path.set(self._input_path)
self._input_type = 'dir'
self._set_output()
self.ButtonOutputFile.configure(state='disabled')
self.ButtonOutputFolder.configure(state='normal')
self._toggle_start()
def set_output_file(self):
output_file = asksaveasfilename(
title=_('Set Output File'),
confirmoverwrite=True,
defaultextension=f'.{self._format}',
filetypes=FILE_TYPES,
initialdir=self._input_path.parent if self._input_path else None,
initialfile=self._output_path.stem if self._input_path else None
)
if output_file:
self._output_custom = True
self._output_path = Path(output_file)
self._format = self._output_path.suffix[1:]
self.output_path.set(self._output_path)
self.format.set(self._format)
self._toggle_start()
def set_output_folder(self):
output_folder = askdirectory(title=_('Set Output Folder'))
if output_folder:
self._output_custom = True
self._output_path = Path(output_folder)
self.output_path.set(self._output_path)
self._toggle_start()
def set_scale(self, event=None):
self._scale = self.scale.get()
self._set_output()
def set_format(self, event=None):
self._format = self.format.get()
self._set_output()
def set_model(self, event=None):
self._model = self.model.get()
self._set_output()
def set_tta_mode(self):
self._tta_mode = self.tta_mode.get()
self._set_output()
def start(self):
if not self._realesrgan_exec:
return None
if self._is_folder:
output_folder = Path(self.output_path.get())
if not output_folder.exists():
if askyesno(
title=_("Create Folder"),
message=_("Output folder dose not exists. Create it?"),
):
output_folder.mkdir()
else:
return None
self.ButtonStart.configure(state= tk.DISABLED)
self.ButtonStop.configure(state=tk.NORMAL)
self._enable_widgets(False)
# Launch the process in a separate thread
process_thread = Thread(target=self._run_process)
process_thread.start()
def stop(self):
self._process.kill()
self._process.wait()
self.TextMessage.insert(tk.END, _('====== realesrgan-ncnn-vulkan process terminated. ======\n'))
self.ButtonStart.configure(state=tk.NORMAL)
self.ButtonStop.configure(state=tk.DISABLED)
self._enable_widgets(True)
def _set_output(self):
if self._output_custom:
return None
output_name = f'{self._input_path.stem}-{self._scale}x-{self._model}'
print(self._tta_mode)
if self._tta_mode:
output_name = f'{output_name}-tta'
if self._input_type == 'file':
output_name = f'{output_name}.{self._format}'
self._output_path = self._input_path.parent / output_name
self.output_path.set(self._output_path)
def _run_process(self):
command = [
str(REALESRGAN_EXEC),
"-i", self.input_path.get(),
"-o", self.output_path.get(),
"-s", self.scale.get(),
"-n", self.model.get(),
"-f", self.format.get(),
"-v"
]
if self.tta_mode.get():
command.append("-x")
self.TextMessage.insert(tk.END, " ".join(command) + "\n")
startupinfo = subprocess.STARTUPINFO()
startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW
self._process = subprocess.Popen(
command,
startupinfo=startupinfo,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
text=True,
bufsize=1
)
# Read and display real-time output in the GUI
with self._process.stdout:
for line in iter(self._process.stdout.readline, ''):
self.TextMessage.insert(tk.END, line)
self.TextMessage.yview(tk.END)
#self.master.update()
# Enable the button after the process completes
self.ButtonStart.configure(state=tk.NORMAL)
self.ButtonStop.configure(state=tk.DISABLED)
self._enable_widgets(True)
def _enable_widgets(self, enable=True):
buttons = (
self.ButtonStart,
self.ButtonInputFile,
self.ButtonInputFolder,
self.ButtonOutputFile,
self.ButtonOutputFolder,
self.CheckButtonTTAMode,
self.EntryInputPath,
self.EntryOutputPath,
)
combobox = (
self.ComboboxScale,
self.ComboboxFormat,
self.ComboboxModel,
)
if enable:
for w in buttons:
w.configure(state='normal')
for w in combobox:
w.configure(state='readonly')
else:
for widgets in (buttons, combobox):
for w in widgets:
w.configure(state='disabled')
def _toggle_start(self):
if self._input_path and self._input_path.exists() and self._output_path:
self.ButtonStart.configure(state='normal')
self.ComboboxScale.configure(state='readonly')
self.ComboboxFormat.configure(state='readonly')
self.ComboboxModel.configure(state='readonly')
self.CheckButtonTTAMode.configure(state='normal')
else:
self.ButtonStart.configure(state='disabled')
self.ComboboxScale.configure(state='disabled')
self.ComboboxFormat.configure(state='disabled')
self.ComboboxModel.configure(state='disabled')
self.CheckButtonTTAMode.configure(state='disabled')
def _check_exec(self):
if not self._realesrgan_exec:
self.TextMessage.configure(state='normal')
self.TextMessage.insert(
'end',
_(
'realesrgan-ncnn-vulkan executable file not found! \n'
'Please download from https://github.com/xinntao/Real-ESRGAN '
'and extract to realesrgan folder.\n'
)
)
self.TextMessage.configure(state='disabled')