python3 调用百度语音识别服务 总是返回 {'err_msg': 'url param cuid error.', 'err_no': 3300, 'sn': '26556231821490279985'}
opticaloptical 发布于2017-03 浏览:4314 回复:3
0
收藏

我使用的服务是:REST API

调用的接口是:

http://vop.baidu.com/server_api?lan=zh'+'?cuid='+cuid+'&token='+token

参数设置情况:

VOICE_RATE = 8000
WAVE_FILE = "D:/record.wav"

WAVE_TYPE = "wav"

单声道

开发平台和机型:windows10,64位,pycharm, python3.6

SDK版本号:

代码或日志截图(上传截图能帮助您更快解决问题):

import requests
import json
import wave
import base64
import os
import pyaudio



#录音功能在另一个模块中实现,将录音功能存入本地文件中
#获取token
API_KEY='XX'
SECRET_KEY='YY'
authUrl='https://openapi.baidu.com/oauth/2.0/token?grant_type=client_credentials&client_id='+API_KEY+'&client_secret='+ SECRET_KEY
response=requests.get(authUrl)
print(response)
res=json.loads(response.content)
token=res['access_token']
#语音识别

srvUrl='http://vop.baidu.com/server_api?lan=zh'+'?cuid='+cuid+'&token='+token
print(srvUrl)

#设置音频属性,根据百度的要求,采样率必须为8000,压缩格式支持pcm(不压缩)、wav、opus、speex、amr
VOICE_RATE = 8000
WAVE_FILE = "D:/PycharmProjects/Exc/PPP/record.wav" #音频文件的路径注意win下用/,\在python中有转意的含义
WAVE_TYPE = "wav"
#打开音频文件,并进行编码
with open(WAVE_FILE,"rb") as f:
speech_data=f.read()
#进行编码
speech_base64 = base64.b64encode(speech_data).decode('utf-8')
#size = len(speech_data)
size=os.path.getsize(WAVE_FILE )
update = json.dumps({'format':WAVE_TYPE, 'rate':VOICE_RATE, 'channel':1,'cuid':"E4-02-9B-ZZ-YY-XX" ,'token':token,'speech':speech_base64,'len':size}).encode('utf-8')

httpHeaders={
'Content-Type':'audio/wav;rate=8000',
}
response=requests.post(srvUrl,headers=httpHeaders,data=update)

res=json.loads(response.content)
text=res['result'][0]
print("识别结果",text)


接口调用总是失败,debug之后发现具体错误信息如下:

{'err_msg': 'url param cuid error.', 'err_no': 3300, 'sn': '26556231821490279985'}

请问是什么原因(怀疑是编码处导致的问题)


复现步骤、现象及其他描述:

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

opticaloptical:

我使用的服务是:REST API

调用的接口是:

http://vop.baidu.com/server_api?lan=zh'+'?cuid='+cuid+'&token='+token

参数设置情况:

VOICE_RATE = 8000
WAVE_FILE = "D:/record.wav"

WAVE_TYPE = "wav"

单声道

开发平台和机型:windows10,64位,pycharm, python3.6

SDK版本号:

代码或日志截图(上传截图能帮助您更快解决问题):

import requests
import json
import wave
import base64
import os
import pyaudio



#录音功能在另一个模块中实现,将录音功能存入本地文件中
#获取token
API_KEY='XX'
SECRET_KEY='YY'
authUrl='https://openapi.baidu.com/oauth/2.0/token?grant_type=client_credentials&client_id='+API_KEY+'&client_secret='+ SECRET_KEY
response=requests.get(authUrl)
print(response)
res=json.loads(response.content)
token=res['access_token']
#语音识别

srvUrl='http://vop.baidu.com/server_api?lan=zh'+'?cuid='+cuid+'&token='+token
print(srvUrl)

#设置音频属性,根据百度的要求,采样率必须为8000,压缩格式支持pcm(不压缩)、wav、opus、speex、amr
VOICE_RATE = 8000
WAVE_FILE = "D:/PycharmProjects/Exc/PPP/record.wav" #音频文件的路径注意win下用/,\在python中有转意的含义
WAVE_TYPE = "wav"
#打开音频文件,并进行编码
with open(WAVE_FILE,"rb") as f:
speech_data=f.read()
#进行编码
speech_base64 = base64.b64encode(speech_data).decode('utf-8')
#size = len(speech_data)
size=os.path.getsize(WAVE_FILE )
update = json.dumps({'format':WAVE_TYPE, 'rate':VOICE_RATE, 'channel':1,'cuid':"E4-02-9B-ZZ-YY-XX" ,'token':token,'speech':speech_base64,'len':size}).encode('utf-8')

httpHeaders={
'Content-Type':'audio/wav;rate=8000',
}
response=requests.post(srvUrl,headers=httpHeaders,data=update)

res=json.loads(response.content)
text=res['result'][0]
print("识别结果",text)


接口调用总是失败,debug之后发现具体错误信息如下:

{'err_msg': 'url param cuid error.', 'err_no': 3300, 'sn': '26556231821490279985'}

请问是什么原因(怀疑是编码处导致的问题)


复现步骤、现象及其他描述:

回复opticaloptical:感谢您的关注

0
#3opticaloptical回复于2017-04

谢谢,我先自查一下。

0
#2xie_yongkang回复于2017-04
http://vop.baidu.com/server_api?lan=zh'+'?cuid='+cuid+'&token='+token

cuid参数前的?应该改为&, 并且代码中并未看到cuid的设置值, 需要注意

0
TOP
切换版块