抖音开放平台Logo
开发者文档
“/”唤起搜索
控制台
  • JS API 列表
  • 基础
  • TTML
  • 网络
  • 媒体
  • 地图
  • 文件
  • 数据缓存
  • 地理位置
  • 设备
  • 画布
  • 界面
  • 页面导航
  • 开放接口
  • 登录
  • 用户信息
  • 广告
  • 营销能力
  • 支付
  • 分享
  • 小程序跳转
  • 抖音视频能力
  • 收货地址
  • 设置
  • 授权
  • 评价能力
  • 数据分析
  • 视频拍摄器
  • 引导关注
  • 订阅消息
  • 电商插件能力(即将废弃)
  • 隐私信息授权
  • web化
  • 转发和挂载
  • 侧边栏能力
  • 直播能力
  • AI/AR能力
  • AI/AR 能力简介
  • 算法管理器方案
  • 原生神经网络方案
  • 原生神经网络方案简介
  • tt.createBytennEngineContext
  • BytennEngineContext
  • BytennEngine
  • 特效贴纸方案
  • 安全能力
  • 行业开放
  • 第三方平台
  • 其它
  • 生活服务(即将废弃)
  • 基础库 2.29.0 开始支持本方法,这是一个同步方法。
    创建并返回 BytennEngineContext 神经网络管理器,可通过该对象加载 BytennEngine 神经网络引擎。
    目前该接口仅对白名单小程序开放,如需使用,开发者需要至控制台-小程序页面下,进入“能力-能力实验室-小程序能力”,申请原生神经网络能力。以上仅申请接口权限,无法满足使用条件;能力使用方式及付费问题,详情请联系火山引擎商务咨询。

    语法

    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

    返回值

    代码示例

    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 后端以获得最佳性能;
    ​