1
0
mirror of https://github.com/upscayl/upscayl.git synced 2025-01-18 17:14:08 +01:00

Refactored full code

This commit is contained in:
Nayam Amarshe 2023-03-18 17:28:38 +05:30
parent 34ff24a06b
commit 8eacbef0a6
10 changed files with 3228 additions and 4671 deletions

View File

@ -24,6 +24,7 @@ import isDev from "electron-is-dev";
import prepareNext from "electron-next";
import commands from "./commands";
import { spawnUpscayl } from "./upscayl";
import { getCommandArguments } from "./utils/getArguments";
// Prepare the renderer once the app is ready
let mainWindow;
@ -133,54 +134,28 @@ ipcMain.on(commands.DOUBLE_UPSCAYL, async (event, payload) => {
const fileName = parse(fullfileName).name;
const fileExt = parse(fullfileName).ext;
const outFile =
outputDir + "/" + fileName + "_upscayl_8x_" + model + "." + saveImageAs;
outputDir + "/" + fileName + "_upscayl_16x_" + model + "." + saveImageAs;
// UPSCALE
let upscayl = spawn(
execPath("realesrgan"),
[
"-i",
inputDir + "/" + fullfileName,
"-o",
outFile,
"-s",
4,
"-m",
let upscayl = spawnUpscayl(
"realesrgan",
getCommandArguments(
"doubleUpscayl",
inputDir,
fullfileName,
outputDir,
modelsPath,
"-n",
model,
gpuId ? `-g ${gpuId}` : "",
"-f",
saveImageAs,
],
{
cwd: undefined,
detached: false,
}
);
console.log(
"🆙 COMMAND:",
"-i",
inputDir + "/" + fullfileName,
"-o",
outFile,
"-s",
4,
"-m",
modelsPath,
"-n",
model,
gpuId ? `-g ${gpuId}` : "",
"-f",
saveImageAs
gpuId,
saveImageAs
)
);
let failed = false;
let isAlpha = false;
let failed2 = false;
// TAKE UPSCAYL OUTPUT
upscayl.stderr.on("data", (data) => {
const onData = (data) => {
// CONVERT DATA TO STRING
data = data.toString();
// PRINT TO CONSOLE
@ -194,80 +169,68 @@ ipcMain.on(commands.DOUBLE_UPSCAYL, async (event, payload) => {
if (data.includes("has alpha channel")) {
isAlpha = true;
}
});
// IF ERROR
upscayl.on("error", (data) => {
};
const onError = (data) => {
data.toString();
// SEND UPSCAYL PROGRESS TO RENDERER
mainWindow.webContents.send(commands.DOUBLE_UPSCAYL_PROGRESS, data);
// SET FAILED TO TRUE
failed = true;
return;
});
};
const onData2 = (data) => {
// CONVERT DATA TO STRING
data = data.toString();
// PRINT TO CONSOLE
console.log(data);
// SEND UPSCAYL PROGRESS TO RENDERER
mainWindow.webContents.send(commands.DOUBLE_UPSCAYL_PROGRESS, data);
// IF PROGRESS HAS ERROR, UPSCAYL FAILED
if (data.includes("invalid gpu") || data.includes("failed")) {
failed2 = true;
}
};
const onError2 = (data) => {
data.toString();
// SEND UPSCAYL PROGRESS TO RENDERER
mainWindow.webContents.send(commands.DOUBLE_UPSCAYL_PROGRESS, data);
// SET FAILED TO TRUE
failed2 = true;
return;
};
const onClose2 = (code) => {
if (!failed2) {
console.log("Done upscaling");
mainWindow.webContents.send(
commands.DOUBLE_UPSCAYL_DONE,
isAlpha ? outFile + ".png" : outFile
);
}
};
// ON UPSCAYL DONE
upscayl.on("close", (code) => {
upscayl.process.stderr.on("data", onData);
upscayl.process.on("error", onError);
upscayl.process.on("close", (code) => {
// IF NOT FAILED
if (!failed) {
// UPSCALE
let upscayl2 = spawn(
execPath("realesrgan"),
[
"-i",
isAlpha ? outFile + ".png" : outFile,
"-o",
isAlpha ? outFile + ".png" : outFile,
"-s",
4,
"-m",
let upscayl2 = spawnUpscayl(
"realesrgan",
getCommandArguments(
"doubleUpscaylSecondPass",
null,
null,
outFile,
modelsPath,
"-n",
model,
gpuId ? `-g ${gpuId}` : "",
"-f",
isAlpha ? "" : saveImageAs,
],
{
cwd: undefined,
detached: false,
}
gpuId,
isAlpha
)
);
let failed2 = false;
// TAKE UPSCAYL OUTPUT
upscayl2.stderr.on("data", (data) => {
// CONVERT DATA TO STRING
data = data.toString();
// PRINT TO CONSOLE
console.log(data);
// SEND UPSCAYL PROGRESS TO RENDERER
mainWindow.webContents.send(commands.DOUBLE_UPSCAYL_PROGRESS, data);
// IF PROGRESS HAS ERROR, UPSCAYL FAILED
if (data.includes("invalid gpu") || data.includes("failed")) {
failed2 = true;
}
});
// IF ERROR
upscayl2.on("error", (data) => {
data.toString();
// SEND UPSCAYL PROGRESS TO RENDERER
mainWindow.webContents.send(commands.DOUBLE_UPSCAYL_PROGRESS, data);
// SET FAILED TO TRUE
failed2 = true;
return;
});
upscayl2.on("close", (code) => {
if (!failed2) {
console.log("Done upscaling");
mainWindow.webContents.send(
commands.DOUBLE_UPSCAYL_DONE,
isAlpha ? outFile + ".png" : outFile
);
}
});
upscayl2.process.stderr.on("data", onData2);
upscayl2.process.on("error", onError2);
upscayl2.process.on("close", onClose2);
}
});
});
@ -317,43 +280,38 @@ ipcMain.on(commands.UPSCAYL, async (event, payload) => {
} else {
let upscayl: ReturnType<typeof spawnUpscayl>;
const defaultArguments = [
"-i",
inputDir + "/" + fullfileName,
"-o",
outFile,
"-s",
scale === 2 ? 4 : scale,
"-m",
modelsPath,
"-n",
model,
gpuId ? `-g ${gpuId}` : "",
"-f",
saveImageAs,
];
const sharpenArguments = [
"-i",
inputDir + "/" + fullfileName,
"-o",
outFile,
"-s",
scale,
"-x",
"-m",
modelsPath + "/" + model,
gpuId ? `-g ${gpuId}` : "",
"-f",
saveImageAs,
];
switch (model) {
default:
upscayl = spawnUpscayl(defaultArguments, "realesrgan");
upscayl = spawnUpscayl(
"realesrgan",
getCommandArguments(
"singleImage",
inputDir,
fullfileName,
outFile,
modelsPath,
model,
scale,
gpuId,
false
)
);
break;
case "models-DF2K":
upscayl = spawnUpscayl(sharpenArguments, "realsr");
upscayl = spawnUpscayl(
"realsr",
getCommandArguments(
"singleImage",
inputDir,
fullfileName,
outFile,
modelsPath,
model,
scale,
gpuId,
false
)
);
break;
}
@ -375,13 +333,11 @@ ipcMain.on(commands.UPSCAYL, async (event, payload) => {
isAlpha = true;
}
};
const onError = (data) => {
mainWindow.webContents.send(commands.UPSCAYL_PROGRESS, data.toString());
failed = true;
return;
};
const onClose = () => {
if (failed !== true) {
console.log("Done upscaling");
@ -419,90 +375,44 @@ ipcMain.on(commands.FOLDER_UPSCAYL, async (event, payload) => {
fs.mkdirSync(outputDir, { recursive: true });
}
// UPSCALE
let upscayl: ChildProcessWithoutNullStreams | null = null;
let upscayl: ReturnType<typeof spawnUpscayl>;
switch (model) {
default:
upscayl = spawn(
execPath("realesrgan"),
[
"-i",
upscayl = spawnUpscayl(
"realesrgan",
getCommandArguments(
"batch",
inputDir,
"-o",
"",
outputDir,
"-s",
4,
"-m",
modelsPath,
"-n",
model,
gpuId ? `-g ${gpuId}` : "",
"-f",
saveImageAs,
],
{
cwd: undefined,
detached: false,
}
);
console.log(
"🆙 COMMAND:",
"-i",
inputDir,
"-o",
outputDir,
"-s",
4,
"-m",
modelsPath,
"-n",
model,
gpuId ? `-g ${gpuId}` : "",
"-f",
saveImageAs
4,
gpuId,
saveImageAs
)
);
break;
case "models-DF2K":
upscayl = spawn(
execPath("realsr"),
[
"-i",
upscayl = spawnUpscayl(
"realsr",
getCommandArguments(
"batch",
inputDir,
"-o",
"",
outputDir,
"-s",
modelsPath,
model,
4,
"-x",
"-m",
modelsPath + "/" + model,
gpuId ? `-g ${gpuId}` : "",
"-f",
saveImageAs,
],
{
cwd: undefined,
detached: false,
}
);
console.log(
"🆙 COMMAND:",
"-i",
inputDir,
"-o",
outputDir,
"-s",
4,
"-x",
"-m",
modelsPath + "/" + model,
gpuId ? `-g ${gpuId}` : "",
"-f",
saveImageAs
gpuId,
saveImageAs
)
);
break;
}
let failed = false;
upscayl?.stderr.on("data", (data) => {
const onData = (data) => {
console.log(
"🚀 => upscayl.stderr.on => stderr.toString()",
data.toString()
@ -515,24 +425,25 @@ ipcMain.on(commands.FOLDER_UPSCAYL, async (event, payload) => {
if (data.includes("invalid gpu") || data.includes("failed")) {
failed = true;
}
});
upscayl?.on("error", (data) => {
};
const onError = (data) => {
mainWindow.webContents.send(
commands.FOLDER_UPSCAYL_PROGRESS,
data.toString()
);
failed = true;
return;
});
// Send done comamnd when
upscayl?.on("close", (code) => {
};
const onClose = (code) => {
if (failed !== true) {
console.log("Done upscaling");
mainWindow.webContents.send(commands.FOLDER_UPSCAYL_DONE, outputDir);
}
});
};
upscayl.process.stderr.on("data", onData);
upscayl.process.on("error", onError);
upscayl.process.on("close", onClose);
});
//------------------------Video Upscayl-----------------------------//

View File

@ -1,7 +1,7 @@
import { spawn } from "child_process";
import { execPath } from "./binaries";
export const spawnUpscayl = (command: string[], binaryName: string) => {
export const spawnUpscayl = (binaryName: string, command: string[]) => {
console.log(" Command: ", command);
const spawnedProcess = spawn(execPath(binaryName), command, {

View File

@ -0,0 +1,99 @@
export const getCommandArguments = (
type:
| "singleImage"
| "singleImageSharpen"
| "doubleUpscayl"
| "doubleUpscaylSecondPass"
| "batch",
inputDir?: any,
fullfileName?: any,
outFile?: any,
modelsPath?: any,
model?: any,
scale?: any,
gpuId?: any,
saveImageAs?: any,
isAlpha?: any
) => {
switch (type) {
case "singleImage":
return [
"-i",
inputDir + "/" + fullfileName,
"-o",
outFile,
"-s",
scale === 2 ? 4 : scale,
"-m",
modelsPath,
"-n",
model,
gpuId ? `-g ${gpuId}` : "",
"-f",
saveImageAs,
];
case "singleImageSharpen":
return [
"-i",
inputDir + "/" + fullfileName,
"-o",
outFile,
"-s",
scale,
"-x",
"-m",
modelsPath + "/" + model,
gpuId ? `-g ${gpuId}` : "",
"-f",
saveImageAs,
];
case "doubleUpscayl":
return [
"-i",
inputDir + "/" + fullfileName,
"-o",
outFile,
"-s",
4,
"-m",
modelsPath,
"-n",
model,
gpuId ? `-g ${gpuId}` : "",
"-f",
saveImageAs,
];
case "doubleUpscaylSecondPass":
return [
"-i",
isAlpha ? outFile + ".png" : outFile,
"-o",
isAlpha ? outFile + ".png" : outFile,
"-s",
4,
"-m",
modelsPath,
"-n",
model,
gpuId ? `-g ${gpuId}` : "",
"-f",
isAlpha ? "" : saveImageAs,
];
case "batch":
return [
"-i",
inputDir,
"-o",
outFile,
"-s",
4,
"-m",
modelsPath,
"-n",
model,
gpuId ? `-g ${gpuId}` : "",
"-f",
saveImageAs,
];
}
};

View File

@ -123,8 +123,7 @@ electron_1.ipcMain.on(commands_1.default.DOUBLE_UPSCAYL, (event, payload) => __a
const fileName = (0, path_1.parse)(fullfileName).name;
const fileExt = (0, path_1.parse)(fullfileName).ext;
const outFile = outputDir + "/" + fileName + "_upscayl_8x_" + model + "." + saveImageAs;
// UPSCALE
let upscayl = (0, child_process_1.spawn)((0, binaries_1.execPath)("realesrgan"), [
const commandArguments = [
"-i",
inputDir + "/" + fullfileName,
"-o",
@ -138,11 +137,9 @@ electron_1.ipcMain.on(commands_1.default.DOUBLE_UPSCAYL, (event, payload) => __a
gpuId ? `-g ${gpuId}` : "",
"-f",
saveImageAs,
], {
cwd: undefined,
detached: false,
});
console.log("🆙 COMMAND:", "-i", inputDir + "/" + fullfileName, "-o", outFile, "-s", 4, "-m", binaries_1.modelsPath, "-n", model, gpuId ? `-g ${gpuId}` : "", "-f", saveImageAs);
];
// UPSCALE
let upscayl = (0, upscayl_1.spawnUpscayl)("realesrgan", commandArguments).process;
let failed = false;
let isAlpha = false;
// TAKE UPSCAYL OUTPUT
@ -157,6 +154,7 @@ electron_1.ipcMain.on(commands_1.default.DOUBLE_UPSCAYL, (event, payload) => __a
if (data.includes("invalid gpu") || data.includes("failed")) {
failed = true;
}
// IF IMAGE HAS ALPHA CHANNEL
if (data.includes("has alpha channel")) {
isAlpha = true;
}
@ -171,11 +169,10 @@ electron_1.ipcMain.on(commands_1.default.DOUBLE_UPSCAYL, (event, payload) => __a
return;
});
// ON UPSCAYL DONE
upscayl.on("close", (code) => {
upscayl.on("close", () => {
// IF NOT FAILED
if (!failed) {
// UPSCALE
let upscayl2 = (0, child_process_1.spawn)((0, binaries_1.execPath)("realesrgan"), [
const commandArguments2 = [
"-i",
isAlpha ? outFile + ".png" : outFile,
"-o",
@ -189,10 +186,9 @@ electron_1.ipcMain.on(commands_1.default.DOUBLE_UPSCAYL, (event, payload) => __a
gpuId ? `-g ${gpuId}` : "",
"-f",
isAlpha ? "" : saveImageAs,
], {
cwd: undefined,
detached: false,
});
];
// UPSCALE
let upscayl2 = (0, upscayl_1.spawnUpscayl)("realesrgan", commandArguments2).process;
let failed2 = false;
// TAKE UPSCAYL OUTPUT
upscayl2.stderr.on("data", (data) => {
@ -296,10 +292,10 @@ electron_1.ipcMain.on(commands_1.default.UPSCAYL, (event, payload) => __awaiter(
];
switch (model) {
default:
upscayl = (0, upscayl_1.spawnUpscayl)(defaultArguments, "realesrgan");
upscayl = (0, upscayl_1.spawnUpscayl)("realesrgan", defaultArguments);
break;
case "models-DF2K":
upscayl = (0, upscayl_1.spawnUpscayl)(sharpenArguments, "realsr");
upscayl = (0, upscayl_1.spawnUpscayl)("realsr", sharpenArguments);
break;
}
let isAlpha = false;
@ -349,49 +345,43 @@ electron_1.ipcMain.on(commands_1.default.FOLDER_UPSCAYL, (event, payload) => __a
if (!fs_1.default.existsSync(outputDir)) {
fs_1.default.mkdirSync(outputDir, { recursive: true });
}
const commandArguments = [
"-i",
inputDir,
"-o",
outputDir,
"-s",
4,
"-m",
binaries_1.modelsPath,
"-n",
model,
gpuId ? `-g ${gpuId}` : "",
"-f",
saveImageAs,
];
const sharpenArguments = [
"-i",
inputDir,
"-o",
outputDir,
"-s",
4,
"-x",
"-m",
binaries_1.modelsPath + "/" + model,
gpuId ? `-g ${gpuId}` : "",
"-f",
saveImageAs,
];
// UPSCALE
let upscayl = null;
switch (model) {
default:
upscayl = (0, child_process_1.spawn)((0, binaries_1.execPath)("realesrgan"), [
"-i",
inputDir,
"-o",
outputDir,
"-s",
4,
"-m",
binaries_1.modelsPath,
"-n",
model,
gpuId ? `-g ${gpuId}` : "",
"-f",
saveImageAs,
], {
cwd: undefined,
detached: false,
});
console.log("🆙 COMMAND:", "-i", inputDir, "-o", outputDir, "-s", 4, "-m", binaries_1.modelsPath, "-n", model, gpuId ? `-g ${gpuId}` : "", "-f", saveImageAs);
upscayl = (0, upscayl_1.spawnUpscayl)("realesrgan", commandArguments).process;
break;
case "models-DF2K":
upscayl = (0, child_process_1.spawn)((0, binaries_1.execPath)("realsr"), [
"-i",
inputDir,
"-o",
outputDir,
"-s",
4,
"-x",
"-m",
binaries_1.modelsPath + "/" + model,
gpuId ? `-g ${gpuId}` : "",
"-f",
saveImageAs,
], {
cwd: undefined,
detached: false,
});
console.log("🆙 COMMAND:", "-i", inputDir, "-o", outputDir, "-s", 4, "-x", "-m", binaries_1.modelsPath + "/" + model, gpuId ? `-g ${gpuId}` : "", "-f", saveImageAs);
upscayl = (0, upscayl_1.spawnUpscayl)("realsr", sharpenArguments).process;
break;
}
let failed = false;

3815
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -143,8 +143,8 @@
"@types/react-dom": "^18.0.8",
"autoprefixer": "^10.4.13",
"cross-env": "^7.0.3",
"electron": "^21.2.2",
"electron-builder": "^23.6.0",
"electron": "^23.1.4",
"electron-builder": "^24.0.0",
"next": "^13.0.2",
"postcss": "^8.4.18",
"prettier": "^2.7.1",

View File

@ -241,7 +241,7 @@ function LeftPaneImageSteps({
</p>
<button
className="badge-info badge cursor-help"
data-tip="Enable this option to get an 8x upscayl. Note that this may not always work properly with all images, for example, images with really large resolutions.">
data-tip="Enable this option to get a 16x upscayl (we just run upscayl twice). Note that this may not always work properly with all images, for example, images with really large resolutions.">
i
</button>
</div>
@ -270,8 +270,8 @@ function LeftPaneImageSteps({
</span>{" "}
to{" "}
<span className="font-bold">
{doubleUpscayl ? dimensions.width * 8 : dimensions.width * 4}x
{doubleUpscayl ? dimensions.height * 8 : dimensions.height * 4}
{doubleUpscayl ? dimensions.width * 16 : dimensions.width * 4}x
{doubleUpscayl ? dimensions.height * 16 : dimensions.height * 4}
</span>
</p>
)}

Binary file not shown.

Before

Width:  |  Height:  |  Size: 1.8 MiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 1.1 MiB

3548
yarn.lock

File diff suppressed because it is too large Load Diff