From a6fd11773686e915de8f47de03e97dc720b4ec00 Mon Sep 17 00:00:00 2001 From: yizhi <946185759@qq.com> Date: Fri, 7 Mar 2025 17:07:02 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E6=A8=A1=E5=9E=8B=E8=BD=AC?= =?UTF-8?q?=E6=8D=A2=E5=B7=A5=E5=85=B7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tool/convert_mnn.js | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 tool/convert_mnn.js diff --git a/tool/convert_mnn.js b/tool/convert_mnn.js new file mode 100644 index 0000000..3fe3584 --- /dev/null +++ b/tool/convert_mnn.js @@ -0,0 +1,36 @@ +const fs = require("fs"); +const path = require("path"); +const { spawnSync } = require("child_process"); + +const CONVERTER_PATH = "C:/Develop/Libs/mnn/bin/MNNConvert.exe"; +const MODEL_PATH = path.join(__dirname, "../models"); + +function onnx2mnn(model) { + const name = path.basename(model, ".onnx"); + const outputFile = path.join(path.dirname(model), name + ".mnn"); + if (fs.existsSync(outputFile)) console.log(`Skip ${name}`); + else { + const result = spawnSync(CONVERTER_PATH, [ + "-f", "ONNX", + "--modelFile", model, + "--MNNModel", outputFile, + "--bizCode", "biz" + ]); + if (result.status !== 0) { + console.error(`Failed to convert ${name}`); + console.log(result.stdout.toString()); + } + else console.log(`Convert ${name} success`); + } +} + +function resolveDir(dir) { + const files = fs.readdirSync(dir); + for (const file of files) { + const filepath = path.join(dir, file); + if (fs.statSync(filepath).isDirectory()) resolveDir(filepath); + else if (path.extname(file) == ".onnx") onnx2mnn(filepath); + } +} + +resolveDir(MODEL_PATH) \ No newline at end of file