-
Notifications
You must be signed in to change notification settings - Fork 0
/
pixteroid.ts
137 lines (118 loc) · 3.37 KB
/
pixteroid.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
import { exec } from "child_process";
import { cpus } from "os";
import { join, relative, resolve } from "path";
type LevelOptions =
| "level1" //normal structutral details
| "level2" //best structutral details
| "level3"; //more and best structutral details
function _nonZeroAssignment(percentage: number): number {
const int: number = Math.floor((percentage * threads) / 100);
return int === 0 ? 1 : int;
}
/*
*@param - executable : String => path of executable/binary.
*@param - commandArgs : Object => command line arguments(switch and value).
*@returns - commandLine : String => executable commandline for cli.
*/
function _commandConstrutor(
executable: string,
commandArgs: Record<string, string | number>,
): string {
let commandline: string = `${executable}`;
Object.keys(commandArgs).forEach((option: string) => {
commandline += ` ${option} ${commandArgs[option]}`;
});
return commandline.replaceAll("\\", "/");
}
const binary: string =
join(__dirname, "..") +
`/bin/ncnn/realesrgan-ncnn-vulkan${
process.platform === "win32" ? ".exe" : ""
}`;
const threads: number = cpus().length;
const decode: number = _nonZeroAssignment(10);
const proc: number = _nonZeroAssignment(65);
const encode: number = _nonZeroAssignment(25);
export function upscale(
input: string,
output: string,
level: LevelOptions,
): Promise<boolean> {
//making absolute path
input = resolve(relative(process.cwd(), input));
output = resolve(relative(process.cwd(), output));
/* constructing command with arguments */
const commandArgs: Record<string, string | number> = {
"-i": input,
"-o": output,
"-s": 4,
"-n": level,
"-j": `${decode}:${proc}:${encode}`,
};
const commandline: string = _commandConstrutor(binary, commandArgs);
return new Promise((resolve, reject) => {
exec(commandline)
.on("exit", (code: number, signal: number) => {
if (code === 0) {
resolve(true);
} else {
reject(
`Unexpected exit code: ${code} -|- signal: ${signal}\n${commandline}`,
);
}
})
.on("error", (err: Error) => {
reject(err.message);
});
});
}
export async function upscaleAll(
inputs: string[],
basePath: string,
level: LevelOptions,
batchSize: number = 2,
): Promise<void> {
console.log("Number of images in queue: " + inputs.length);
console.log(
"Number of cycles: " + Math.floor(inputs.length / batchSize),
);
console.log("Number of batches per cycle: " + batchSize);
const outputPromises: (() => Promise<void>)[] = inputs.map(
(input: string) => {
return (): Promise<void> => {
return new Promise((resolve, reject) => {
const output: string = join(
basePath,
relative(process.cwd(), input),
);
upscale(input, output, level)
.then((success: boolean) => {
if (success) {
resolve();
} else {
reject("Upscale failed: " + input);
}
})
.catch((err: Error) => {
reject(err);
});
});
};
},
);
//batching
const promiseBatches: (() => Promise<void>)[][] = [];
for (let i: number = 0; i < outputPromises.length; i += batchSize) {
promiseBatches.push(outputPromises.slice(i, i + batchSize));
}
/* Resolving batches */
for (const batch of promiseBatches) {
const activatedBatch: Promise<void>[] = batch.map((func) => func());
try {
await Promise.all(activatedBatch);
} catch (error) {
console.log(error);
process.exit(1);
}
}
}