2024-01-15 12:31:40 +01:00
|
|
|
import fs from "fs";
|
2023-09-11 04:25:14 +02:00
|
|
|
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
|
|
|
saveOutputFolder,
|
2023-11-23 06:39:46 +01:00
|
|
|
setCompression,
|
2023-11-22 16:54:02 +01:00
|
|
|
setNoImageProcessing,
|
2023-09-11 04:25:14 +02:00
|
|
|
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";
|
2024-01-16 07:30:07 +01:00
|
|
|
import COMMAND from "../../common/commands";
|
2023-09-11 04:25:14 +02:00
|
|
|
import convertAndScale from "../utils/convert-and-scale";
|
2023-11-22 16:54:02 +01:00
|
|
|
import { BatchUpscaylPayload } from "../../common/types/types";
|
2024-01-15 10:07:22 +01:00
|
|
|
import { ImageFormat } from "../utils/types";
|
2024-01-15 10:25:29 +01:00
|
|
|
import getModelScale from "../../common/check-model-scale";
|
2024-01-15 11:04:11 +01:00
|
|
|
import removeFileExtension from "../utils/remove-file-extension";
|
2024-01-15 12:30:59 +01:00
|
|
|
import showNotification from "../utils/show-notification";
|
2024-01-16 07:30:07 +01:00
|
|
|
import { DEFAULT_MODELS } from "../../common/models-list";
|
2023-09-11 04:25:14 +02:00
|
|
|
|
2023-11-22 16:54:02 +01:00
|
|
|
const batchUpscayl = async (event, payload: BatchUpscaylPayload) => {
|
2023-09-11 04:25:14 +02:00
|
|
|
const mainWindow = getMainWindow();
|
|
|
|
if (!mainWindow) return;
|
|
|
|
// GET THE MODEL
|
|
|
|
const model = payload.model;
|
|
|
|
const gpuId = payload.gpuId;
|
2024-01-15 10:07:22 +01:00
|
|
|
const saveImageAs = payload.saveImageAs as ImageFormat;
|
2024-01-16 10:22:31 +01:00
|
|
|
console.log("PAYLOAD: ", payload);
|
2023-09-11 04:25:14 +02:00
|
|
|
// GET THE IMAGE DIRECTORY
|
|
|
|
let inputDir = payload.batchFolderPath;
|
|
|
|
// GET THE OUTPUT DIRECTORY
|
2024-01-15 12:30:59 +01:00
|
|
|
let outputFolderPath = payload.outputPath;
|
2023-09-11 04:25:14 +02:00
|
|
|
if (saveOutputFolder === true && outputFolderPath) {
|
2024-01-15 12:30:59 +01:00
|
|
|
outputFolderPath = outputFolderPath;
|
2023-09-11 04:25:14 +02:00
|
|
|
}
|
2024-01-16 10:22:31 +01:00
|
|
|
// ! Don't do fetchLocalStorage() again, it causes the values to be reset
|
2023-11-22 16:54:02 +01:00
|
|
|
setNoImageProcessing(payload.noImageProcessing);
|
2023-11-23 06:39:46 +01:00
|
|
|
setCompression(parseInt(payload.compression));
|
2023-11-22 16:54:02 +01:00
|
|
|
|
2023-09-11 04:25:14 +02:00
|
|
|
const isDefaultModel = DEFAULT_MODELS.includes(model);
|
|
|
|
|
2024-01-15 10:25:29 +01:00
|
|
|
let initialScale = getModelScale(model);
|
|
|
|
|
2023-11-26 08:22:57 +01:00
|
|
|
const desiredScale = payload.scale as string;
|
2023-09-11 04:25:14 +02:00
|
|
|
|
2024-01-15 12:30:59 +01:00
|
|
|
const outputFolderName = `upscayl_${model}_x${
|
|
|
|
noImageProcessing ? initialScale : desiredScale
|
|
|
|
}`;
|
|
|
|
outputFolderPath += slash + outputFolderName;
|
|
|
|
if (!fs.existsSync(outputFolderPath)) {
|
|
|
|
fs.mkdirSync(outputFolderPath, { recursive: true });
|
2023-09-11 04:25:14 +02:00
|
|
|
}
|
2023-12-03 07:36:00 +01:00
|
|
|
|
|
|
|
// Delete .DS_Store files
|
2023-09-11 04:25:14 +02:00
|
|
|
fs.readdirSync(inputDir).forEach((file) => {
|
2024-01-16 09:47:09 +01:00
|
|
|
if (
|
|
|
|
file === ".DS_Store" ||
|
|
|
|
file.toLowerCase() === "desktop.ini" ||
|
|
|
|
file.startsWith(".")
|
|
|
|
) {
|
2023-12-03 07:36:00 +01:00
|
|
|
logit("🗑️ Deleting .DS_Store file");
|
|
|
|
fs.unlinkSync(inputDir + slash + file);
|
2023-09-11 04:25:14 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// UPSCALE
|
|
|
|
const upscayl = spawnUpscayl(
|
|
|
|
getBatchArguments(
|
2023-12-03 07:36:00 +01:00
|
|
|
inputDir,
|
2024-01-15 12:30:59 +01:00
|
|
|
outputFolderPath,
|
2023-09-11 04:25:14 +02:00
|
|
|
isDefaultModel ? modelsPath : customModelsFolderPath ?? modelsPath,
|
|
|
|
model,
|
|
|
|
gpuId,
|
2024-01-15 13:46:21 +01:00
|
|
|
saveImageAs,
|
2023-11-26 08:22:57 +01:00
|
|
|
initialScale
|
2023-09-11 04:25:14 +02:00
|
|
|
),
|
|
|
|
logit
|
|
|
|
);
|
|
|
|
|
|
|
|
childProcesses.push(upscayl);
|
|
|
|
|
|
|
|
setStopped(false);
|
|
|
|
let failed = false;
|
2024-01-15 14:09:46 +01:00
|
|
|
let isAlpha = false;
|
2023-09-11 04:25:14 +02:00
|
|
|
|
|
|
|
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();
|
|
|
|
}
|
2024-01-15 14:09:46 +01:00
|
|
|
if (data.includes("has alpha channel")) {
|
|
|
|
isAlpha = true;
|
|
|
|
}
|
2023-09-11 04:25:14 +02:00
|
|
|
};
|
|
|
|
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);
|
2024-01-15 12:30:59 +01:00
|
|
|
mainWindow.webContents.send(
|
|
|
|
COMMAND.FOLDER_UPSCAYL_DONE,
|
|
|
|
outputFolderPath
|
|
|
|
);
|
2023-09-19 17:04:31 +02:00
|
|
|
return;
|
|
|
|
}
|
2023-11-26 14:06:23 +01:00
|
|
|
|
2023-12-03 07:36:00 +01:00
|
|
|
const files = fs.readdirSync(inputDir);
|
2023-09-11 04:25:14 +02:00
|
|
|
try {
|
|
|
|
files.forEach(async (file) => {
|
2024-01-15 12:30:59 +01:00
|
|
|
if (file.startsWith(".") || file === outputFolderName) return;
|
2024-01-15 11:04:11 +01:00
|
|
|
console.log("Filename: ", removeFileExtension(file));
|
2023-09-11 04:25:14 +02:00
|
|
|
await convertAndScale(
|
2023-12-03 07:36:00 +01:00
|
|
|
inputDir + slash + file,
|
2024-01-15 14:09:46 +01:00
|
|
|
isAlpha
|
|
|
|
? `${outputFolderPath}${slash}${removeFileExtension(file)}.png`
|
|
|
|
: `${outputFolderPath}${slash}${removeFileExtension(
|
|
|
|
file
|
|
|
|
)}.${saveImageAs}`,
|
2024-01-15 13:50:36 +01:00
|
|
|
`${outputFolderPath}${slash}${removeFileExtension(
|
|
|
|
file
|
|
|
|
)}.${saveImageAs}`,
|
2023-11-26 08:22:57 +01:00
|
|
|
desiredScale,
|
2023-09-11 04:25:14 +02:00
|
|
|
saveImageAs,
|
|
|
|
onError
|
|
|
|
);
|
|
|
|
});
|
2024-01-15 12:30:59 +01:00
|
|
|
mainWindow.webContents.send(
|
|
|
|
COMMAND.FOLDER_UPSCAYL_DONE,
|
|
|
|
outputFolderPath
|
|
|
|
);
|
|
|
|
showNotification("Upscayled", "Image upscayled successfully!");
|
2023-09-11 04:25:14 +02:00
|
|
|
} 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
|
|
|
);
|
2024-01-15 12:30:59 +01:00
|
|
|
showNotification("Upscayl Failure", "Failed to upscale image!");
|
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;
|