python音频转文字腾讯_腾讯语音识别python3

腾讯的sdk文档是真的坑,官方给的例子都能跑不动的。安卓语音识别的sdk居然不给源码,反编译了改了参数才生效。

下面是python的语音识别例子,在官方基础上修复了一下,需要安装官方sdk

# -*- coding:utf-8 -*-

import json

from urllib import request

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:

#重要:、需要替换成用户自己的账号信息

#请参考接口说明中的使用步骤1进行获取。

secret_key = ''

secretid = ''

appid = 'yourappid'

cred = credential.Credential("yourSecretId", "yourSecretkey")

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='rb').read()

dataLen = len(fwave)

base64Wav = base64.b64encode(fwave).decode('utf8')

print(base64Wav)

#发送请求

req = models.SentenceRecognitionRequest()

params = {"ProjectId":0,"SubServiceType":2,"EngSerViceType":"16k_en","SourceType":1,"Url":"","VoiceFormat":"wav","UsrAudioKey":"session-123", "Data":base64Wav, "DataLen":dataLen}

req._deserialize(params)

resp = client.SentenceRecognition(req)

print(resp)

print(resp.to_json_string())

#windows系统使用下面一行替换上面一行

# print(resp.to_json_string().decode('UTF-8').encode('GBK') )

except TencentCloudSDKException as err:

print(err)

你可能感兴趣的:(python音频转文字腾讯)