更新时间:2024-05-08 GMT+08:00
分享

录音文件极速版

前提条件

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

初始化Client

初始化FlashLasrClient详见表 FlashLasrClient初始化参数

表1 FlashLasrClient初始化参数

参数名称

是否必选

参数类型

描述

ak

String

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

sk

String

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

region

String

区域,如cn-north-4,参考终端节点

project_id

String

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

service_endpoint

String

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

sis_config

Object

详见表2

表2 SisConfig

参数名称

是否必选

参数类型

描述

connect_timeout

Integer

连接超时,默认10,单位s。

read_timeout

Integer

读取超时,默认10,单位s。

proxy

List

[host, port] 或 [host, port, username, password]。

请求参数

请求类为FlashLasrRequest,详见表3

表3 FlashLasrRequest

参数

是否必选

参数类型

描述

audio_format

String

支持语音的格式,请参考表 audio_format取值范围

property

String

所使用的模型特征串,通常是 “语种_采样率_领域”的形式,采样率需要与音频采样率保持一致,取值范围请参考表 property取值范围

add_punc

String

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

digit_norm

String

表示是否将语音中的数字识别为阿拉伯数字,取值为“yes”“no”,默认为“yes”

vocabulary_id

String

热词表id,不使用则不填写。

创建热词表信息请参考创建热词表

need_word_info

String

表示是否在识别结果中输出分词结果信息,取值为“yes”“no”,默认为“no”

first_channel_only

String

表示是否在识别中只识别首个声道的音频数据,取值为“yes”“no”,默认为“no”

obs_bucket_name

String

表示在OBS对象桶名,使用前请先授权,操作方法请参见配置OBS访问权限。obs_bucket_name长度大于等于3个字符,小于64个字符,不需要进行urlencode编码,如果包含中文,直接输入中文即可。

示例

obs url为https://test.obs.cn-north-4.myhuaweicloud.com/data/0601/test.wav

则obs_bucket_name=test,obs_bucket_key=data/0601/test.wav

obs_object_key

String

表示OBS对象桶中的对象的键值,长度小于1024个字符,不需要进行urlencode编码,如果包含中文,直接输入中文即可。

示例

obs url为https://test.obs.cn-north-4.myhuaweicloud.com/data/0601/test.wav

则obs_bucket_name=test,obs_bucket_key=data/0601/test.wav

表4 audio_format取值范围

audio_format取值

描述

wav

wav格式音频

mp3

mp3格式音频

m4a

m4a格式音频

aac

aac格式音频

opus

ops格式音频。

表5 property取值范围

property取值

描述

chinese_8k_common

支持采样率为8k的中文普通话语音识别。

chinese_16k_conversation

支持采样率为16k的会议场景的中文普通话语音识别。

响应参数

响应类为FlashLasrResponse, 详见表6。调用失败处理方法请参见错误码

表6 FlashLasrResponse

参数

是否必选

参数类型

描述

trace_id

String

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

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

audio_duration

Integer

音频时长,单位毫秒

flash_result

Array of FlashResult objects

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

表7 FlashResult

参数

是否必选

参数类型

描述

channel_id

Integer

声道Id

sentences

Array of Sentences objects

分句信息列表

表8 Sentences

参数

是否必选

参数类型

描述

start_time

Integer

一句话开始时间,单位毫秒

result

Result object

分句结果信息

end_time

Integer

一句话结束时间,单位毫秒

表9 Result

参数

是否必选

参数类型

描述

text

String

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

score

Double

调用成功表示识别出的置信度(0-1之间)。

word_info

Array of WordInfo objects

分词信息列表

表10 WordInfo

参数

是否必选

参数类型

描述

start_time

Integer

起始时间

end_time

Integer

结束时间

word

String

分词

代码示例

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

# -*- coding: utf-8 -*-
from huaweicloud_sis.client.flash_lasr_client import FlashLasrClient
from huaweicloud_sis.bean.flash_lasr_request import FlashLasrRequest
from huaweicloud_sis.exception.exceptions import ClientException
from huaweicloud_sis.exception.exceptions import ServerException
from huaweicloud_sis.bean.sis_config import SisConfig
import json
import os
# 鉴权参数
# 认证用的ak和sk硬编码到代码中或者明文存储都有很大的安全风险,建议在配置文件或者环境变量中密文存放,使用时解密,确保安全;
# 本示例以ak和sk保存在环境变量中来实现身份验证为例,运行本示例前请先在本地环境中设置环境变量HUAWEICLOUD_SIS_AK/HUAWEICLOUD_SIS_SK
ak = os.getenv("HUAWEICLOUD_SIS_AK")             # 从环境变量获取ak 参考https://support.huaweicloud.com/sdkreference-sis/sis_05_0003.html
assert ak is not None, "Please add ak in your develop environment"
sk = os.getenv("HUAWEICLOUD_SIS_SK")             # 从环境变量获取sk 参考https://support.huaweicloud.com/sdkreference-sis/sis_05_0003.html
assert sk is not None, "Please add sk in your develop environment"
project_id = ""     # project id 同region一一对应,参考https://support.huaweicloud.com/api-sis/sis_03_0008.html
region = ''         # region,如cn-north-4
obs_bucket_name = ''    # obs桶名
obs_object_key = ''     # obs对象的key
audio_format = ''       # 文件格式,如wav等, 支持格式详见api文档
property = ''           # 属性字符串,language_sampleRate_domain, 如chinese_8k_common, 详见api文档
def flash_lasr_example():
    """ 录音文件极速版示例 """
    # step1 初始化客户端
    config = SisConfig()
    config.set_connect_timeout(10)  # 设置连接超时
    config.set_read_timeout(10)  # 设置读取超时
    # 设置代理,使用代理前一定要确保代理可用。 代理格式可为[host, port] 或 [host, port, username, password]
    # config.set_proxy(proxy)
    client = FlashLasrClient(ak, sk, region, project_id, sis_config=config)
    # step2 构造请求
    asr_request = FlashLasrRequest()
    # 以下参数必选
    # 设置存放音频的桶名,必选
    asr_request.set_obs_bucket_name(obs_bucket_name)
    # 设置桶内音频对象名,必选
    asr_request.set_obs_object_key(obs_object_key)
    # 设置格式,必选
    asr_request.set_audio_format(audio_format)
    # 设置属性,必选
    asr_request.set_property(property)
    # 以下参数可选
    # 设置是否添加标点,yes or no,默认no
    asr_request.set_add_punc('yes')
    # 设置是否将语音中数字转写为阿拉伯数字,yes or no,默认yes
    asr_request.set_digit_norm('yes')
    # 设置是否添加热词表id,没有则不填
    # asr_request.set_vocabulary_id(None)
    # 设置是否需要word_info,yes or no, 默认no
    asr_request.set_need_word_info('no')
    # 设置是否只识别收个声道的音频数据,默认no
    asr_request.set_first_channel_only('no')
    # step3 发送请求,返回结果,返回结果为json格式
    result = client.get_flash_lasr_result(asr_request)
    # use enterprise_project_Id
    # headers = {'Enterprise-Project-Id': 'your enterprise project id', 'Content-Type': 'application/json'}
    # result = client.get_flash_lasr_result(asr_request, headers)
    print(json.dumps(result, indent=2, ensure_ascii=False))
if __name__ == '__main__':
    try:
        flash_lasr_example()
    except ClientException as e:
        print(e)
    except ServerException as e:
        print(e)
分享:

    相关文档

    相关产品