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

View File

@ -1,7 +1,7 @@
import { spawn } from "child_process"; import { spawn } from "child_process";
import { execPath } from "./binaries"; import { execPath } from "./binaries";
export const spawnUpscayl = (command: string[], binaryName: string) => { export const spawnUpscayl = (binaryName: string, command: string[]) => {
console.log(" Command: ", command); console.log(" Command: ", command);
const spawnedProcess = spawn(execPath(binaryName), 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 fileName = (0, path_1.parse)(fullfileName).name;
const fileExt = (0, path_1.parse)(fullfileName).ext; const fileExt = (0, path_1.parse)(fullfileName).ext;
const outFile = outputDir + "/" + fileName + "_upscayl_8x_" + model + "." + saveImageAs; const outFile = outputDir + "/" + fileName + "_upscayl_8x_" + model + "." + saveImageAs;
// UPSCALE const commandArguments = [
let upscayl = (0, child_process_1.spawn)((0, binaries_1.execPath)("realesrgan"), [
"-i", "-i",
inputDir + "/" + fullfileName, inputDir + "/" + fullfileName,
"-o", "-o",
@ -138,11 +137,9 @@ electron_1.ipcMain.on(commands_1.default.DOUBLE_UPSCAYL, (event, payload) => __a
gpuId ? `-g ${gpuId}` : "", gpuId ? `-g ${gpuId}` : "",
"-f", "-f",
saveImageAs, saveImageAs,
], { ];
cwd: undefined, // UPSCALE
detached: false, let upscayl = (0, upscayl_1.spawnUpscayl)("realesrgan", commandArguments).process;
});
console.log("🆙 COMMAND:", "-i", inputDir + "/" + fullfileName, "-o", outFile, "-s", 4, "-m", binaries_1.modelsPath, "-n", model, gpuId ? `-g ${gpuId}` : "", "-f", saveImageAs);
let failed = false; let failed = false;
let isAlpha = false; let isAlpha = false;
// TAKE UPSCAYL OUTPUT // 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")) { if (data.includes("invalid gpu") || data.includes("failed")) {
failed = true; failed = true;
} }
// IF IMAGE HAS ALPHA CHANNEL
if (data.includes("has alpha channel")) { if (data.includes("has alpha channel")) {
isAlpha = true; isAlpha = true;
} }
@ -171,11 +169,10 @@ electron_1.ipcMain.on(commands_1.default.DOUBLE_UPSCAYL, (event, payload) => __a
return; return;
}); });
// ON UPSCAYL DONE // ON UPSCAYL DONE
upscayl.on("close", (code) => { upscayl.on("close", () => {
// IF NOT FAILED // IF NOT FAILED
if (!failed) { if (!failed) {
// UPSCALE const commandArguments2 = [
let upscayl2 = (0, child_process_1.spawn)((0, binaries_1.execPath)("realesrgan"), [
"-i", "-i",
isAlpha ? outFile + ".png" : outFile, isAlpha ? outFile + ".png" : outFile,
"-o", "-o",
@ -189,10 +186,9 @@ electron_1.ipcMain.on(commands_1.default.DOUBLE_UPSCAYL, (event, payload) => __a
gpuId ? `-g ${gpuId}` : "", gpuId ? `-g ${gpuId}` : "",
"-f", "-f",
isAlpha ? "" : saveImageAs, isAlpha ? "" : saveImageAs,
], { ];
cwd: undefined, // UPSCALE
detached: false, let upscayl2 = (0, upscayl_1.spawnUpscayl)("realesrgan", commandArguments2).process;
});
let failed2 = false; let failed2 = false;
// TAKE UPSCAYL OUTPUT // TAKE UPSCAYL OUTPUT
upscayl2.stderr.on("data", (data) => { upscayl2.stderr.on("data", (data) => {
@ -296,10 +292,10 @@ electron_1.ipcMain.on(commands_1.default.UPSCAYL, (event, payload) => __awaiter(
]; ];
switch (model) { switch (model) {
default: default:
upscayl = (0, upscayl_1.spawnUpscayl)(defaultArguments, "realesrgan"); upscayl = (0, upscayl_1.spawnUpscayl)("realesrgan", defaultArguments);
break; break;
case "models-DF2K": case "models-DF2K":
upscayl = (0, upscayl_1.spawnUpscayl)(sharpenArguments, "realsr"); upscayl = (0, upscayl_1.spawnUpscayl)("realsr", sharpenArguments);
break; break;
} }
let isAlpha = false; let isAlpha = false;
@ -349,11 +345,7 @@ electron_1.ipcMain.on(commands_1.default.FOLDER_UPSCAYL, (event, payload) => __a
if (!fs_1.default.existsSync(outputDir)) { if (!fs_1.default.existsSync(outputDir)) {
fs_1.default.mkdirSync(outputDir, { recursive: true }); fs_1.default.mkdirSync(outputDir, { recursive: true });
} }
// UPSCALE const commandArguments = [
let upscayl = null;
switch (model) {
default:
upscayl = (0, child_process_1.spawn)((0, binaries_1.execPath)("realesrgan"), [
"-i", "-i",
inputDir, inputDir,
"-o", "-o",
@ -367,14 +359,8 @@ electron_1.ipcMain.on(commands_1.default.FOLDER_UPSCAYL, (event, payload) => __a
gpuId ? `-g ${gpuId}` : "", gpuId ? `-g ${gpuId}` : "",
"-f", "-f",
saveImageAs, saveImageAs,
], { ];
cwd: undefined, const sharpenArguments = [
detached: false,
});
console.log("🆙 COMMAND:", "-i", inputDir, "-o", outputDir, "-s", 4, "-m", binaries_1.modelsPath, "-n", model, gpuId ? `-g ${gpuId}` : "", "-f", saveImageAs);
break;
case "models-DF2K":
upscayl = (0, child_process_1.spawn)((0, binaries_1.execPath)("realsr"), [
"-i", "-i",
inputDir, inputDir,
"-o", "-o",
@ -387,11 +373,15 @@ electron_1.ipcMain.on(commands_1.default.FOLDER_UPSCAYL, (event, payload) => __a
gpuId ? `-g ${gpuId}` : "", gpuId ? `-g ${gpuId}` : "",
"-f", "-f",
saveImageAs, saveImageAs,
], { ];
cwd: undefined, // UPSCALE
detached: false, let upscayl = null;
}); switch (model) {
console.log("🆙 COMMAND:", "-i", inputDir, "-o", outputDir, "-s", 4, "-x", "-m", binaries_1.modelsPath + "/" + model, gpuId ? `-g ${gpuId}` : "", "-f", saveImageAs); default:
upscayl = (0, upscayl_1.spawnUpscayl)("realesrgan", commandArguments).process;
break;
case "models-DF2K":
upscayl = (0, upscayl_1.spawnUpscayl)("realsr", sharpenArguments).process;
break; break;
} }
let failed = false; 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", "@types/react-dom": "^18.0.8",
"autoprefixer": "^10.4.13", "autoprefixer": "^10.4.13",
"cross-env": "^7.0.3", "cross-env": "^7.0.3",
"electron": "^21.2.2", "electron": "^23.1.4",
"electron-builder": "^23.6.0", "electron-builder": "^24.0.0",
"next": "^13.0.2", "next": "^13.0.2",
"postcss": "^8.4.18", "postcss": "^8.4.18",
"prettier": "^2.7.1", "prettier": "^2.7.1",

View File

@ -241,7 +241,7 @@ function LeftPaneImageSteps({
</p> </p>
<button <button
className="badge-info badge cursor-help" 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 i
</button> </button>
</div> </div>
@ -270,8 +270,8 @@ function LeftPaneImageSteps({
</span>{" "} </span>{" "}
to{" "} to{" "}
<span className="font-bold"> <span className="font-bold">
{doubleUpscayl ? dimensions.width * 8 : dimensions.width * 4}x {doubleUpscayl ? dimensions.width * 16 : dimensions.width * 4}x
{doubleUpscayl ? dimensions.height * 8 : dimensions.height * 4} {doubleUpscayl ? dimensions.height * 16 : dimensions.height * 4}
</span> </span>
</p> </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