淘先锋技术网

首页 1 2 3 4 5 6 7

一、登录腾讯云、开通语音识别接口、获取密钥  登录 - 腾讯云 (tencent.com)

二、打开 API Explorer 语音识别 录音文件识别请求 - API 文档 - 文档中心 - 腾讯云

选择 录音文件请求

三、打开参数说明,按需求填入参数。

 其中Data输入的的是音频文件经过base64编码后得到的string 代码如下

import base64

file1 = open("16k.pcm","rb").read()   # 读取二进制文件
text = base64.b64encode(file1)   # 进行编码
print(text)

将输出结果复制到Data获得TaskId

四、将TaskId复制到结果查询窗口的TaskId输入栏中,

​ 

 识别结果

 

 动态界面调试的成功率极低因为复制过程中可能会导致base64编码的文本产生错误,这个仅作为调试。所以上面操作有些是多余的。下面正文开始

填好必要的参数Data先随便填入一个字符后然后复制代码

 拷入编译器中

 然后再安装tencentcloud-sdk-python

在修改一下复制在编译器中的代码,免去复制base64编码的步骤

输入端

import json
import base64
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

#编码
path1=r'文件路径'
file1 = open(path1,"rb").read()   # 读取二进制文件
text = base64.b64encode(file1).decode("utf-8")


try:
    cred = credential.Credential("密钥id", "密钥key")
    httpProfile = HttpProfile()
    httpProfile.endpoint = "asr.tencentcloudapi.com"

    clientProfile = ClientProfile()
    clientProfile.httpProfile = httpProfile
    client = asr_client.AsrClient(cred, "", clientProfile)

    req = models.CreateRecTaskRequest()
    params = {
        "EngineModelType": "16k_zh_video",
        "ChannelNum": 1,
        "ResTextFormat": 0,
        "SourceType": 1,
        "Data":text
    }
    
    req.from_json_string(json.dumps(params))

    resp = client.CreateRecTask(req)
  
    print(resp.to_json_string())#输出为字符串用eval()可转为字典

except TencentCloudSDKException as err:
    print(err)

输出端

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
try:
    cred = credential.Credential("密钥ID", "密钥key")
    httpProfile = HttpProfile()
    httpProfile.endpoint = "asr.tencentcloudapi.com"

    clientProfile = ClientProfile()
    clientProfile.httpProfile = httpProfile
    client = asr_client.AsrClient(cred, "", clientProfile)

    req = models.DescribeTaskStatusRequest()
    params = {
        "TaskId": 1945514777 #TaskId号
    }
    req.from_json_string(json.dumps(params))

    resp = client.DescribeTaskStatus(req)
    print(resp.to_json_string())

except TencentCloudSDKException as err:
    print(err)