python调用语音识别接口一直recognition error 3301
唉我的个神啊 发布于2017-08 浏览:2842 回复:1
1
收藏

我使用的服务是:百度语音restful api服务

调用的接口是:百度语音接口

开发平台:window10 pycharm python3.6

参数设置情况:

data = {}
# 语音的一些参数
data['format'] = 'wav'
data['rate'] = 8000
data['channel'] = 1
data['lan'] = 'en'
data['cuid'] = self.cu_id
data['token'] = self.token_str
wav_fp = open(filename, 'rb')
voice_data = wav_fp.read()
data['len'] = len(voice_data)
data['speech'] = base64.b64encode(voice_data).decode('utf-8')

代码及返回:

import urllib.request
import urllib
import json
import base64


class BaiduRest:
   def __init__(self, cu_id, api_key, api_secert):
       # token认证的url
       self.token_url = "https://openapi.baidu.com/oauth/2.0/token?grant_type=client_credentials&client_id=%s&client_secret=%s"
       # 语音合成的resturl
       self.getvoice_url = "http://tsn.baidu.com/text2audio?tex=%s&lan=zh&cuid=%s&ctp=1&tok=%s"
       # 语音识别的resturl
       self.upvoice_url = 'http://vop.baidu.com/server_api'

       self.cu_id = cu_id
       self.getToken(api_key, api_secert)
return

   def getToken(self, api_key, api_secert):
       # 1.获取token
       token_url = self.token_url % (api_key, api_secert)

r_str = urllib.request.urlopen(token_url).read()
token_data = json.loads(r_str)
self.token_str = token_data['access_token']
pass

   def getVoice(self, text, filename):
       # 2. 向Rest接口提交数据
       get_url = self.getvoice_url % (urllib.parse.quote(text), self.cu_id, self.token_str)

voice_data = urllib.request.urlopen(get_url).read()
# 3.处理返回数据
       voice_fp = open(filename, 'wb+')
voice_fp.write(voice_data)
voice_fp.close()
pass

   def getText(self, filename):
       # 2. 向Rest接口提交数据
       data = {}
       # 语音的一些参数
       data['format'] = 'wav'
       data['rate'] = 8000
       data['channel'] = 1
       data['lan'] = 'en'
       data['cuid'] = self.cu_id
data['token'] = self.token_str
wav_fp = open(filename, 'rb')
voice_data = wav_fp.read()
data['len'] = len(voice_data)
data['speech'] = base64.b64encode(voice_data).decode('utf-8')
post_data = json.dumps(data).encode('utf-8')
json_length = len(post_data)
r_data = urllib.request.Request(self.upvoice_url)
r_data.add_header("Content-Type", "application/json")
r_data.add_header("Content-Length", json_length)
r_data = urllib.request.urlopen(url=r_data, data=post_data).read()
# 3.处理返回数据
       # r_data=requests.post(self.upvoice_url,json=data,headers={'Content-Type':'application/json'})
       return json.loads(r_data)


if __name__ == "__main__":
   api_key = "SrhYKqzl3SE1URnAEuZ0FKdT"
   api_secert = "hGqeCkaMPb0ELMqtRGc2VjWdmjo7T89d"
   bdr = BaiduRest("test_pythonasdas", api_key, api_secert)
bdr.getVoice("您好", "out.wav")
print(bdr.getText("out.wav"))

返回:

{'err_msg': 'recognition error.', 'err_no': 3301, 'sn': '932559213741502475093'}


复现步骤、现象及其他描述:这里补充说一下,我的代码合成语音是成功了,播放可以听出来。但是识别一直是3301,我在调试的时候也发现data['speech']内容非空,读取正确,但是一直识别不出语音,很郁闷,希望得到你们的帮助,谢谢!


收藏
点赞
1
个赞
共1条回复 最后由用户已被禁言回复于2022-04
#2fujiayi1984回复于2017-08

唉我的个神啊:

我使用的服务是:百度语音restful api服务

调用的接口是:百度语音接口

开发平台:window10 pycharm python3.6

参数设置情况:

data = {}
# 语音的一些参数
data['format'] = 'wav'
data['rate'] = 8000
data['channel'] = 1
data['lan'] = 'en'
data['cuid'] = self.cu_id
data['token'] = self.token_str
wav_fp = open(filename, 'rb')
voice_data = wav_fp.read()
data['len'] = len(voice_data)
data['speech'] = base64.b64encode(voice_data).decode('utf-8')

代码及返回:

import urllib.request
import urllib
import json
import base64


class BaiduRest:
   def __init__(self, cu_id, api_key, api_secert):
       # token认证的url
       self.token_url = "https://openapi.baidu.com/oauth/2.0/token?grant_type=client_credentials&client_id=%s&client_secret=%s"
       # 语音合成的resturl
       self.getvoice_url = "http://tsn.baidu.com/text2audio?tex=%s&lan=zh&cuid=%s&ctp=1&tok=%s"
       # 语音识别的resturl
       self.upvoice_url = 'http://vop.baidu.com/server_api'

       self.cu_id = cu_id
       self.getToken(api_key, api_secert)
return

   def getToken(self, api_key, api_secert):
       # 1.获取token
       token_url = self.token_url % (api_key, api_secert)

r_str = urllib.request.urlopen(token_url).read()
token_data = json.loads(r_str)
self.token_str = token_data['access_token']
pass

   def getVoice(self, text, filename):
       # 2. 向Rest接口提交数据
       get_url = self.getvoice_url % (urllib.parse.quote(text), self.cu_id, self.token_str)

voice_data = urllib.request.urlopen(get_url).read()
# 3.处理返回数据
       voice_fp = open(filename, 'wb+')
voice_fp.write(voice_data)
voice_fp.close()
pass

   def getText(self, filename):
       # 2. 向Rest接口提交数据
       data = {}
       # 语音的一些参数
       data['format'] = 'wav'
       data['rate'] = 8000
       data['channel'] = 1
       data['lan'] = 'en'
       data['cuid'] = self.cu_id
data['token'] = self.token_str
wav_fp = open(filename, 'rb')
voice_data = wav_fp.read()
data['len'] = len(voice_data)
data['speech'] = base64.b64encode(voice_data).decode('utf-8')
post_data = json.dumps(data).encode('utf-8')
json_length = len(post_data)
r_data = urllib.request.Request(self.upvoice_url)
r_data.add_header("Content-Type", "application/json")
r_data.add_header("Content-Length", json_length)
r_data = urllib.request.urlopen(url=r_data, data=post_data).read()
# 3.处理返回数据
       # r_data=requests.post(self.upvoice_url,json=data,headers={'Content-Type':'application/json'})
       return json.loads(r_data)


if __name__ == "__main__":
   api_key = "SrhYKqzl3SE1URnAEuZ0FKdT"
   api_secert = "hGqeCkaMPb0ELMqtRGc2VjWdmjo7T89d"
   bdr = BaiduRest("test_pythonasdas", api_key, api_secert)
bdr.getVoice("您好", "out.wav")
print(bdr.getText("out.wav"))

返回:

{'err_msg': 'recognition error.', 'err_no': 3301, 'sn': '932559213741502475093'}


复现步骤、现象及其他描述:这里补充说一下,我的代码合成语音是成功了,播放可以听出来。但是识别一直是3301,我在调试的时候也发现data['speech']内容非空,读取正确,但是一直识别不出语音,很郁闷,希望得到你们的帮助,谢谢!


回复唉我的个神啊:3301可能是音频文件不符合要求, 另外请确认下format与rate参数是否一致

0
TOP
切换版块