TencentCloud / tencentcloud-sdk-python

Tencent Cloud API 3.0 SDK for Python
Apache License 2.0
613 stars 251 forks source link

调用语音识别 api的时候遇到问题 #74

Closed huitang closed 4 years ago

huitang commented 4 years ago

直接是官网的例子 fwave = open('./data/0.wav', mode='r')# 这一句报错 data = str(fwave.read()) dataLen = len(data) base64Wav = base64.b64encode(data)

'utf-8' codec can't decode byte 0xac in position 25: invalid start byte

zqfan commented 4 years ago

麻烦发一下文档链接, 读取文件转base64可以参考测试用例中的https://github.com/TencentCloud/tencentcloud-sdk-python/blob/master/tests/integration/ocr/test_general_accurate_ocr.py

huitang commented 4 years ago

-- coding: utf-8 --

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
fwave = open('./test.wav', mode='r')
data = str(fwave.read())
dataLen = len(data)
base64Wav = base64.b64encode(data)

#发送请求
req = models.SentenceRecognitionRequest()
params = {"ProjectId":0,"SubServiceType":2,"EngSerViceType":"16k","SourceType":1,"Url":"","VoiceFormat":"wav","UsrAudioKey":"session-123", "Data":base64Wav, "DataLen":dataLen}
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)

我按照官网的例子中报错的

huitang commented 4 years ago

麻烦发一下文档链接, 读取文件转base64可以参考测试用例中的https://github.com/TencentCloud/tencentcloud-sdk-python/blob/master/tests/integration/ocr/test_general_accurate_ocr.py

是语音识别 不是ocr 识别哈

zqfan commented 4 years ago

@huitang 麻烦给一下官网的链接,我需要联系写这个文档的同事修改下。 跟哪个产品哪个接口无关,读取二进制文件转base64是python语言中的问题。

huitang commented 4 years ago

https://cloud.tencent.com/document/sdk/Python 我直接跑的第二个例子 就是本地的语音 多谢了

On Thu, Jul 9, 2020 at 11:27 PM ZhiQiang Fan notifications@github.com wrote:

@huitang https://github.com/huitang 麻烦给一下官网的链接,我需要联系写这个文档的同事修改下。 跟哪个产品哪个接口无关,读取二进制文件转base64是python语言中的问题。

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/TencentCloud/tencentcloud-sdk-python/issues/74#issuecomment-656507526, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAOPKZHN233U3O7M67PVCT3R22YGJANCNFSM4OVV7QMQ .

zqfan commented 4 years ago

@huitang 这个页面只有一个DescribeZones的例子呀,并没有读取文件的示例吧

huitang commented 4 years ago

b不好意思 是我发错了链接 我用的例子在这里 第二个不work

https://cloud.tencent.com/document/product/1093/35734

On Sun, Jul 12, 2020 at 5:31 AM ZhiQiang Fan notifications@github.com wrote:

@huitang https://github.com/huitang 这个页面只有一个DescribeZones的例子呀,并没有读取文件的示例吧

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/TencentCloud/tencentcloud-sdk-python/issues/74#issuecomment-657215670, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAOPKZEJEBP3XCFRVFOGODLR3GUKDANCNFSM4OVV7QMQ .