腾讯本地语音上传的一句话识别

先安装调用所需的Python依赖包:pip install tencentcloud-sdk-python

# -*- coding: utf-8 -*-
"""
@author: Looking
@email: [email protected]
"""
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
import base64

# 通过本地语音上传方式调用
try:

    # 重要:<Your SecretId>、<Your SecretKey>需要替换成用户自己的账号信息
    # 请参考接口说明中的使用步骤1进行获取。
    cred = credential.Credential("Your SecretId", "Your SecretKey")
    httpProfile = HttpProfile()
    httpProfile.endpoint = "asr.tencentcloudapi.com"
    clientProfile = ClientProfile()
    clientProfile.httpProfile = httpProfile
    clientProfile.signMethod = "TC3-HMAC-SHA256"
    client = asr_client.AsrClient(cred, "ap-shanghai", clientProfile)

    # 读取文件以及base64
    with open("./output.wav", 'rb') as f:
        data = f.read()
    base64Wav = base64.b64encode(data).decode()

    # 发送请求
    req = models.SentenceRecognitionRequest()
    params = {"ProjectId": 0, "SubServiceType": 2, "EngSerViceType": "8k", "SourceType": 1, "Url": "",
              "VoiceFormat": "wav", "UsrAudioKey": "session-123", "Data": base64Wav}
    req._deserialize(params)
    resp = client.SentenceRecognition(req)
    print(resp.to_json_string())
    # windows系统使用下面一行替换上面一行
    # print(resp.to_json_string().decode('UTF-8').encode('GBK') )
except TencentCloudSDKException as err:
    print(err)

运行结果输出为:

猜你喜欢

转载自blog.csdn.net/TomorrowAndTuture/article/details/104751677