diff --git a/README.md b/README.md index 96e5bdc..10af61f 100644 --- a/README.md +++ b/README.md @@ -1,14 +1,47 @@ -## AI工具箱 +#AI工具箱 - -### 代码编译 - -注意:在Windows下编译时,需要打开Visual Studio命令行 +## 安装 ``` -# 编译第三方库 -node thirdpart/install.js --with-onnx --with-mnn --with-opencv -# 编译主库 +npm install @yizhi/ai +``` + +## 使用 + +```typescript +import ai from "@yizhi/ai"; + +// 配置相应的node插件,插件需自行编译 +ai.config("CV_ADDON_FILE", "/path/to/cv.node"); +ai.config("MNN_ADDON_FILE", "/path/to/mnn.node"); +ai.config("ORT_ADDON_FILE", "/path/to/onnxruntime.node"); + +//直接推理 +const facedet = await ai.deploy.facedet.Yolov5Face.load("YOLOV5S_ONNX"); +const boxes = await facedet.predict("/path/to/image"); + +//使用自己的模型 +const session = new ai.backend.ort.Session(modelBuffer); +const outputs = session.run(inputs); + +``` + +## 插件编译 + +1. 依赖 + 1. python3 + 1. cmake + 1. ninja + 1. c++编译器(gcc,clang,Visual Studio ...) + +1. 编译第三方库 +``` +node thirdpart/install.js --with-mnn --with-onnx --with-opencv +``` +1. 编译插件 +``` cmake -B build -G Ninja . -DCMAKE_BUILD_TYPE=Release cmake --build build --config Release -``` \ No newline at end of file +``` + +注意:注意:在Windows下编译时,需要打开Visual Studio命令行 diff --git a/package.json b/package.json index 1f3b27c..443aeb7 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@yizhi/ai", - "version": "1.0.0", + "version": "1.0.1", "main": "index.js", "scripts": { "watch": "tsc -w --inlineSourceMap" diff --git a/src/backend/mnn/session.ts b/src/backend/mnn/session.ts index ec60ba1..c21fa96 100644 --- a/src/backend/mnn/session.ts +++ b/src/backend/mnn/session.ts @@ -1,3 +1,4 @@ +import { getConfig } from "../../config"; import { CommonSession, dataTypeFrom, isTypedArray, SessionNodeData, SessionNodeInfo, SessionRunInputOption, SessionRunOutput } from "../common"; export class MNNSession extends CommonSession { @@ -7,7 +8,7 @@ export class MNNSession extends CommonSession { public constructor(modelData: Uint8Array) { super(); - const addon = require("../../../build/mnn.node") + const addon = require(getConfig("MNN_ADDON_FILE")); this.#session = new addon.MNNSession(modelData); } diff --git a/src/backend/ort/session.ts b/src/backend/ort/session.ts index 80ca63f..5f04ab6 100644 --- a/src/backend/ort/session.ts +++ b/src/backend/ort/session.ts @@ -1,3 +1,4 @@ +import { getConfig } from "../../config"; import { CommonSession, dataTypeFrom, isTypedArray, SessionNodeData, SessionNodeInfo, SessionRunInputOption, SessionRunOutput } from "../common"; export class OrtSession extends CommonSession { @@ -7,7 +8,7 @@ export class OrtSession extends CommonSession { public constructor(modelData: Uint8Array) { super(); - const addon = require("../../../build/ort.node") + const addon = require(getConfig("ORT_ADDON_FILE")); this.#session = new addon.OrtSession(modelData); } diff --git a/src/config.ts b/src/config.ts new file mode 100644 index 0000000..9e5655a --- /dev/null +++ b/src/config.ts @@ -0,0 +1,13 @@ +import path from "path"; + +const defaultAddonDir = path.join(__dirname, "../build") + +const aiConfig = { + "CV_ADDON_FILE": path.join(defaultAddonDir, "cv.node"), + "MNN_ADDON_FILE": path.join(defaultAddonDir, "mnn.node"), + "ORT_ADDON_FILE": path.join(defaultAddonDir, "ort.node"), +}; + +export function setConfig(key: K, value: typeof aiConfig[K]) { aiConfig[key] = value; } + +export function getConfig(key: K): typeof aiConfig[K] { return aiConfig[key]; } diff --git a/src/cv/mat.ts b/src/cv/mat.ts index 981e156..c597dd1 100644 --- a/src/cv/mat.ts +++ b/src/cv/mat.ts @@ -1,3 +1,4 @@ +import { getConfig } from "../config"; export enum ImreadModes { IMREAD_UNCHANGED = -1, @@ -32,7 +33,7 @@ export class Mat { } public constructor(imageData: Uint8Array, option?: MatConstructorOption) { - const addon = require("../../build/cv.node"); + const addon = require(getConfig("CV_ADDON_FILE")); if ((imageData as any) instanceof addon.Mat) this.#mat = imageData; else this.#mat = new addon.Mat(imageData, option); } diff --git a/src/index.ts b/src/index.ts index e69de29..39fa13a 100644 --- a/src/index.ts +++ b/src/index.ts @@ -0,0 +1,4 @@ +import * as ai from "./main"; + +export default ai; +export { ai }; diff --git a/src/main.ts b/src/main.ts index e69de29..b9010e1 100644 --- a/src/main.ts +++ b/src/main.ts @@ -0,0 +1,4 @@ +export { deploy } from "./deploy"; +export { cv } from "./cv"; +export { backend } from "./backend"; +export { setConfig as config } from "./config"; diff --git a/src/test.ts b/src/test.ts index e089278..4f76338 100644 --- a/src/test.ts +++ b/src/test.ts @@ -4,6 +4,7 @@ import { cv } from "./cv"; import { faceidTestData } from "./test_data/faceid"; import path from "path"; import crypto from "crypto"; +import ai from "."; async function cacheImage(group: string, url: string) { const _url = new URL(url); diff --git a/src/utils/utils.ts b/src/utils/utils.ts deleted file mode 100644 index c0795bf..0000000 --- a/src/utils/utils.ts +++ /dev/null @@ -1,33 +0,0 @@ - -export namespace utils { - - export function rgba2rgb(data: T): T { - const pixelCount = data.length / 4; - const result = new (data.constructor as any)(pixelCount * 3) as T; - for (let i = 0; i < pixelCount; i++) { - result[i * 3 + 0] = data[i * 4 + 0]; - result[i * 3 + 1] = data[i * 4 + 1]; - result[i * 3 + 2] = data[i * 4 + 2]; - } - return result; - } - - export function rgb2bgr(data: T): T { - const pixelCount = data.length / 3; - const result = new (data.constructor as any)(pixelCount * 3) as T; - for (let i = 0; i < pixelCount; i++) { - result[i * 3 + 0] = data[i * 3 + 2]; - result[i * 3 + 1] = data[i * 3 + 1]; - result[i * 3 + 2] = data[i * 3 + 0]; - } - return result; - } - - export function normalize(data: Uint8Array | Float32Array, mean: number[], std: number[]): Float32Array { - const result = new Float32Array(data.length); - for (let i = 0; i < data.length; i++) { - result[i] = (data[i] - mean[i % mean.length]) / std[i % std.length]; - } - return result; - } -}