import requests
import json
import base64
import wave
import numpy as np
if __name__ == "__main__":
#Step1: get the Access Token
auth_url = "https://openapi.baidu.com/oauth/2.0/token"
grant_type = "client_credentials"
apiKey = "************"
secretKey = "*******************"
data = {"grant_type":grant_type, "client_id":apiKey, "client_secret": secretKey}
result = requests.post(url = auth_url, data = data)
ret = json.loads(result.text)
#Step2:
asr_url = "http://vop.baidu.com/server_api"
formattype = "wav"
cuid = "baidu_workshop"
token = ret["access_token"]
filename = "test.wav"
f = wave.open(filename, "rb")
params = f.getparams()
nchannels, sampwidth, framerate, nframes = params[:4]
str_data = f.readframes(nframes)
f.close()
wave_data = np.fromstring(str_data, dtype = np.short)
wave_data = wave_data
speech = base64.b64encode(wave_data)
asr_data = {"format":formattype, "rate":framerate, "channel":nchannels, "cuid":cuid, "token":token, "speech":speech, "len":nframes}
result = requests.post(url = asr_url, data = data)
ret = json.loads(result.text)
返回err_no : 3300
access_token返回是OK的,python读取wav数据后是数组,直接做base64的编码后作为speech参数上传,这个是不是会有问题呢?base64编码后,数据变成以下的格式:
ighack:
你需要按JSON提交
headers = {'content-type': 'application/json;charset=utf-8'}
requests.post(url = asr_url, data = json.dumps(data))
回复ighack:感谢您的分享
你需要按JSON提交
headers = {'content-type': 'application/json;charset=utf-8'}
requests.post(url = asr_url, data = json.dumps(data))
百度语音识别REST API方式不再支持speex压缩格式,谢谢~