腾讯云asr一句话识别python模块的实现

在参阅了腾讯云官方的文档数千遍,以及在腾讯云官方工作人员的支持下,这个模块终于完成。

不得不说,他们的技术文档还是有一点坑的,这个模块就是拿来避坑的哈哈。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import base64
import json
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
# 语音识别api,结果自动去奇怪字符并转小写
#fileplace:wav文件的地址,如"D://test.wav"<code>
def Tencentcloud_asr(fileplace):
    try:
        with open(fileplace, 'rb') as f:
            data = str(base64.b64encode(f.read()))
            data = data.replace("\n", "")
            data = data.replace("\r", "")
            data = data&#91;2:]
            data = data&#91;:-1]
            UsrAudioKey=str(hash(data))
        datalen=len(base64.b64decode(data))
        f.close()
        cred = credential.Credential("这里填你的SecretId", "这里填你的SecretKey")
        httpProfile = HttpProfile()
        httpProfile.endpoint = "asr.tencentcloudapi.com"
        clientProfile = ClientProfile()
        clientProfile.httpProfile = httpProfile
        client = asr_client.AsrClient(cred, "", clientProfile)
        req = models.SentenceRecognitionRequest()
        params = {
            "SubServiceType": 2,
            "EngSerViceType": "16k_ca",
            "SourceType": 1,
            "VoiceFormat": "wav",
            "Data": data,
            "DataLen": datalen,
            "ProjectId": 0,
            "UsrAudioKey": UsrAudioKey,
        }
        req.from_json_string(json.dumps(params))
        resp = client.SentenceRecognition(req)
        结果=resp.to_json_string()
    except TencentCloudSDKException as err:
        print("语音识别api报错:"+err)
    结果=json.loads(结果)
    return str.lower(结果&#91;'Result'].replace(" ","").replace("。",""))

发布者:admin

www.wumouren.xyz站长

留下评论

您的电子邮箱地址不会被公开。 必填项已用*标注