先安裝調用所需的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)
運行結果輸出爲: