tt.getAlgorithmManager
基础库 2.18.0 开始支持本方法,低版本需做兼容处理,这是一个异步方法。
获取传入字段对应的算法管理器。非白名单合作用户仅支持 IDE 中「扫码体验」使用算法能力,不可在线上使用算法能力,合作事宜参见算法能力简介。
语法
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 | AlgorithmManager | 获取的算法管理器 | 2.18.0 |
errMsg | string | "getAlgorithmManager:success" | 2.18.0 |
回调失败
object 类型,属性如下:
属性名 | 类型 | 说明 | 最低支持版本 |
---|---|---|---|
errMsg | string | "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 目前仅支持单个元素数组。
点击纠错
该文档是否对你的开发有所帮助?
有帮助
没帮助
该文档是否对你的开发有所帮助?
有帮助
没帮助