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