-
Notifications
You must be signed in to change notification settings - Fork 0
/
engine.py
81 lines (64 loc) · 3.18 KB
/
engine.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
import os
import sys
from PySide6.QtCore import QObject, Signal, QProcess, QUrl, QWaitCondition, QMutex
from PySide6.QtGui import QDesktopServices
def resource_path(*relative_path):
if getattr(sys, 'frozen', False):
base_path = sys._MEIPASS
else:
base_path = os.path.abspath(".")
return os.path.join(base_path, *relative_path)
srcnn_path = resource_path('waifu2x','waifu2x-ncnn-vulkan.exe')
srcnn_model_path = resource_path('waifu2x')
realesrgan_path = resource_path('realesrgan','realesrgan-ncnn-vulkan.exe')
realesrgan_model_path = resource_path('realesrgan','models')
class Client_Engine(QObject):
status = Signal(str)
def __init__(self):
super().__init__()
self.process = QProcess()
self.process.readyReadStandardError.connect(self.do_read)
self.process.finished.connect(self.do_finish)
self.process.errorOccurred.connect(self.do_error)
self.param = {'total':0}
def do_generate(self, param):
self.param.update(param)
# print(param['input'])
# print(param['output'])
if len(self.param['input'])<1:
if self.param['open'] and self.param['output']:
QDesktopServices.openUrl(QUrl(f"file:///{param['output']}",QUrl().TolerantMode))
return
input_path = self.param['input'][0]
self.param['input'] = self.param['input'][1:]
basename = os.path.basename(input_path)
form = basename.split('.')[-1]
output_path = basename[:-len(form)-1]+'_waifu2x.'+form
output_path = os.path.join(param['output'], output_path)
if param['model'] == 'SRCNN':
engine = srcnn_path
args = [f" -m \"{os.path.join(srcnn_model_path,param['SRCNN'])}\"" ,
f" -i \"{input_path}\" ",
f" -o \"{output_path}\" ",
f" -s {2**param['scale']}",
f" -n {param['noise']}"]
else:
engine = realesrgan_path
args = [f" -m \"{realesrgan_model_path}\" ",
f" -n {param['RealESRGAN']} " ,
f" -i \"{input_path}\" ",
f" -o \"{output_path}\" ",
f" -s 4"]
self.status.emit(f"[INFO {self.param['total']-len(self.param['input'])}/{self.param['total']}] "+f"\"{engine}\""+' '.join(args))
self.process.startCommand(f"\"{engine}\""+' '.join(args))
def do_finish(self):
self.status.emit(f"[INFO {self.param['total']-len(self.param['input'])}/{self.param['total']}] Finished with {len(self.param['input'])} left.")
self.do_generate(self.param)
def do_error(self):
self.status.emit("[ERROR] Process failed:"+ self.process.errorString())
def do_read(self):
self.status.emit(f"[INFO {self.param['total']-len(self.param['input'])}/{self.param['total']}] "+self.process.readAllStandardError().data().decode().strip())
def do_cancel(self):
self.status.emit(f"[INFO {self.param['total']-len(self.param['input'])}/{self.param['total']}] Trying kill process.")
self.process.kill()
self.process.terminate()