BytennEngineContext.onError
收藏
我的收藏

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

设置当前 context 的加载失败回调。

语法

BytennEngineContext.onError(callback)

参数说明

function callback

回调参数

加载错误的原因。

返回值

代码示例

开发者工具中预览

const app = getApp();

Page({
  data: {
    item_class: "unknow",
    prob: 0,
  },
  onLoad: function () {
    if (this.offscreenCanvas == null) {
      this.offscreenCanvas = tt.createOffscreenCanvas();
    }
    this.utils = require("./utils");
  },
  createBytennEngine: function () {
    const engineCtx = tt.createBytennEngineContext("littleapp_mobilenet", {
      backend: "auto",
    });
    engineCtx.onLoad((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 前调用本方法设置回调,否则不会触发。