1
0
mirror of https://github.com/upscayl/upscayl.git synced 2024-12-18 18:35:58 +01:00

Fixed sharpen code

This commit is contained in:
Nayam Amarshe 2022-09-11 15:57:47 +05:30
parent 18cbfd8b50
commit d4ef6acca7
6 changed files with 60 additions and 92 deletions

View File

@ -13,7 +13,9 @@ const binariesPath = isDev
? join(appRootDir.get(), "resources", getPlatform(), "bin") ? join(appRootDir.get(), "resources", getPlatform(), "bin")
: join(dirname(appRootDir.get()), "bin"); : join(dirname(appRootDir.get()), "bin");
const execPath = resolve(join(binariesPath, "./upscayl")); const execPath = (execName) =>
resolve(join(binariesPath, `./upscayl-${execName}`));
const modelsPath = isDev const modelsPath = isDev
? resolve(join(appRootDir.get(), "resources", "models")) ? resolve(join(appRootDir.get(), "resources", "models"))
: resolve(join(dirname(appRootDir.get()), "models")); : resolve(join(dirname(appRootDir.get()), "models"));

View File

@ -29,7 +29,7 @@ app.on("ready", async () => {
console.log("🚀 Icon Path: ", join(__dirname, "icon.png")); console.log("🚀 Icon Path: ", join(__dirname, "icon.png"));
console.log("🚀 Development Mode? :", isDev); console.log("🚀 Development Mode? :", isDev);
console.log("🚀 RS Executable Path: ", execPath); console.log("🚀 RS Executable Path: ", execPath(""));
console.log("🚀 Models: ", modelsPath); console.log("🚀 Models: ", modelsPath);
mainWindow = new BrowserWindow({ mainWindow = new BrowserWindow({
@ -106,10 +106,8 @@ ipcMain.handle(commands.SELECT_FOLDER, async (event, message) => {
}); });
ipcMain.on(commands.SHARPEN, async (event, payload) => { ipcMain.on(commands.SHARPEN, async (event, payload) => {
const model = payload.model;
const scale = payload.scaleFactor;
let inputDir = payload.imagePath.match(/(.*)[\/\\]/)[1] || ""; let inputDir = payload.imagePath.match(/(.*)[\/\\]/)[1] || "";
let outputDir = "./sharpened"; let outputDir = "./sharpened";
if (!fs.existsSync(outputDir)) { if (!fs.existsSync(outputDir)) {
fs.mkdirSync(outputDir); fs.mkdirSync(outputDir);
@ -125,61 +123,53 @@ ipcMain.on(commands.SHARPEN, async (event, payload) => {
const fileExt = parse(fullfileName).ext; const fileExt = parse(fullfileName).ext;
const inputFile = inputDir + "/" + fullfileName; const inputFile = inputDir + "/" + fullfileName;
const copiedInputFile = outputDir + "/" + fullfileName;
const outFile = outputDir + "/" + fileName + "_sharpen" + fileExt; const outFile = outputDir + "/" + fileName + "_sharpen" + fileExt;
fs.copyFile(inputFile, outFile, (err) => { fs.copyFile(inputFile, copiedInputFile, (err) => {
if (err) throw err; if (err) throw err;
}); });
// UPSCALE let sharpen = spawn(
if (fs.existsSync(outFile)) { execPath("realsr"),
// If already upscayled, just output that file [
return outFile; "-i",
} else { copiedInputFile,
let upscayl = spawn( "-o",
execPath + "-realsr", outFile,
[ "-s",
"-i", 4,
inputDir + "/" + fullfileName, "-x",
"-o", "-m",
outFile, modelsPath + "/models-DF2K",
"-s", ],
4, {
"-x", cwd: null,
"-m", detached: false,
modelsPath + "/" + model, }
], );
{
cwd: null,
detached: false,
}
);
let failed = false; let failed = false;
upscayl.stderr.on("data", (stderr) => { sharpen.stderr.on("data", (data) => {
console.log(stderr.toString()); console.log(data.toString());
stderr = stderr.toString(); data = data.toString();
mainWindow.webContents.send(commands.SHARPEN_PROGRESS, stderr.toString()); mainWindow.webContents.send(commands.UPSCAYL_PROGRESS, data.toString());
if (stderr.includes("invalid gpu") || stderr.includes("failed")) { if (data.includes("invalid gpu") || data.includes("failed")) {
failed = true; failed = true;
return null; sharpen.kill("SIGKILL");
} return;
}); }
});
// Send done comamnd when sharpen.on("close", (_) => {
upscayl.on("close", (code) => { if (failed !== true) {
if (failed !== true) { console.log("Done upscaling");
console.log("Done upscaling"); }
return outFile; });
}
});
}
}); });
ipcMain.on(commands.UPSCAYL, async (event, payload) => { ipcMain.on(commands.UPSCAYL, async (event, payload) => {
const model = payload.model; const model = payload.model;
const scale = payload.scaleFactor; const scale = payload.scaleFactor;
const sharpen = payload.sharpen;
let inputDir = payload.imagePath.match(/(.*)[\/\\]/)[1] || ""; let inputDir = payload.imagePath.match(/(.*)[\/\\]/)[1] || "";
let outputDir = payload.outputPath; let outputDir = payload.outputPath;
@ -202,44 +192,25 @@ ipcMain.on(commands.UPSCAYL, async (event, payload) => {
// If already upscayled, just output that file // If already upscayled, just output that file
mainWindow.webContents.send(commands.UPSCAYL_DONE, outFile); mainWindow.webContents.send(commands.UPSCAYL_DONE, outFile);
} else { } else {
let upscayl = model.includes("realesrgan") let upscayl = spawn(
? spawn( execPath + "-realesrgan",
execPath + "-realesrgan", [
[ "-i",
"-i", inputDir + "/" + fullfileName,
inputDir + "/" + fullfileName, "-o",
"-o", outFile,
outFile, "-s",
"-s", scale === 2 ? 4 : scale,
scale === 2 ? 4 : scale, "-m",
"-m", modelsPath,
modelsPath, "-n",
"-n", model,
model, ],
], {
{ cwd: null,
cwd: null, detached: false,
detached: false, }
} );
)
: spawn(
execPath + "-realsr",
[
"-i",
inputDir + "/" + fullfileName,
"-o",
outFile,
"-s",
4,
"-x",
"-m",
modelsPath + "/" + model,
],
{
cwd: null,
detached: false,
}
);
let failed = false; let failed = false;
upscayl.stderr.on("data", (stderr) => { upscayl.stderr.on("data", (stderr) => {
@ -273,7 +244,6 @@ autoUpdater.on("update-available", (_event, releaseNotes, releaseName) => {
}; };
dialog.showMessageBox(dialogOpts, (response) => {}); dialog.showMessageBox(dialogOpts, (response) => {});
}); });
autoUpdater.on("update-downloaded", (_event, releaseNotes, releaseName) => { autoUpdater.on("update-downloaded", (_event, releaseNotes, releaseName) => {
const dialogOpts = { const dialogOpts = {
type: "info", type: "info",

View File

@ -152,10 +152,7 @@ const Home = () => {
if (sharpen) { if (sharpen) {
const sharpenedImage = await window.electron.send(commands.SHARPEN, { const sharpenedImage = await window.electron.send(commands.SHARPEN, {
scaleFactor: 4,
imagePath, imagePath,
outputPath,
model: "models-DF2K",
}); });
console.log("🚀 => upscaylHandler => sharpenedImage", sharpenedImage); console.log("🚀 => upscaylHandler => sharpenedImage", sharpenedImage);
} else { } else {
@ -164,7 +161,6 @@ const Home = () => {
imagePath, imagePath,
outputPath, outputPath,
model, model,
sharpen,
}); });
} }
} else { } else {

Binary file not shown.

Before

Width:  |  Height:  |  Size: 2.2 MiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 1.3 MiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 20 KiB