开发
API

BytennEngineContext.load

基础库 2.29.0 开始支持本方法,这是一个同步方法。

加载配置好的神经网络引擎。

语法

BytennEngineContext.load()

参数说明

返回值

代码示例

开发者工具中预览

const app = getApp();

Page({
  data: {
    item_class: "unknow",
    prob: 0,
  },
  onLoad: function () {
    console.log("Welcome to Mini Code");
    if (this.offscreenCanvas == null) {
      this.offscreenCanvas = tt.createOffscreenCanvas();
    }
    this.utils = require("./utils");
  },
  createBytennEngine: function () {
    console.log("on create clicked");
    const engineCtx = tt.createBytennEngineContext("littleapp_mobilenet", {
      backend: "auto",
    });
    console.log("engine context is: ", engineCtx);
    engineCtx.onLoad((engine) => {
      console.log("load result: ", engine);
      this.engine = engine;
    });
    engineCtx.onError((err) => {
      console.log("load error: ", err);
    });
    engineCtx.load();
  },
  startInfer: function () {
    if (this.cameraCtx == null) {
      this.cameraCtx = tt.createCameraContext();
    }
    this.listener = this.cameraCtx.onCameraFrame((res) => {
      this.engine.infer({
        dataConfig: {
          data: res.data,
          width: res.width,
          height: res.height,
          channel: 4,
          dataType: "U8",
          dataFormat: "NHWC",
        },
        convertConfig: [
          {
            outputChannel: 0,
            normalizeFactor: 255,
            inputConfig: [
              {
                inputChannel: 0,
                weight: 1.0,
              },
            ],
          },
          {
            outputChannel: 1,
            normalizeFactor: 255,
            inputConfig: [
              {
                inputChannel: 1,
                weight: 1.0,
              },
            ],
          },
          {
            outputChannel: 2,
            normalizeFactor: 255,
            inputConfig: [
              {
                inputChannel: 2,
                weight: 1.0,
              },
            ],
          },
        ],
        success: (res) => {
          const classifications = res;
          if (classifications != null) {
            const top = 300;
            const left = 100;

            const embedding = new Float32Array(classifications.data);
            const topK = this.utils.topK(
              embedding,
              this.utils.IMAGENET_CLASSES,
              1
            );
            const { className, probability } = topK[0];
            this.setData({
              item_class: className,
              prob: probability,
            });
          } else {
            console.warn("no predictions!!");
          }
        },
        fail: (err) => {
          console.log(err);
        },
      });
    });
    this.listener.start();
  },
});

Bug & Tip

  • Tip: 需在调用 load 前设置回调,否则无法触发回调。
点击纠错
评价此篇文档