- JS API 列表
- 基础
- 网络
- 媒体
- 地图
- 文件
- 开放接口
- 数据缓存
- 地理位置
- 设备
- 画布
- 界面
- 页面导航
- 侧边栏能力
- 行业开放
- AI/AR能力
- 第三方平台
- TTML
- 转发和挂载
- 其它
- 直播能力
tt.createBytennEngineContext收藏我的收藏
收藏
我的收藏基础库 2.29.0 开始支持本方法,这是一个同步方法。
目前该接口仅对白名单小程序开放,如需使用,开发者需要至控制台-小程序页面下,进入“能力-能力实验室-小程序能力”,申请原生神经网络能力。以上仅申请接口权限,无法满足使用条件;能力使用方式及付费问题,详情请联系火山引擎商务咨询。
语法
text复制tt.createBytennEngineContext(modelName, engineConfig)
参数说明
modelName
类型 | 默认值 | 必填 | 说明 | 最低支持版本 |
string | | 是 | 需要加载的神经 网络名称 | 2.29.0 |
engineConfig
类型 | 默认值 | 必填 | 说明 | 最低支持版本 |
object | | 否 | 加载网络的配置 | 2.29.0 |
engineConfig 的结构
属性 | 类型 | 说明 |
numThread | number | 工作线程数,不填默认为 1 |
backend | string | 神经网络推理使用的后端,不填默认为 auto ,下列为合法值 |
backend 的合法值
值 | 说明 | 最低支持版本 |
auto | 自动选择后端 | 2.29.0 |
cpu | 使用 CPU 后端 | 2.29.0 |
gpu | 使用 gpu 后端 | 2.29.0 |
core_ml | 使用 CoreML 后端(iOS) | 2.67.0 |
返回值
代码示例
js复制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: 推荐使用 auto 后端以获得最佳性能;
点击纠错