我想使用Google的语音API,使用下面的Python代码和wav文件(或其他格式的音频文件)。我现在的管道出了故障,我不知道怎么解决。已经阅读了一些关于更改头这里的内容,但是如果这是前进的方向,我觉得我需要一些指导。不知道这是否真的有效,使用Google语音API演示:
我的代码:
#!/usr/bin/python
import sys
import urllib.request
import urllib.parse
import json
import scipy.io.wavfile
try:
filename = sys.argv[1]
except IndexError:
print('Usage: transcribe.py <file>')
sys.exit(1)
rate, data = scipy.io.wavfile.read(filename)
url = 'https://www.google.com/intl/en/chrome/demos/speech.html'
headers = {'Content-type': 'audio/wav; rate=16000'}
# Possibly use this somehow later on...
# user_agent = 'Mozilla/5.0 (Windows NT 6.1; Win64; x64)'
# values = {'name' : 'Michael Foord',
'location' : 'Northampton',
'language' : 'Python' }
req = urllib.request.Request(url, data, headers)
try:
ret = urllib.request.urlopen(req)
except urllib.error.URLError as e:
print(e.reason)
sys.exit(1)
resp = ret.read()
text = json.loads(resp)['hypotheses'][0]['utterance']
print(text)发布于 2016-03-01 19:42:44
您使用的url是不正确的API url,v1语音API的url是https://www.google.com/speech-api/v1/recognize,但是它已经被拒绝了相当一段时间。详情见
您可能希望在Google中使用流式API v2,但这些都需要API键,有关详细信息,请参阅https://github.com/gillesdemey/google-speech-v2。
总之,我建议您使用现有的包装器,它将隐藏所有的API复杂性。这个包装应该很好:
https://pypi.python.org/pypi/SpeechRecognition/
您仍然需要一个来自google的API密钥。
或者,您也可以使用其他API端点,比如Microsoft的Project Oxford。
https://stackoverflow.com/questions/35729634
复制相似问题