文档首页/ 语音交互服务 SIS/ SDK参考/ Java SDK/ 一句话识别Http接口
更新时间:2024-03-05 GMT+08:00

一句话识别Http接口

前提条件

  • 确保已按照配置Java环境配置完毕。
  • 确保已存在待识别的音频文件。如果需要请在下载的SDK压缩包中获取示例音频。

初始化Client

初始化AsrCustomizationClient,其参数包括AuthInfo和SisConfig。

表1 AuthInfo

参数名称

是否必选

参数类型

描述

ak

是

String

用户的ak,可参考AK/SK认证。

sk

是

String

用户的sk,可参考AK/SK认证。

region

是

String

区域,如ap-southeast-3,参考终端节点。

projectId

是

String

项目ID,同region一一对应,参考获取项目ID。

endpoint

否

String

终端节点,一般使用默认即可。

表2 SisConfig

参数名称

是否必选

参数类型

描述

connectionTimeout

否

Integer

连接超时,默认10000,单位ms。

readTimeout

否

Integer

读取超时,默认10000,单位ms。

请求参数

请求类为AsrCustomShortRequest,详见表3。

表3 AsrCustomShortRequest

参数名称

是否必选

参数类型

描述

data

是

String

本地音频文件经过Base64编码后的字符串,音频文件时长不超过30s。

audioFormat

是

String

音频格式,具体信息请参见《API参考》中一句话识别章节。

property

是

String

属性字符串,语言_采样率_模型,如 english_8k_common。具体信息请参见《API参考》中一句话识别章节。

addPunc

否

String

表示是否在识别结果中添加标点,取值为yes 、 no,默认no。

响应参数

响应类为AsrCustomShortResponse,详见表4。

表4 AsrCustomShortResponse

参数名

是否必选

参数类型

说明

trace_id

是

String

服务内部的令牌,可用于在日志中追溯具体流程,调用失败无此字段。

在某些错误情况下可能没有此令牌字符串。

result

是

Object

调用成功表示识别结果,调用失败时无此字段。请参考表5。

表5 Result

参数名

是否必选

参数类型

说明

text

是

String

调用成功表示识别出的内容。

score

是

Float

调用成功表示识别出的置信度,取值范围:0~1。

代码示例

如下示例仅供参考,最新代码请前往获取SDK章节获取并运行。

import com.huawei.sis.bean.SisConfig;
import com.huawei.sis.bean.SisConstant;
import com.huawei.sis.bean.request.AsrCustomShortRequest;
import com.huawei.sis.bean.response.AsrCustomShortResponse;
import com.huawei.sis.bean.AuthInfo;
import com.huawei.sis.client.AsrCustomizationClient;
import com.huawei.sis.exception.SisException;
import com.huawei.sis.util.IOUtils;
import java.util.List;
import com.huawei.sis.util.JsonUtils;


/**
 * 一句话识别
 *
 * Copyright 2021 Huawei Technologies Co.,Ltd.
 */
public class AsrCustomizationDemo {
  private static final int SLEEP_TIME = 500;
  private static final int MAX_POLLING_NUMS = 1000;

  // 认证用的ak和sk硬编码到代码中或者明文存储都有很大的安全风险,建议在配置文件或者环境变量中密文存放,使用时解密,确保安全;
  // 本示例以ak和sk保存在环境变量中来实现身份验证为例,运行本示例前请先在本地环境中设置环境变量HUAWEICLOUD_SDK_AK和HUAWEICLOUD_SDK_SK。
  private String ak = System.getenv("HUAWEICLOUD_SDK_AK");
  private String sk = System.getenv("HUAWEICLOUD_SDK_SK");
  

  private String region = "";    // 区域,如ap-southeast-3
  private String projectId = ""; // 项目id。登录管理控制台,鼠标移动到右上角的用户名上,在下拉列表中选择我的凭证,在项目列表中查看项目id。多项目时,展开“所属区域”,从“项目ID”列获取子项目ID。
  // 一句话识别参数
  private String path = "";             // 音频文件路径,如D:/test.wav等,sdk会将音频文件转化为base64编码
  private String pathAudioFormat = "";  // 文件格式,如wav等
  private String pathProperty = "chinese_16k_general";     // 属性字符串,language_sampleRate_domain, 16k模型推荐使用chinese_16k_general


  /**
   * 设置一句话识别参数,所有参数均有默认值,不配置也可使用
   *
   * @param request 一句话识别请求
   */
  private void setShortParameter(AsrCustomShortRequest request) {

    // 设置是否添加标点,默认是no
    request.setAddPunc("yes");
  }

  /**
   * 定义config,所有参数可选,设置超时时间等。
   *
   * @return SisConfig
   */
  private SisConfig getConfig() {
    SisConfig config = new SisConfig();
    // 设置连接超时,默认10000ms
    config.setConnectionTimeout(SisConstant.DEFAULT_CONNECTION_TIMEOUT);
    // 设置读取超时,默认10000ms
    config.setReadTimeout(SisConstant.DEFAULT_READ_TIMEOUT);
    // 设置代理, 一定要确保代理可用才启动此设置。 代理初始化也可用不加密的代理,new ProxyHostInfo(host, port);
    // ProxyHostInfo proxy = new ProxyHostInfo(host, port, username, password);
    // config.setProxy(proxy);
    return config;
  }


  /**
   * 一句话识别demo
   */
  private void shortDemo() {
    try {
      // 1. 初始化AsrCustomizationClient
      // 定义authInfo,根据ak,sk,region,projectId
      AuthInfo authInfo = new AuthInfo(ak, sk, region, projectId);
      // 设置config,主要与超时有关
      SisConfig config = getConfig();
      // 根据authInfo和config,构造AsrCustomizationClient
      AsrCustomizationClient asr = new AsrCustomizationClient(authInfo, config);

      // 2. 配置请求
      String data = IOUtils.getEncodeDataByPath(path);
      AsrCustomShortRequest request = new AsrCustomShortRequest(data, pathAudioFormat, pathProperty);
      // 设置请求参数,所有参数均为可选
      setShortParameter(request);

      // 3. 发送请求,获取响应
      AsrCustomShortResponse response = asr.getAsrShortResponse(request);
      // 打印结果
      System.out.println(JsonUtils.obj2Str(response, true));

    } catch (SisException e) {
      e.printStackTrace();
      System.out.println("error_code:" + e.getErrorCode() + "\nerror_msg:" + e.getErrorMsg());
    }
  }

  public static void main(String[] args) {
    AsrCustomizationDemo demo = new AsrCustomizationDemo();
    demo.shortDemo();
  }

}