UNPKG

pixteroid

Version:

Pixteroid is a Node.js API designed for efficient image upscaling and restoration, powered by AI and utilizing the NCNN framework. It employs Real-ESRGAN and ESRGAN model weights to upscale and restore images, providing three distinct levels of detail and

111 lines (110 loc) 4.12 kB
#! node.exe "use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const decompress_1 = __importDefault(require("decompress")); const fs_1 = require("fs"); const https_1 = require("https"); const path_1 = require("path"); const util_1 = require("util"); async function _getLatestRelease(owner, repo) { const options = { hostname: "api.github.com", path: `/repos/${owner}/${repo}/releases/latest`, method: "GET", headers: { "User-Agent": "node.js", Accept: "application/vnd.github.v3+json", }, }; return new Promise((resolve, reject) => { (0, https_1.get)(options, (res) => { let data = ""; res.on("data", (chunk) => { data += chunk; }); res.on("end", () => { if (res.statusCode === 200) { const latestRelease = JSON.parse(data); resolve(latestRelease.tag_name); } else { reject(`Failed to fetch the latest release. Status code: ${res.statusCode}`); } }); }).on("error", (err) => { reject(`Error: ${err.message}`); }); }); } function _downloadFile(sourceUrl, to, cb) { (0, https_1.get)(sourceUrl, (response) => { if (response.statusCode === 200) { const fileStream = (0, fs_1.createWriteStream)(to); response.pipe(fileStream); fileStream.on("close", () => { cb(); }); } else if ([301, 302].some((statusCode) => response.statusCode === statusCode)) { const redirectedUrl = response.headers.location; _downloadFile(redirectedUrl, to, cb); return; } else { throw new Error(`Failed to download ncnn: ${response.statusCode} ${response.statusMessage}`); } }).on("error", (err) => { console.error(`Error downloading file: ${err.message}`); }); } function _extractFile(archive, destPath) { console.log("extracting: " + archive); (0, decompress_1.default)(archive, destPath, { strip: 1, }).then(() => { (0, fs_1.rmSync)(archive, { recursive: true }); }); } function install(url, destPath) { const tempFilePath = (0, path_1.join)(__dirname, "temp.zip"); (0, fs_1.mkdirSync)(destPath, { recursive: true }); console.log("fetching: " + url); _downloadFile(url, tempFilePath, () => { _extractFile(tempFilePath, destPath); console.log("ncnn-CLI installation complete."); }); _downloadModels(destPath); } function _downloadModels(destPath) { const sourceUrl = "https://github.com/iamspdarsan/pixteroid/releases/download/0.0.0/model.weights.zip"; const tempFilePath = (0, path_1.join)(__dirname, "models.zip"); const modelPath = (0, path_1.join)(destPath, "models"); _downloadFile(sourceUrl, tempFilePath, () => { _extractFile(tempFilePath, modelPath); console.log("Model weights downloaded."); }); } async function main() { const destpath = (0, path_1.join)(__dirname, "..", "..", "bin/ncnn"); if ((0, fs_1.existsSync)(destpath)) { return; } const downloadPath = "https://github.com/xinntao/Real-ESRGAN-ncnn-vulkan/releases/download/%s/realesrgan-ncnn-vulkan-%s-%s"; const version = await _getLatestRelease("xinntao", "Real-ESRGAN-ncnn-vulkan"); if (process.platform === "darwin") { install((0, util_1.format)(downloadPath, version, version, "macos.zip"), destpath); } else if (process.platform === "win32") { install((0, util_1.format)(downloadPath, version, version, "windows.zip"), destpath); } else if (process.platform === "linux") { install((0, util_1.format)(downloadPath, version, version, "ubuntu.zip"), destpath); } } main().catch((err) => { console.log(err); process.exit(1); });