语音听写,可以将60s以内的音频文件转换为文字
1.进入“讯飞开放平台”,创建“WebAPI”平台的应用
添加服务
把你的IP添加白名单,就可以进行测试了
Demo
# -*- coding: utf-8 -*-
import requests
import time
import hashlib
import base64
import json
URL = "http://api.xfyun.cn/v1/service/v1/iat"
APPID = "******"
API_KEY = "******"
def getHeader(aue, engineType):
curTime = str(int(time.time()))
# curTime = '1526542623'
param = "{\"aue\":\"" + aue + "\"" + ",\"engine_type\":\"" + engineType + "\"}"
print("param:{}".format(param))
paramBase64 = str(base64.b64encode(param.encode('utf-8')), 'utf-8')
print("x_param:{}".format(paramBase64))
m2 = hashlib.md5()
m2.update((API_KEY + curTime + paramBase64).encode('utf-8'))
checkSum = m2.hexdigest()
print('checkSum:{}'.format(checkSum))
header = {
'X-CurTime': curTime,
'X-Param': paramBase64,
'X-Appid': APPID,
'X-CheckSum': checkSum,
'Content-Type': 'application/x-www-form-urlencoded; charset=utf-8',
}
print(header)
return header
def getBody(filepath):
binfile = open(filepath, 'rb')
data = {'audio': base64.b64encode(binfile.read())}
print(data)
print('data:{}'.format(type(data['audio'])))
# print("type(data['audio']):{}".format(type(data['audio'])))
return data
aue = "raw"
engineType = "sms16k"
audioFilePath = r"test.pcm"
r = requests.post(URL, headers=getHeader(aue, engineType), data=getBody(audioFilePath))
r2 = r.content.decode('utf-8')
dictinfo = json.loads(r2)
#得到语音转换为文字的数据
dictinfo["data"]
print(dictinfo["data"])
讯飞的转换结果有标点符号,准确率高,耗时短,能力强。