开发
API

AlgorithmManager.doExecute

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

对输入的图像进行算法处理,获取所需算法结果数据

语法

AlgorithmManager.doExecute(options);

参数说明

options 为 object 类型,属性如下:

属性名类型默认值必填说明最低支持版本
inputArrayBuffer输入的图像2.18.0
widthnumber输入的图像的宽度2.18.0
heightnumber输入的高度2.18.0
timeStampnumber当前时间戳2.18.0
successfunction执行成功的回调2.18.0
failfunction执行失败的回调2.18.0

回调成功

object 类型,属性如下:

属性类型说明最低支持版本
与算法类型相关AlgorithmResult算法结果,属性名称与创建 AlgorithmManager 时传入的字段相关,具体内容请参照 AlgorithmResult2.18.0
errMsgstring"doExecute:success"2.18.0

回调失败

object 类型,属性如下:

属性类型说明最低支持版本
errMsgstring"doExecute:fail " + 详细错误信息2.18.0

代码示例

开发者工具中预览

index.js

Page({
  data: {},
  onLoad: function () {
    this.cameraContext = tt.createCameraContext();
  },
  initCanvasConfig() {
    this.cameraContext = tt.createCameraContext();
    this.cameraListener = this.cameraContext.onCameraFrame((frame) => {
      this.frameWidth = frame.width;
      this.frameHeight = frame.height;
      this.canvas.requestAnimationFrame(() => {
        this.onFrame(frame);
      });
    });
    this.cameraListener.start();
  },
  init() {
    this.frameWidth = 480;
    this.frameHeight = 640;

    if (this.algorithmModule == null) {
      this.algorithmModule = require("./AlgorithmModule");
      this.algorithmModule.initAlgorithm(
        this.frameWidth,
        this.frameHeight,
        this.showAlgorithmResult
      );
    }

    this.cameraContext = tt.createCameraContext();
    this.cameraListener = this.cameraContext.onCameraFrame((frame) => {
      this.onFrame(frame);
    });
    this.cameraListener.start();
  },
  onFrame(cameraFrame) {
    if (this.algorithmModule != null) {
      this.algorithmModule.onFrame(cameraFrame.data);
    }
  },
  showAlgorithmResult(algorithmResult) {
    console.log(
      "[debug] showAlgorithmResult :: algorithmResult = ",
      algorithmResult
    );
  },
  onError(e) {
    tt.showModal({
      content: "相机出错了:" + e.detail.errMsg,
    });
  },
});

AlgorithmManager.js

let algorithmManager = null;
let width,
  height = null;
let cb = null;
export function initAlgorithm(_width, _height, onResultFallback) {
  cb = onResultFallback;
  width = _width;
  height = _height;
  tt.getAlgorithmManager({
    width: _width,
    height: _height,
    useSyncMode: true,
    requirements: ["face106"],
    success: (algMgr) => {
      console.log("get algorithm Manager ~");
      console.log(algMgr);
      algorithmManager = algMgr.algorithmManager;
    },
    fail: (errMsg) => {
      console.log(errMsg);
    },
    complete: () => {
      console.log("get alg mgr complete");
    },
  });
}

export function onFrame(cameraData) {
  if (algorithmManager != null) {
    algorithmManager.doExecute({
      input: cameraData,
      width: width,
      height: height,
      timeStamp: Date.now() / 1e9,
      success: (algMgr) => {
        cb(algMgr);
      },
      fail: (errMsg) => {
        console.log(errMsg);
      },
    });
  }
}

Bug & Tip

  • Tip: 执行成功回调的参数与算法管理器类型相关,具体内容请参阅 AlgorithmResult 中不同类型算法管理器对应的算法结果。
点击纠错
评价此篇文档