抖音开放平台Logo
开发者文档
“/”唤起搜索
控制台
  • JS API 列表
  • 基础
  • TTML
  • 网络
  • 媒体
  • 地图
  • 文件
  • 数据缓存
  • 地理位置
  • 设备
  • 画布
  • 界面
  • 页面导航
  • 开放接口
  • 登录
  • 用户信息
  • 广告
  • 营销能力
  • 支付
  • 分享
  • 小程序跳转
  • 抖音视频能力
  • 收货地址
  • 设置
  • 授权
  • 评价能力
  • 数据分析
  • 视频拍摄器
  • 引导关注
  • 订阅消息
  • 电商插件能力(即将废弃)
  • 隐私信息授权
  • web化
  • 转发和挂载
  • 侧边栏能力
  • 直播能力
  • AI/AR能力
  • AI/AR 能力简介
  • 算法管理器方案
  • 原生神经网络方案
  • 原生神经网络方案简介
  • tt.createBytennEngineContext
  • BytennEngineContext
  • BytennEngine
  • BytennEngine
  • BytennEngine.inferSync
  • 特效贴纸方案
  • 安全能力
  • 行业开放
  • 第三方平台
  • 其它
  • 生活服务(即将废弃)
  • 基础库 2.29.0 开始支持本方法,低版本需做兼容处理,这是一个同步方法。
    使用神经网络对输入数据进行推理。

    语法

    BytennEngine.inferSync(options)

    参数说明

    options

    object 类型,属性如下:
    属性名
    类型
    默认值
    必填
    说明
    最低支持版本
    dataConfig
    ​
    是
    输入数据及配置信息
    2.29.0
    convertConfig
    ​
    是
    从输入数据到神经网络输入的转换配置
    2.29.0
    success
    function
    ​
    否
    推理成功回调,返回推理结果
    2.29.0
    fail
    function
    ​
    否
    推理失败回调,返回错误原因
    2.29.0
    complete
    function
    ​
    否
    推理完成回调,不论成功失败都会触发
    2.29.0

    dataConfig 结构

    属性
    类型
    说明
    data
    ArrayBuffer
    输入源数据
    width
    number
    输入数据的宽度
    height
    number
    输入数据的高度
    channel
    number
    输入数据的通道数
    batch
    number
    输入数据的包数,不填默认为 1
    dataType
    string
    数据类型,类型定义见 dataType 的合法值
    dataFormat
    string
    数据组织结构,类型定义见 dataFormat 的合法值
    dataInputMode
    string
    数据传入模式(可选),即 data 要作为一个整体传给一个 tensor(常用于图像识别等场景),还是拆开作为 n 个 tensor。不传时默认处理方式为前者。

    dataType 的合法值

    值
    说明
    最低支持版本
    'U8'
    8 位无符号整数
    2.29.0
    'I8'
    8 位有符号整数
    2.29.0
    'U16'
    16 位无符号整数
    2.29.0
    'I16'
    16 位有符号整数
    2.29.0
    'F16'
    16 位浮点数
    2.29.0
    'F32'
    32 位浮点数
    2.29.0
    'F64'
    64 位浮点数
    2.29.0

    dataFormat 的合法值

    值
    说明
    最低支持版本
    'NCHW'
    NCHW 数据格式
    2.29.0
    'NHWC'
    NHWC 数据格式
    2.29.0

    dataInputMode 的合法值

    值
    说明
    最低支持版本
    default
    不传 dataInputMode 即为此默认值。data 将作为一个整体传给一个 tensor(适用于图像识别等场景)
    2.29.0
    singleValuePerTensor
    data 将拆开作为多个 tensor
    3.85.0

    SingleChannelConvertConfig 结构

    属性
    类型
    说明
    outputChannel
    number
    对应的输出通道
    normalizeFactor
    number
    归一化因子
    offset
    number
    偏置因子,不填默认为 0
    inputConfig
    对应的输入通道配置

    InputConfig 结构

    属性
    类型
    说明
    inputChannel
    number
    输入通道
    weight
    number
    当前选中输入通道的权重

    返回值

    object 类型,具体内容与神经网络类型有关。

    扫码体验

    无,请点击代码示例进行预览。

    代码示例

    const app = getApp(); Page({ data: { item_class: "unknow", prob: 0, }, onLoad: function () { console.log("Welcome to Mini Code"); if (this.offscreenCanvas == null) { this.offscreenCanvas = tt.createOffscreenCanvas(); } this.utils = require("./utils"); }, createBytennEngine: function () { console.log("on create clicked"); const engineCtx = tt.createBytennEngineContext("littleapp_mobilenet"); console.log("engine context is: ", engineCtx); engineCtx.onLoad((engine) => { console.log("load result: ", 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) => { const inferResult = this.engine.inferSync({ dataConfig: { data: res.data, width: res.width, height: res.height, channel: 4, dataType: "U8", dataFormat: "NCHW", }, 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, }, ], }, ], }); const embedding = new Float32Array(inferResult.data); const topK = this.utils.topK(embedding, this.utils.IMAGENET_CLASSES, 1); const { className, probability } = topK[0]; this.setData({ item_class: className, prob: probability, }); }); this.listener.start(); }, });
    // 使用dataInputMode的示例代码 const inferResult = this.engine.inferSync({ dataConfig: { data: new Float32Array([83, 0, 104, 24, 8, 5, 5, 17, 4, 1, 12, 10, 13, 11, 6, 406, 6, 0, 11, 3, 16, 86, 44]).buffer, width: 23, height: 1, channel: 1, batch: 1, dataType: "F32", dataFormat: "NHWC", // 可选字段,默认不写就走老的解析方式,把data作为一个整体传给一个tensor dataInputMode: "singleValuePerTensor" }, convertConfig: [{ outputChannel: 1, normalizeFactor: 1, offset: 0, inputConfig: [{ inputChannel: 22, weight: 1 }] }] });

    Bug & Tip

      •Tip:输入参数会被校验,但仅会对数据大小进行校验,不会校验图片完整性。
      •Tip:不同神经网络需要的输入格式不同,需按照网络结构传入 dataConfig、convertConfig。
    ​