- 首先在腾讯ai开放平台注册,依次找到技术引擎->语音识别,选择语音识别一栏,在技术文档里左侧的新手指南下载python版本的SDK.同时在免费试用的接入能力中,创建应用,并得到app_key和app_id,修改test_aai_wxasrs.py
import sys
import os
import optparse
import time
import apiutil
import base64
import json
import hashlib
import time
import glob
import codecs
app_key = ''
app_id = '‘
if __name__ == '__main__':
wav_files = glob.glob(u'''./tools/TX_aiplat/audio/*.wav''')
count = 1
for wav_file in wav_files:
if not os.path.exists(wav_file[:-3] + "trn"):
seq = 0
for_mat = 2
rate = 16000
bits = 16
cont_res = 1
file_path = wav_file
f = open(file_path, 'r')
md5obj = hashlib.md5()
md5obj.update(f.read())
hash = md5obj.hexdigest()
speech_id = str(hash).upper()
f.close()
f = open(file_path, 'rb')
file_size = os.path.getsize(file_path)
try:
chunk = f.read()
if not chunk:
pass
else:
chunk_size = len(chunk)
if (seq + chunk_size) == file_size:
end = 1
else:
end = 0
ai_obj = apiutil.AiPlat(app_id, app_key)
print '----------------------SEND REQ----------------------'
rsp = ai_obj.getAaiWxAsrs(chunk, speech_id, end, for_mat, rate, bits, seq, chunk_size, cont_res)
seq += chunk_size
print rsp
if rsp['ret'] == 0:
results = json.dumps(rsp, encoding="UTF-8", ensure_ascii=False, sort_keys=False, indent=4)
results = json.loads(results)
if results['data']['speech_text'] != u'':
print wav_file
trn_file = wav_file[:-3] + "trn"
with codecs.open(trn_file,'w','utf-8') as wp:
wp.write(results['data']['speech_text'])
count = count + 1
print u'''处理第%d个音频:%s''' %(count,wav_file)
print '----------------------API SUCC----------------------'
else:
os.remove(wav_file)
else:
print json.dumps(rsp, encoding="UTF-8", ensure_ascii=False, sort_keys=False, indent=4)
print '----------------------API FAIL----------------------'
except:
f.close()
continue