开发
API

tt.getAlgorithmManager

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

获取传入字段对应的算法管理器

语法

tt.getAlgorithmManager(options);

参数说明

options 为 object 类型,属性如下:

属性名类型默认值必填说明最低支持版本
widthnumber输入图像的宽度2.18.0
heightnumber输入图像的高度2.18.0
useSyncModeboolean是否使用同步模式2.18.0
requirementsstring[]需要开启的算法类型,详情见requirements 的合法值2.18.0
successfunction接口调用成功的回调函数2.18.0
failfunction接口调用失败的回调函数2.18.0
completefunction接口调用结束的回调函数(调用成功、失败都会执行)2.18.0

requirements 的合法值

说明最低支持版本
['skeleton']肢体识别算法对应字段2.18.0
['face106']面部 106 点算法对应字段2.18.0
['nail']指甲分割算法对应字段2.18.0
['foot']足部识别算法对应字段2.18.0
['trackingAr']追踪 AR 算法对应字段2.26.0

回调成功

object 类型,属性如下:

属性类型说明最低支持版本
algorithmManagerAlgorithmManager获取的算法管理器2.18.0
errMsgstring"getAlgorithmManager:success"2.18.0

回调失败

object 类型,属性如下:

属性类型说明最低支持版本
errMsgstring"getAlgorithmManager: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,
    });
  },
});

AlgorithmModule.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: 传入参数 requirements 目前仅支持单个元素数组
  • Tip: 非白名单合作用户仅支持 IDE 中「扫码体验」使用算法能力,不可在线上使用算法能力,合作事宜参见算法能力简介
点击纠错
评价此篇文档