tt.getAlgorithmManager
收藏
我的收藏

基础库 2.18.0 开始支持本方法,低版本需做兼容处理,这是一个异步方法。​
获取传入字段对应的算法管理器。目前该接口仅对白名单小程序开放,如需使用,开发者需要至控制台-小程序页面下,进入“能力-能力实验室-小程序能力”,申请AI算法管理器能力。以上仅申请接口权限,无法满足使用条件;能力使用方式及付费问题,详情请联系火山引擎商务咨询。​

语法​

text
复制
tt.getAlgorithmManager(options)

参数说明​

options 为 object 类型,属性如下:​
属性名​
类型​
默认值​
必填​
说明​
最低支持版本​
width​
number​
是​
输入图像的宽度​
2.18.0​
height​
number​
是​
输入图像的高度​
2.18.0​
useSyncMode​
boolean​
是​
是否使用同步模式​
2.18.0​
requirements​
string[]​
是​
需要开启的算法类型,详情见 requirements 的合法值
2.18.0​
success​
function​
否​
接口调用成功的回调函数​
2.18.0​
fail​
function​
否​
接口调用失败的回调函数​
2.18.0​
complete​
function​
否​
接口调用结束的回调函数(调用成功、失败都会执行)​
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 类型,属性如下:​
属性名​
类型​
说明​
最低支持版本​
algorithmManager​
获取的算法管理器​
2.18.0​
errMsg​
string​
"getAlgorithmManager:success"​
2.18.0​

回调失败​

object 类型,属性如下:​
属性名​
类型​
说明​
最低支持版本​
errMsg​
string​
"getAlgorithmManager:fail " + 详细错误信息​
2.18.0​

代码示例​

index.js​
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​
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 目前仅支持单个元素数组。​