百度语音API使用(python实现之二)

昨天是原始PCM数据上传,响应太慢,,profiling一番,发现是curl耗时,估计是音频数据上传耗时

于是改用speex压缩编码格式

#encoding=utf-8import osimport timeimport urllib2, pycurlimport base64import jsonTOKEN_PATH = '/home/wang/.speech.token'## get access token by api key & secret keydef has_token():try:stat_info = os.stat(TOKEN_PATH)except OSError:return Falseif stat_info.st_size < 10: #invalid if too smallreturn Falsedb_ctime = stat_info.st_ctimecreate_date = time.strftime('%m', time.localtime(db_ctime))current_date = time.strftime('%m', time.localtime(time.time()))if current_date != create_date:return False #old beyond 1 day, need updateelse:return Truedef get_token():if has_token():fp = open(TOKEN_PATH, 'r')token = fp.readline().rstrip('\n')fp.close()return tokenapiKey = "FzxxxxxwY0SS"secretKey = "66xx5axxxb882a"auth_url = "https://openapi.baidu.com/oauth/2.0/token?grant_type=client_credentials&client_id=" + apiKey + "&client_secret=" + secretKey;res = urllib2.urlopen(auth_url)json_data = res.read()token = json.loads(json_data)['access_token']fp = open(TOKEN_PATH, 'w')fp.write(token)fp.close()return tokendef dump_res(buf):fp = open('log_spx.log', 'w')fp.write(buf)fp.close()## post audio to serverdef use_cloud(token):fp = open('test.spx', 'rb')fp.seek(os.SEEK_END)f_len = fp.tell()audio_data = fp.read(f_len)cuid = "acxxxxxx677" #my xiaomi phone MACsrv_url = '' + '?cuid=' + cuid + '&token=' + tokenhttp_header = ['Content-Type: audio/speex; rate=8000','Content-Length: %d' % f_len]c = pycurl.Curl()c.setopt(pycurl.URL, str(srv_url)) #curl doesn't support unicode#c.setopt(c.RETURNTRANSFER, 1)c.setopt(c.HTTPHEADER, http_header) #must be list, not dictc.setopt(c.POST, 1)c.setopt(c.CONNECTTIMEOUT, 30)c.setopt(c.TIMEOUT, 30)c.setopt(c.WRITEFUNCTION, dump_res)c.setopt(c.POSTFIELDS, audio_data)c.setopt(c.POSTFIELDSIZE, f_len)c.perform() #pycurl.perform() has no return valif __name__ == "__main__":token = get_token()use_cloud(token)但是百度“识别失败”,考虑到我的麦克风太差,换用样例代码中的test.pcm压成speex格式再试,仍然“识别错误”,求教!

{"err_msg":"recognition error.","err_no":3301,"sn":"320487617001432001015"}

明天的希望,让我们忘了今天的痛苦

百度语音API使用(python实现之二)

相关文章:

你感兴趣的文章:

标签云: