python调用识别API返回参数错误
方寸間 发布于2016-01 浏览:2666 回复:3
0
收藏

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编码后,数据变成以下的格式:

收藏
点赞
0
个赞
共3条回复 最后由用户已被禁言回复于2022-04
#4fujiayi1984回复于2017-09

ighack:

你需要按JSON提交

headers = {'content-type': 'application/json;charset=utf-8'}

requests.post(url = asr_url, data = json.dumps(data))

回复ighack:感谢您的分享

0
#3ighack回复于2017-04

你需要按JSON提交

headers = {'content-type': 'application/json;charset=utf-8'}

requests.post(url = asr_url, data = json.dumps(data))

0
#20半君0回复于2016-01

百度语音识别REST API方式不再支持speex压缩格式,谢谢~

0
TOP
切换版块