python使用百度语音的时候,结果提示 输入参数不正确,参数错误
xu001314 发布于2016-05 浏览:1936 回复:1
0
收藏

这个是用百度语音识别的部分

def reg(self,filename):
  #获取token
  Api_Key="pmUzrWcsA3Ce7RB5rSqsvQt2"
  Secrect_Key="d39ec848d016a8474c7c25e308b310c3"
 
  self.url = 'https://openapi.baidu.com/oauth/2.0/token?grant_type=client_credentials&client_id='+Api_Key+'&client_secret='+Secrect_Key
 
  res = urllib2.urlopen(self.url).read()
  data = json.loads(res)
  token = data['access_token']    #获取的token
  print 'token获取成功 '#, token
 
  #提交数据
  file_path=self.current_path()
  WAVE_FILE = '%s/%s.wav'%(file_path,filename)
  USER_ID = '8168466'
  FORMAT = 'wav'
  print WAVE_FILE
 
  f = wave.open(WAVE_FILE,"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)
 
  speech = base64.b64encode(wave_data)
  size = nframes

  print 'size',size
 
  update = json.dumps('%s'%{'format':FORMAT,'rate':framerate,'channel':nchannels,'token':token,'cuid':USER_ID,'len':size,'speech':'%s'%speech})
  #print "update",update
 
  self.re_url='http://vop.baidu.com/server_api?lan=zh&cuid='+USER_ID+'&token='+token
 
  r = urllib2.urlopen(self.re_url,update)
 
  #处理JSON
  t = r.read()
  result = json.loads(t)
  #print 'result',result['err_msg']
  print 'result',result #,type(result)
 
  if result[u'err_msg']=='success.':
   word = result['result'][0].encode('utf-8')
   if word!='':
    if word[len(word)-3:len(word)]==',':
     print word[0:len(word)-3]
     return word[0:len(word)-3]
    else:
     print word
     return word
   else:
    print "音频文件不存在或格式错误"
    return '音频文件不存在或格式错误'
  else:
   print self.error_reason[result[u'err_no']]
   return  self.error_reason[result[u'err_no']]

运行后提示“输入参数不正确”

具体是这样的


token获取成功
/home/turtlebot/xu_slam/src/simple_voice/src/testing.wav
size 46000
result {u'err_no': 3300, u'err_msg': u'parameters error.', u'sn': u'224927388791464346476'}
输入参数不正确


收藏
点赞
0
个赞
共1条回复 最后由用户已被禁言回复于2022-04
#2mosal4hi回复于2016-05

请提供请求的时间

0
TOP
切换版块