我有一个大的mp3文件(大约1.8GB),我必须使用wit.ai进行转录。由于我经常使用wav文件,所以我将其转换为wav文件。
但是由于wit.ai的语音api不能占用10 s长的音频,所以我计划将文件以块的形式进行流。但有些我只是得到回复400(糟糕的要求)。我无法弄清楚,我发错了什么。详情如下:
headers = {'authorization': 'Bearer ' + wit_access_token,
'Content-Type': 'audio/wav','Transfer-encoding': 'chunked'}
with open('meeting-record.wav', 'rb') as f:
audio = f.read(2048) # taken it any number
resp = requests.post(API_ENDPOINT, headers = headers,
data = audio)
print(resp)
data = json.loads(resp.content)
text = data['_text']
print(text)
f.close()
我得到以下输出
<Response [400]>
Traceback (most recent call last):
File ".\sound-record.py", line 61, in <module>
text = data['_text']
KeyError: '_text'
有人能给我指点一下哪里出了问题吗?
发布于 2018-08-22 07:34:40
我以前没有使用过wit.ai API,但必应语音API似乎以类似的方式要求数据。我不确定您是否因为代码而得到了错误,但是为了正确地对文件进行块和流处理,您可以在其中添加另一个函数,如下所示:
def stream_audio_file(speech_file, chunk_size=1024):
# Chunk audio file
with open(speech_file, 'rb') as f:
while 1:
data = f.read(1024)
if not data:
break
yield data
现在,只要在文件中的某个地方有该函数为您提供数据流和数据块,您就可以返回您的初始方法:
headers = {
'Accept': 'application/json',
'Transfer-Encoding': 'chunked',
'Content-type': 'audio/wav',
'Authorization': 'Bearer {0}'.format(YOUR_AUTH_TOKEN)
}
data = stream_audio_file(YOUR_AUDIO_FILE)
r = requests.post(url, headers=headers, data=data)
results = json.loads(r.content)
print(results)
侧注:,您刚才提到您想在自己的服务器上找到一些东西。有一个很好的模块名为麻雀,它是免费的,托管在您的机器上,用Python编写。它与SpeechRecognition模块非常匹配,它在上面提供了一个很好的层,所以您不必花那么多时间格式化请求。
发布于 2018-04-19 06:34:15
https://stackoverflow.com/questions/49697393
复制相似问题