百度语音API使用(python实现之二)
昨天是原始PCM数据上传,响应太慢,profiling一番,发现是curl耗时,估计是音频数据上传耗时
于是改用speex压缩编码格式
#encoding=utf-8
import os
import time
import urllib2, pycurl
import base64
import json
TOKEN_PATH = "/home/wang/.speech.token"
## get access token by api key & secret key
def has_token():
try:
stat_info = os.stat(TOKEN_PATH)
except OSError:
return False
if stat_info.st_size < 10: #invalid if too small
return False
db_ctime = stat_info.st_ctime
create_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 update
else:
return True
def get_token():
if has_token():
fp = open(TOKEN_PATH, "r")
token = fp.readline().rstrip("
")
fp.close()
return token
apiKey = "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 token
def dump_res(buf):
fp = open("log_spx.log", "w")
fp.write(buf)
fp.close()
## post audio to server
def 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 MAC
srv_url = "http://vop.baidu.com/server_api" + "?cuid=" + cuid + "&token=" + token
http_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 dict
c.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 val
if __name__ == "__main__":
token = get_token()
use_cloud(token)但是百度“识别失败”,考虑到我的麦克风太差,换用样例代码中的test.pcm压成speex格式再试,仍然“识别错误”,求教!
{"err_msg":"recognition error.","err_no":3301,"sn":"320487617001432001015"}声明:该文观点仅代表作者本人,入门客AI创业平台信息发布平台仅提供信息存储空间服务,如有疑问请联系rumenke@qq.com。
- 上一篇:没有了
- 下一篇: 正确使用Java的foreach语句
