python 百度语音识别API 和腾讯云识别API

技术 · 2023-03-02 · 36 人浏览

python 百度语音识别API 和腾讯云识别API

百度语音识别API

  1. 需要先去注册百度语音识别获取到个人的APP_IDAPI_KEYSECRET_KEY
  2. 安装百度API包
    pip install baidu-aip
  3. 样例代码
from aip import AipSpeech
APP_ID = '********'
    API_KEY = '************'
    SECRET_KEY = '****************'
    client = AipSpeech(APP_ID, API_KEY, SECRET_KEY)

    # 识别本地文件
    res = client.asr(get_file_content("文件路径"), 'wav', 16000, {
        'dev_pid': 1537,  # 默认1537(普通话 输入法模型)
    })
    print(res.get("result")[0])

说明

formatstring必填语音文件的格式,pcm/wav/amr/m4a。不区分大小写。推荐pcm文件
rateint必填采样率,16000、8000,固定值

使用腾讯语音识别API

  1. 需要去腾讯云注册并获取需要的SecretIdSecretKey
  2. 安装腾讯pythonSDK
    pip install --upgrade tencentcloud-sdk-python
  3. 样例代码
import json
from tencentcloud.common import credential
from tencentcloud.common.profile.client_profile import ClientProfile
from tencentcloud.common.profile.http_profile import HttpProfile
from tencentcloud.common.exception.tencent_cloud_sdk_exception import TencentCloudSDKException
from tencentcloud.asr.v20190614 import asr_client, models

file = open("./2.wav", "rb").read()  # 读取二进制文件
    base64_string = base64.b64encode(file).decode("utf-8")

    file_size = os.path.getsize('音频路径') # 获取音频字节长度

    SecretId="****************************"
    SecretKey="*************************"
    cred = credential.Credential(SecretId, SecretKey)
    # 实例化一个http选项,可选的,没有特殊需求可以跳过
    httpProfile = HttpProfile()
    httpProfile.endpoint = "asr.tencentcloudapi.com"

    # 实例化一个client选项,可选的,没有特殊需求可以跳过
    clientProfile = ClientProfile()
    clientProfile.httpProfile = httpProfile
    # 实例化要请求产品的client对象,clientProfile是可选的
    client = asr_client.AsrClient(cred, "", clientProfile)

    # 实例化一个请求对象,每个接口都会对应一个request对象
    req = models.SentenceRecognitionRequest()
    params = {
        "ProjectId": 0,
        "SubServiceType": 2,
        "EngSerViceType": "16k_zh",
        "SourceType": 1,
        "VoiceFormat": "wav",  
        "UsrAudioKey": "111",  
        "Data": base64_string,
        "DataLen": file_size
    }
    req.from_json_string(json.dumps(params))

    # 返回的resp是一个SentenceRecognitionResponse的实例,与请求对象对应
    resp = client.SentenceRecognition(req)
    # 输出json格式的字符串回包
    print(resp.to_json_string())
    resp1 = json.loads(resp.to_json_string())
    print(resp1['Result'])

说明

参数名称必选类型描述
ActionString公共参数,本接口取值:SentenceRecognition。
VersionString公共参数,本接口取值:2019-06-14。
RegionString公共参数,本接口不需要传递此参数。
ProjectIdInteger腾讯云项目 ID,废弃参数,填写0即可。
SubServiceTypeInteger子服务类型。2: 一句话识别。
EngSerViceTypeString引擎模型类型。 电话场景: • 8k_en:电话 8k 英语; • 8k_zh:电话 8k 中文普通话通用; 非电话场景: • 16k_zh:16k 中文普通话通用; • 16k_en:16k 英语; • 16k_ca:16k 粤语; • 16k_ja:16k 日语; • 16k_zh_medical:16k 医疗; • 16k_zh-PY 中英粤; • 16k_zh_dialect:多方言,支持23种方言(上海话、四川话、武汉话、贵阳话、昆明话、西安话、郑州话、太原话、兰州话、银川话、西宁话、南京话、合肥话、南昌话、长沙话、苏州话、杭州话、济南话、天津话、石家庄话、黑龙江话、吉林话、辽宁话);
SourceTypeInteger语音数据来源。0:语音 URL;1:语音数据(post body)。
VoiceFormatString识别音频的音频格式,支持wav、pcm、ogg-opus、speex、silk、mp3、m4a、aac。
UsrAudioKeyString废弃参数,填写任意字符串即可。
UrlString语音的URL地址,需要公网环境浏览器可下载。当 SourceType 值为 0时须填写该字段,为 1 时不填。音频时长不能超过60s,音频文件大小不能超过3MB。
DataString语音数据,当SourceType 值为1(本地语音数据上传)时必须填写,当SourceType 值为0(语音 URL上传)可不写。要使用base64编码(采用python语言时注意读取文件应该为string而不是byte,以byte格式读取后要decode()。编码后的数据不可带有回车换行符)。音频时长不能超过60s,音频文件大小不能超过3MB(Base64后)。
DataLenInteger数据长度,单位为字节。当 SourceType 值为1(本地语音数据上传)时必须填写,当 SourceType 值为0(语音 URL上传)可不写(此数据长度为数据未进行base64编码时的数据长度)。
python 语音识别 百度语音识别 腾旭云语音识别
Theme Jasmine by Kent Liao