From f839df5e019b1e0e4f98459d239db5a122c2b04c Mon Sep 17 00:00:00 2001 From: Nayam Amarshe <25067102+NayamAmarshe@users.noreply.github.com> Date: Sat, 17 Sep 2022 15:10:47 +0530 Subject: [PATCH] Updated Double Upscayl --- main/commands.js | 6 +- main/index.js | 128 +++++++++--------- main/models.js | 0 main/utils.js | 25 ++++ renderer/components/LeftPaneSteps.jsx | 38 +++--- renderer/components/ProgressBar.jsx | 15 +- renderer/pages/index.jsx | 39 +++--- .../models}/realesrgan-x4plus-anime.bin | Bin .../models}/realesrgan-x4plus-anime.param | 0 sharpened/Trollface_non-free.png | Bin 82578 -> 0 bytes sharpened/Trollface_non-free_sharpen.png | Bin 1317786 -> 0 bytes sharpened/to_upscale_sharpen.jpeg | Bin 832298 -> 0 bytes .../to_upscale.jpeg => to_upscale copy.jpeg | Bin 13 files changed, 133 insertions(+), 118 deletions(-) delete mode 100644 main/models.js create mode 100644 main/utils.js rename {models/future-use => resources/models}/realesrgan-x4plus-anime.bin (100%) rename {models/future-use => resources/models}/realesrgan-x4plus-anime.param (100%) delete mode 100644 sharpened/Trollface_non-free.png delete mode 100644 sharpened/Trollface_non-free_sharpen.png delete mode 100644 sharpened/to_upscale_sharpen.jpeg rename sharpened/to_upscale.jpeg => to_upscale copy.jpeg (100%) diff --git a/main/commands.js b/main/commands.js index 4aa1284..49efe00 100644 --- a/main/commands.js +++ b/main/commands.js @@ -4,9 +4,9 @@ const commands = { UPSCAYL: "Upscale the Image", UPSCAYL_DONE: "Upscaling Done", UPSCAYL_PROGRESS: "Send Progress from Main to Renderer", - SHARPEN: "Sharpen the Image First", - SHARPEN_PROGRESS: "Send Sharpening Progress from Main to Renderer", - SHARPEN_DONE: "Sharpening Done", + DOUBLE_UPSCAYL: "Double Upscale the Image", + DOUBLE_UPSCAYL_DONE: "Double Upscaling Done", + DOUBLE_UPSCAYL_PROGRESS: "Send Double Upscayl Progress from Main to Renderer", }; module.exports = commands; diff --git a/main/index.js b/main/index.js index aa30582..e893229 100644 --- a/main/index.js +++ b/main/index.js @@ -105,46 +105,36 @@ ipcMain.handle(commands.SELECT_FOLDER, async (event, message) => { } }); -ipcMain.on(commands.SHARPEN, async (event, payload) => { +ipcMain.on(commands.DOUBLE_UPSCAYL, async (event, payload) => { const model = payload.model; - const scale = payload.scaleFactor; let inputDir = payload.imagePath.match(/(.*)[\/\\]/)[1] || ""; - const platform = getPlatform(); - - let outputDir = platform === "win" ? ".\\sharpened" : "./sharpened"; - if (!fs.existsSync(outputDir)) { - fs.mkdirSync(outputDir); - } + let outputDir = payload.outputPath; + console.log(outputDir); // COPY IMAGE TO TMP FOLDER + const platform = getPlatform(); const fullfileName = platform === "win" ? payload.imagePath.split("\\").slice(-1)[0] : payload.imagePath.split("/").slice(-1)[0]; const fileName = parse(fullfileName).name; const fileExt = parse(fullfileName).ext; + const outFile = outputDir + "/" + fileName + "_upscayl_8x_" + model + fileExt; - const inputFile = inputDir + "/" + fullfileName; - const copiedInputFile = outputDir + "/" + fullfileName; - const sharpenedFile = platform === "win" ? outputDir + "\\" + fileName + "_sharpen" + fileExt : outputDir + "/" + fileName + "_sharpen" + fileExt; - const outFile = - inputDir + "/" + fileName + "_upscayl_" + scale + "x_" + model + fileExt; - fs.copyFile(inputFile, copiedInputFile, (err) => { - if (err) throw err; - }); - - let sharpen = spawn( - execPath("realsr"), + // UPSCALE + let upscayl = spawn( + execPath("realesrgan"), [ "-i", - copiedInputFile, + inputDir + "/" + fullfileName, "-o", - sharpenedFile, + outFile, "-s", 4, - "-x", "-m", - modelsPath + "/models-DF2K", + modelsPath, + "-n", + model, ], { cwd: null, @@ -153,65 +143,77 @@ ipcMain.on(commands.SHARPEN, async (event, payload) => { ); let failed = false; - sharpen.stderr.on("data", (data) => { - console.log(data.toString()); + // TAKE UPSCAYL OUTPUT + upscayl.stderr.on("data", (data) => { + // CONVERT DATA TO STRING data = data.toString(); - mainWindow.webContents.send(commands.SHARPEN_PROGRESS, 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")) { failed = true; - sharpen.kill("SIGKILL"); - return; } }); - sharpen.on("close", (_) => { - if (failed !== true) { - console.log("Done sharpening: ", outFile); - mainWindow.webContents.send(commands.SHARPEN_DONE, outFile); - let upscayl = spawn( + + // IF ERROR + upscayl.on("error", (data) => { + data.toString(); + // SEND UPSCAYL PROGRESS TO RENDERER + mainWindow.webContents.send(commands.DOUBLE_UPSCAYL_PROGRESS, data); + // SET FAILED TO TRUE + failed = true; + return; + }); + + // ON UPSCAYL DONE + upscayl.on("close", (code) => { + // IF NOT FAILED + if (!failed) { + // UPSCALE + let upscayl2 = spawn( execPath("realesrgan"), - [ - "-i", - sharpenedFile, - "-o", - outFile, - "-s", - scale === 2 ? 4 : scale, - "-m", - modelsPath, - "-n", - model, - ], + ["-i", outFile, "-o", outFile, "-s", 4, "-m", modelsPath, "-n", model], { cwd: null, detached: false, } ); - let failed = false; - upscayl.stderr.on("data", (data) => { - console.log( - "🚀 => upscayl.stderr.on => stderr.toString()", - data.toString() - ); + + let failed2 = false; + // TAKE UPSCAYL OUTPUT + upscayl2.stderr.on("data", (data) => { + // CONVERT DATA TO STRING data = data.toString(); - mainWindow.webContents.send(commands.UPSCAYL_PROGRESS, 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")) { - failed = true; + failed2 = true; } }); - upscayl.on("error", (data) => { - mainWindow.webContents.send(commands.UPSCAYL_PROGRESS, data.toString()); - failed = 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; }); - // Send done comamnd when - upscayl.on("close", (code) => { - if (failed !== true) { + + upscayl2.on("close", (code) => { + if (!failed2) { console.log("Done upscaling"); - mainWindow.webContents.send(commands.UPSCAYL_DONE, outFile); + mainWindow.webContents.send(commands.DOUBLE_UPSCAYL_DONE, outFile); } - }) + }); } - }) + }); }); ipcMain.on(commands.UPSCAYL, async (event, payload) => { @@ -227,7 +229,7 @@ ipcMain.on(commands.UPSCAYL, async (event, payload) => { platform === "win" ? payload.imagePath.split("\\").slice(-1)[0] : payload.imagePath.split("/").slice(-1)[0]; - console.log(fullfileName) + console.log(fullfileName); const fileName = parse(fullfileName).name; const fileExt = parse(fullfileName).ext; const outFile = diff --git a/main/models.js b/main/models.js deleted file mode 100644 index e69de29..0000000 diff --git a/main/utils.js b/main/utils.js new file mode 100644 index 0000000..3779b64 --- /dev/null +++ b/main/utils.js @@ -0,0 +1,25 @@ +const { spawn } = require("child_process"); +const { execPath } = require("./binaries"); +/** + * + * @param {*} inputFile + * @param {*} outFile + * @param {*} modelsPath + * @param {*} model + * @returns + */ +function upscaylImage(inputFile, outFile, modelsPath, model) { + // UPSCALE + let upscayl = spawn( + execPath("realesrgan"), + ["-i", inputFile, "-o", outFile, "-s", 4, "-m", modelsPath, "-n", model], + { + cwd: null, + detached: false, + } + ); + + return upscayl; +} + +module.exports = { upscaylImage }; diff --git a/renderer/components/LeftPaneSteps.jsx b/renderer/components/LeftPaneSteps.jsx index d7a993c..14b362b 100644 --- a/renderer/components/LeftPaneSteps.jsx +++ b/renderer/components/LeftPaneSteps.jsx @@ -49,22 +49,24 @@ function LeftPaneSteps(props) {

Select Upscaling Type

-
- { - props.setSharpen(e.target.checked); - }} - /> -

- Sharpen Image -

-
+ {props.model !== "models-DF2K" && ( +
+ { + props.setDoubleUpscayl(e.target.checked); + }} + /> +

+ Double Upscayl +

+
+ )} @@ -129,8 +132,7 @@ function LeftPaneSteps(props) { > - - + diff --git a/renderer/components/ProgressBar.jsx b/renderer/components/ProgressBar.jsx index c982d5a..597151b 100644 --- a/renderer/components/ProgressBar.jsx +++ b/renderer/components/ProgressBar.jsx @@ -8,18 +8,11 @@ function ProgressBar(props) {
-

- {props.sharpening ? props.sharpeningProgress : props.progress} +

{props.progress}

+ +

+ Doing the Upscayl magic...

- {props.sharpening ? ( -

- Sharpening your Image... -

- ) : ( -

- Doing the Upscayl magic... -

- )}
); diff --git a/renderer/pages/index.jsx b/renderer/pages/index.jsx index ea2065e..6211e09 100644 --- a/renderer/pages/index.jsx +++ b/renderer/pages/index.jsx @@ -21,10 +21,7 @@ const Home = () => { const [loaded, setLoaded] = useState(false); const [version, setVersion] = useState(""); const [batchMode, setBatchMode] = useState(false); - const [sharpen, setSharpen] = useState(false); - const [sharpening, setSharpening] = useState(false); - const [sharpenedImagePath, setSharpenedImagePath] = useState(""); - const [sharpeningProgress, setSharpeningProgress] = useState(""); + const [doubleUpscayl, setDoubleUpscayl] = useState(false); const resetImagePaths = () => { setProgress(""); @@ -64,9 +61,9 @@ const Home = () => { handleErrors(data); }); - window.electron.on(commands.SHARPEN_PROGRESS, (_, data) => { + window.electron.on(commands.DOUBLE_UPSCAYL_PROGRESS, (_, data) => { if (data.length > 0 && data.length < 10) { - setSharpeningProgress(data); + setProgress(data); } handleErrors(data); }); @@ -75,8 +72,8 @@ const Home = () => { setProgress(""); setUpscaledImagePath(data); }); - window.electron.on(commands.SHARPEN_DONE, (_, data) => { - setSharpenedImagePath(data); + window.electron.on(commands.DOUBLE_UPSCAYL_DONE, (_, data) => { + setUpscaledImagePath(data); }); }, []); @@ -167,24 +164,23 @@ const Home = () => { setUpscaledImagePath(""); if (imagePath !== "") { setProgress("Hold on..."); + if (model === "models-DF2K") { + setDoubleUpscayl(false); + } - if (sharpen) { - setSharpening(true); - const sharpenResponse = await window.electron.send(commands.SHARPEN, { - scaleFactor, + if (doubleUpscayl) { + await window.electron.send(commands.DOUBLE_UPSCAYL, { imagePath, outputPath, model, }); - console.log("🚀 => upscaylHandler => sharpenResponse", sharpenResponse); - } - else { + } else { await window.electron.send(commands.UPSCAYL, { scaleFactor, imagePath, outputPath, model, - }) + }); } } else { alert("Please select an image to upscale"); @@ -212,8 +208,9 @@ const Home = () => { setBatchMode={setBatchMode} imagePath={imagePath} outputPath={outputPath} - sharpen={sharpen} - setSharpen={setSharpen} + doubleUpscayl={doubleUpscayl} + setDoubleUpscayl={setDoubleUpscayl} + model={model} />