-
Notifications
You must be signed in to change notification settings - Fork 4
/
waifuprocess.cpp
120 lines (102 loc) · 2.91 KB
/
waifuprocess.cpp
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
#include <QDir>
#include <QFileInfo>
#include "waifuprocess.h"
WaifuProcess::WaifuProcess(QObject *parent)
: QObject{parent}
{
}
WaifuProcess::~WaifuProcess()
{
if (waifu) {
delete waifu;
waifu = nullptr;
}
}
void WaifuProcess::setIgnoreExisting(bool ignore)
{
ignoreExisting = ignore;
}
bool WaifuProcess::start(QString inputFile, double scale, int noise, QString executable, QString modelFolder, bool forceOpenCL, int processor)
{
if (waifu)
return false;
emit preparing();
QStringList args;
double scaleRatio = scale;
int noiseLevel = noise;
if (forceOpenCL)
args << "--force-OpenCL";
args << "--scale-ratio" << QString::number(scaleRatio, 'f', 3);
if (noiseLevel > 0)
args << "--noise-level" << QString::number(noiseLevel);
args << "-m";
if (noiseLevel > 0) {
if (scaleRatio > 1.0)
args << "noise-scale";
else
args << "noise";
} else {
args << "scale";
}
args << "-i" << inputFile;
QString outputFile;
if (true) {
QFileInfo qfi(inputFile);
QString suffix = "_waifu2x";
if (noiseLevel > 0)
suffix.append(QString("_noise%1").arg(noiseLevel));
if (scaleRatio > 1.0)
suffix.append(QString("_scale%2").arg(QString::number(scaleRatio, 'f', 3)));
suffix.append(".png");
QString trimmedFileName = qfi.completeBaseName().left(255-suffix.length());
outputFile = qfi.dir().absolutePath() + "/" + trimmedFileName + suffix;
}
if (ignoreExisting) {
QFileInfo outinfo(outputFile);
if (outinfo.exists()) {
emit finished();
return false;
}
}
args << "-o" << outputFile;
args << "--model-dir" << modelFolder;
if (processor > 0)
args << "--processor" << QString::number(processor-1);
emit log("Program arguments:\n\t");
emit log(args.join(" "));
waifu = new QProcess(this);
waifu->setProgram(executable);
waifu->setArguments(args);
connect(waifu, &QProcess::readyRead,
this, &WaifuProcess::waifu_readyRead);
connect(waifu, SIGNAL(finished(int,QProcess::ExitStatus)),
this, SLOT(waifu_finished(int,QProcess::ExitStatus)));
emit log("\n\nLaunching waifu2x\n\n");
waifu->start();
emit started();
return true;
}
void WaifuProcess::stop()
{
waifu->deleteLater();
waifu = nullptr;
}
bool WaifuProcess::isRunning()
{
return waifu != nullptr;
}
void WaifuProcess::waifu_readyRead()
{
emit log(QString::fromUtf8(waifu->readAll()));
}
void WaifuProcess::waifu_finished(int exitCode, QProcess::ExitStatus status)
{
if (!waifu)
return;
emit log(QString::fromUtf8(waifu->readAllStandardError()));
emit log(QString("\nerror code: %1\n"
"status: %2").arg(exitCode).arg(status));
waifu->deleteLater();
waifu = NULL;
emit finished();
}