2023-09-11 04:25:14 +02:00
|
|
|
import fs from "fs";
|
|
|
|
import { getMainWindow } from "../main-window";
|
|
|
|
import {
|
|
|
|
childProcesses,
|
|
|
|
customModelsFolderPath,
|
2023-09-19 17:04:31 +02:00
|
|
|
noImageProcessing,
|
2023-09-11 04:25:14 +02:00
|
|
|
outputFolderPath,
|
|
|
|
saveOutputFolder,
|
|
|
|
setStopped,
|
|
|
|
stopped,
|
|
|
|
} from "../utils/config-variables";
|
|
|
|
import logit from "../utils/logit";
|
|
|
|
import { spawnUpscayl } from "../utils/spawn-upscayl";
|
|
|
|
import { getBatchArguments } from "../utils/get-arguments";
|
|
|
|
import slash from "../utils/slash";
|
|
|
|
import { modelsPath } from "../utils/get-resource-paths";
|
|
|
|
import COMMAND from "../constants/commands";
|
|
|
|
import convertAndScale from "../utils/convert-and-scale";
|
|
|
|
import DEFAULT_MODELS from "../constants/models";
|
|
|
|
|
|
|
|
const batchUpscayl = async (event, payload) => {
|
|
|
|
const mainWindow = getMainWindow();
|
|
|
|
if (!mainWindow) return;
|
|
|
|
// GET THE MODEL
|
|
|
|
const model = payload.model;
|
|
|
|
const gpuId = payload.gpuId;
|
|
|
|
const saveImageAs = payload.saveImageAs;
|
|
|
|
// const scale = payload.scale as string;
|
|
|
|
|
|
|
|
// GET THE IMAGE DIRECTORY
|
|
|
|
let inputDir = payload.batchFolderPath;
|
|
|
|
// GET THE OUTPUT DIRECTORY
|
|
|
|
let outputDir = payload.outputPath;
|
|
|
|
|
|
|
|
if (saveOutputFolder === true && outputFolderPath) {
|
|
|
|
outputDir = outputFolderPath;
|
|
|
|
}
|
|
|
|
|
|
|
|
const isDefaultModel = DEFAULT_MODELS.includes(model);
|
|
|
|
|
|
|
|
let scale = "4";
|
2023-11-10 14:15:48 +01:00
|
|
|
if (model.includes("x1")) {
|
|
|
|
scale = "1";
|
|
|
|
} else if (model.includes("x2")) {
|
2023-09-11 04:25:14 +02:00
|
|
|
scale = "2";
|
|
|
|
} else if (model.includes("x3")) {
|
|
|
|
scale = "3";
|
|
|
|
} else {
|
|
|
|
scale = "4";
|
|
|
|
}
|
|
|
|
|
2023-09-25 05:39:21 +02:00
|
|
|
outputDir += slash + `upscayl_${model}_x${payload.scale}`;
|
2023-09-11 04:25:14 +02:00
|
|
|
if (!fs.existsSync(outputDir)) {
|
|
|
|
fs.mkdirSync(outputDir, { recursive: true });
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete .DS_Store files
|
|
|
|
fs.readdirSync(inputDir).forEach((file) => {
|
|
|
|
if (file === ".DS_Store") {
|
|
|
|
logit("🗑️ Deleting .DS_Store file");
|
|
|
|
fs.unlinkSync(inputDir + slash + file);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// UPSCALE
|
|
|
|
const upscayl = spawnUpscayl(
|
|
|
|
"realesrgan",
|
|
|
|
getBatchArguments(
|
|
|
|
inputDir,
|
|
|
|
outputDir,
|
|
|
|
isDefaultModel ? modelsPath : customModelsFolderPath ?? modelsPath,
|
|
|
|
model,
|
|
|
|
gpuId,
|
|
|
|
"png",
|
|
|
|
scale
|
|
|
|
),
|
|
|
|
logit
|
|
|
|
);
|
|
|
|
|
|
|
|
childProcesses.push(upscayl);
|
|
|
|
|
|
|
|
setStopped(false);
|
|
|
|
let failed = false;
|
|
|
|
|
|
|
|
const onData = (data: any) => {
|
|
|
|
if (!mainWindow) return;
|
|
|
|
data = data.toString();
|
|
|
|
mainWindow.webContents.send(
|
|
|
|
COMMAND.FOLDER_UPSCAYL_PROGRESS,
|
|
|
|
data.toString()
|
|
|
|
);
|
|
|
|
if (data.includes("invalid") || data.includes("failed")) {
|
|
|
|
logit("❌ INVALID GPU OR INVALID FILES IN FOLDER - FAILED");
|
|
|
|
failed = true;
|
|
|
|
upscayl.kill();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
const onError = (data: any) => {
|
|
|
|
if (!mainWindow) return;
|
|
|
|
mainWindow.setProgressBar(-1);
|
|
|
|
mainWindow.webContents.send(
|
|
|
|
COMMAND.FOLDER_UPSCAYL_PROGRESS,
|
|
|
|
data.toString()
|
|
|
|
);
|
|
|
|
failed = true;
|
|
|
|
upscayl.kill();
|
|
|
|
mainWindow &&
|
|
|
|
mainWindow.webContents.send(
|
|
|
|
COMMAND.UPSCAYL_ERROR,
|
|
|
|
"Error upscaling image. Error: " + data
|
|
|
|
);
|
|
|
|
return;
|
|
|
|
};
|
|
|
|
const onClose = () => {
|
|
|
|
if (!mainWindow) return;
|
|
|
|
if (!failed && !stopped) {
|
|
|
|
logit("💯 Done upscaling");
|
|
|
|
logit("♻ Scaling and converting now...");
|
|
|
|
upscayl.kill();
|
|
|
|
mainWindow && mainWindow.webContents.send(COMMAND.SCALING_AND_CONVERTING);
|
2023-09-19 17:04:31 +02:00
|
|
|
if (noImageProcessing) {
|
|
|
|
logit("🚫 Skipping scaling and converting");
|
|
|
|
mainWindow.setProgressBar(-1);
|
|
|
|
mainWindow.webContents.send(COMMAND.FOLDER_UPSCAYL_DONE, outputDir);
|
|
|
|
return;
|
|
|
|
}
|
2023-09-11 04:25:14 +02:00
|
|
|
// Get number of files in output folder
|
|
|
|
const files = fs.readdirSync(inputDir);
|
|
|
|
try {
|
|
|
|
files.forEach(async (file) => {
|
|
|
|
console.log("Filename: ", file.slice(0, -3));
|
|
|
|
await convertAndScale(
|
|
|
|
inputDir + slash + file,
|
|
|
|
outputDir + slash + file.slice(0, -3) + "png",
|
|
|
|
outputDir + slash + file.slice(0, -3) + saveImageAs,
|
|
|
|
payload.scale,
|
|
|
|
saveImageAs,
|
|
|
|
onError
|
|
|
|
);
|
|
|
|
// Remove the png file (default) if the saveImageAs is not png
|
|
|
|
if (saveImageAs !== "png") {
|
|
|
|
fs.unlinkSync(outputDir + slash + file.slice(0, -3) + "png");
|
|
|
|
}
|
|
|
|
});
|
|
|
|
mainWindow.webContents.send(COMMAND.FOLDER_UPSCAYL_DONE, outputDir);
|
|
|
|
} catch (error) {
|
|
|
|
logit("❌ Error processing (scaling and converting) the image.", error);
|
|
|
|
upscayl.kill();
|
|
|
|
mainWindow &&
|
|
|
|
mainWindow.webContents.send(
|
|
|
|
COMMAND.UPSCAYL_ERROR,
|
2023-10-14 10:41:57 +02:00
|
|
|
"Error processing (scaling and converting) the image. Please report this error on Upscayl GitHub Issues page.\n" +
|
|
|
|
error
|
2023-09-11 04:25:14 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
upscayl.kill();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
upscayl.process.stderr.on("data", onData);
|
|
|
|
upscayl.process.on("error", onError);
|
|
|
|
upscayl.process.on("close", onClose);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default batchUpscayl;
|