语音交互服务 SIS-一句话识别Http接口:代码示例
时间:2023-11-20 11:21:53
代码示例
如下示例仅供参考,最新代码请前往获取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 = ""; // 区域,如cn-north-1、cn-north-4 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"); // 设置是否将语音中的数字转写为阿拉伯数字,yes或no,默认yes request.setDigitNorm("no"); } /** * 定义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(); } }
support.huaweicloud.com/sdkreference-sis/sis_05_0044.html
看了此文的人还看了
CDN加速
GaussDB
文字转换成语音
免费的服务器
如何创建网站
域名网站购买
私有云桌面
云主机哪个好
域名怎么备案
手机云电脑
SSL证书申请
云点播服务器
免费OCR是什么
电脑云桌面
域名备案怎么弄
语音转文字
文字图片识别
云桌面是什么
网址安全检测
网站建设搭建
国外CDN加速
SSL免费证书申请
短信批量发送
图片OCR识别
云数据库MySQL
个人域名购买
录音转文字
扫描图片识别文字
OCR图片识别
行驶证识别
虚拟电话号码
电话呼叫中心软件
怎么制作一个网站
Email注册网站
华为VNC
图像文字识别
企业网站制作
个人网站搭建
华为云计算
免费租用云托管
云桌面云服务器
ocr文字识别免费版
HTTPS证书申请
图片文字识别转换
国外域名注册商
使用免费虚拟主机
云电脑主机多少钱
鲲鹏云手机
短信验证码平台
OCR图片文字识别
SSL证书是什么
申请企业邮箱步骤
免费的企业用邮箱
云免流搭建教程
域名价格
推荐文章