环境依赖
安装 SDK
pip install --upgrade tencentcloud-sdk-python
pip install --upgrade tencentcloud-sdk-python-common tencentcloud-sdk-python-soe tencentcloud-sdk-python-sts
使用 SDK
本地音频文件评测
# -*- coding: utf-8 -*-# 发音数据传输接口附带初始化过程(https://cloud.tencent.com/document/product/884/32605)import jsonimport osimport uuidimport base64from tencentcloud.common import credentialfrom tencentcloud.common.exception.tencent_cloud_sdk_exception import TencentCloudSDKExceptionfrom tencentcloud.soe.v20180724 import soe_client, models# 实例化一个认证对象,入参需要传入腾讯云账户 SecretId 和 SecretKey,此处还需注意密钥对的保密# 代码泄露可能会导致 SecretId 和 SecretKey 泄露,并威胁账号下所有资源的安全性。以下代码示例仅供参考,建议采用更安全的方式来使用密钥,请参见:https://cloud.tencent.com/document/product/1278/85305# 密钥可前往官网控制台 https://console.cloud.tencent.com/cam/capi 进行获取cred = credential.Credential("SecretId", "SecretKey")# 实例化soe的client对象client = soe_client.SoeClient(cred, "")# 实例化发音数据传输接口请求对象req = models.TransmitOralProcessWithInitRequest()# 请求参数赋值SessionId = str(uuid.uuid1()) # 使用uuid作为请求SessionId# 读取音频文件,music_file = "SOEMUSIC/apple.mp3" # 音频文件地址with open(music_file, "rb") as f:base64_data = base64.b64encode(f.read()).decode() # 读取音频文件byte数据转成base64格式params = {"SeqId": 1,"IsEnd": 1,"VoiceFileType": 3,"VoiceEncodeType": 1,"UserVoiceData": base64_data,"SessionId": SessionId,"RefText": "{::cmd{F_TDET=true}}山","WorkMode": 1,"EvalMode": 1,"ServerType": 1,"ScoreCoeff": 1.0}req.from_json_string(json.dumps(params))try:# 通过client对象调用TransmitOralProcessWithInit方法发起请求resp = client.TransmitOralProcessWithInit(req)# 输出json格式的字符串回包print(resp.to_json_string())except TencentCloudSDKException as err:print(err)
评测超时处理
如果本地文件过大,可能会导致评测超时。可以选用如下方案:
1. 流式分片传输:将发音数据进行分片处理,减少每次评测的时间。
2. 异步查询:使用异步功能将发音数据先上传,然后使用查询功能查询结构。
from tencentcloud.common import credentialfrom tencentcloud.common.exception.tencent_cloud_sdk_exception import TencentCloudSDKExceptionfrom tencentcloud.soe.v20180724 import soe_client, modelsimport uuidimport jsonimport osimport base64import math# 实例化一个认证对象,入参需要传入腾讯云账户 SecretId 和 SecretKey,此处还需注意密钥对的保密# 代码泄露可能会导致 SecretId 和 SecretKey 泄露,并威胁账号下所有资源的安全性。以下代码示例仅供参考,建议采用更安全的方式来使用密钥,请参见:https://cloud.tencent.com/document/product/1278/85305# 密钥可前往官网控制台 https://console.cloud.tencent.com/cam/capi 进行获取cred = credential.Credential("SecretId", "SecretKey")# 实例化soe的client对象client = soe_client.SoeClient(cred, "")# 实例化发音数据传输接口请求对象req = models.TransmitOralProcessWithInitRequest()# 请求参数赋值SessionId = str(uuid.uuid1()) # 使用uuid作为请求SessionIdslice_num = 100 * 1024 # 分片大小, 1 * 1024即为1k# 读取音频文件,使用本地音频文件录音替换apple.mp3music_file = "SOEMUSIC/para.mp3"with open(music_file, "rb") as f:content = f.read()content_len = len(content)# 分片数量计算pkg_num = math.ceil(content_len / slice_num)# 开始分片并传输for j in range(int(pkg_num)):last_index = content_len if j + 1 == pkg_num else (j + 1) * slice_numIsEnd = 1 if j + 1 == pkg_num else 0send_content = content[j * slice_num:last_index]base64_data = base64.b64encode(send_content).decode()params = {"SeqId": j + 1,"IsEnd": IsEnd,"VoiceFileType": 3,"VoiceEncodeType": 1,"UserVoiceData": base64_data,"SessionId": SessionId,"RefText": "apple","WorkMode": 1,"EvalMode": 0,"ServerType": 0,"ScoreCoeff": 1.0}req.from_json_string(json.dumps(params))try:# 通过client对象调用TransmitOralProcessWithInit方法发起请求resp = client.TransmitOralProcessWithInit(req)# 输出json格式的字符串回包print(resp.to_json_string())except TencentCloudSDKException as err:print(err)break
# -*- coding: utf-8 -*-# 发音数据传输接口附带初始化过程(https://cloud.tencent.com/document/product/884/32605)import jsonimport osimport timeimport uuidimport base64from tencentcloud.common import credentialfrom tencentcloud.common.exception.tencent_cloud_sdk_exception import TencentCloudSDKExceptionfrom tencentcloud.soe.v20180724 import soe_client, models# 实例化一个认证对象,入参需要传入腾讯云账户 SecretId 和 SecretKey,此处还需注意密钥对的保密# 代码泄露可能会导致 SecretId 和 SecretKey 泄露,并威胁账号下所有资源的安全性。以下代码示例仅供参考,建议采用更安全的方式来使用密钥,请参见:https://cloud.tencent.com/document/product/1278/85305# 密钥可前往官网控制台 https://console.cloud.tencent.com/cam/capi 进行获取cred = credential.Credential("SecretId", "SecretKey")# 实例化soe的client对象client = soe_client.SoeClient(cred, "")# 实例化发音数据传输接口请求对象req = models.TransmitOralProcessWithInitRequest()# 请求参数赋值SessionId = str(uuid.uuid1()) # 使用uuid作为请求SessionId# 读取音频文件,music_file = "SOEMUSIC/para.mp3" # 音频文件地址with open(music_file, "rb") as f:base64_data = base64.b64encode(f.read()).decode() # 读取音频文件byte数据转成base64格式params = {"SeqId": 1,"IsEnd": 1,"VoiceFileType": 3,"VoiceEncodeType": 1,"UserVoiceData": base64_data,"SessionId": SessionId,"RefText": "apple","WorkMode": 1,"EvalMode": 2,"ServerType": 0,"ScoreCoeff": 1.0,"IsAsync": 1}req.from_json_string(json.dumps(params))# 异步查询for i in range(60):try:# 通过client对象调用TransmitOralProcess方法发起请求resp = client.TransmitOralProcessWithInit(req)# 输出json格式的字符串回包print(resp.to_json_string())json_resp = resp.to_json_string() # 获取结果status = json.loads(json_resp)["Status"] # 查看评测状态if status == "Finished": # 判断是否评测结束breakelse:params["IsQuery"] = 1 # 查询time.sleep(1)print("查询结果")except TencentCloudSDKException as err:print(err)break
使用方法说明
1,SessionID :使用 uuid 来作为 SessionID,用于区分不同的音频。
2,base64 转换:读取本地音频文件,转换成 base64 数据进行评测。
3,流式分片 base64 转换:读取本地音频文件,将音频进行分片,转换成 base64 数据进行评测。
4,轮询查询:使用 json 反序列化,获取评测结果,判断是否查询到最终结果。
import uuid #python自带SessionId = str(uuid.uuid1()) # 使用uuid作为请求SessionId
import base64 #python自带with open("apple.mp3", "rb") as f: #使用二进制读取音频文件apple.mp3,请自行替换成本地音频文件地址base64_data = base64.b64encode(f.read).decode() #音频byte数据转base64
with open(music_file, "rb") as f:content = f.read()content_len = len(content)# 分片数量计算pkg_num = math.ceil(content_len / slice_num)# 开始分片并传输for j in range(int(pkg_num)):last_index = content_len if j + 1 == pkg_num else (j + 1) * slice_numIsEnd = 1 if j + 1 == pkg_num else 0send_content = content[j * slice_num:last_index]base64_data = base64.b64encode(send_content).decode()
# 异步查询for i in range(60):try:# 通过client对象调用TransmitOralProcess方法发起请求resp = client.TransmitOralProcessWithInit(req)# 输出json格式的字符串回包print(resp.to_json_string())json_resp = resp.to_json_string() # 获取结果status = json.loads(json_resp)["Status"] # 查看评测状态if status == "Finished": # 判断是否评测结束breakelse:params["IsQuery"] = 1 # 查询time.sleep(1)print("查询结果")except TencentCloudSDKException as err:print(err)break
临时授权凭证
# -*- coding: utf-8 -*-# 获取联合身份临时访问凭证(https://cloud.tencent.com/document/product/1312/48195)import jsonimport osfrom tencentcloud.common import credentialfrom tencentcloud.common.exception.tencent_cloud_sdk_exception import TencentCloudSDKExceptionfrom tencentcloud.sts.v20180813 import sts_client, models# 实例化一个认证对象,入参需要传入腾讯云账户 SecretId 和 SecretKey,此处还需注意密钥对的保密# 代码泄露可能会导致 SecretId 和 SecretKey 泄露,并威胁账号下所有资源的安全性。以下代码示例仅供参考,建议采用更安全的方式来使用密钥,请参见:https://cloud.tencent.com/document/product/1278/85305# 密钥可前往官网控制台 https://console.cloud.tencent.com/cam/capi 进行获取cred = credential.Credential("SecretId", "SecretKey")# 实例化sts的client对象client = sts_client.StsClient(cred, "ap-beijing")# 实例化获取联合身份临时访问凭证请求对象req = models.GetFederationTokenRequest()# 请求参数赋值# soe:TransmitOralProcessWithInit 最小权限原则params = {"Name": "soe","Policy": "{\\"version\\": \\"2.0\\",\\"statement\\": {\\"effect\\": \\"allow\\", \\"action\\":[\\"soe:TransmitOralProcessWithInit\\"],\\"resource\\": \\"*\\"}}"}req.from_json_string(json.dumps(params))try:# 通过client对象调用GetFederationToken方法发起请求resp = client.GetFederationToken(req)# 输出json格式的字符串回包print(resp.to_json_string())except TencentCloudSDKException as err:print(err)