I want to use Google's Speech API using the Python code below and a wav file (or an audio file in some other format for that matter). I get a broken pipe error at the moment, which I don't know how to fix. Have been reading a bit about changing headers here, but feel I would need some guidance there if this is the way forward. Don't know if this is actually supposed to work, using the Google Web Speech API Demo:
My code:
#!/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)
The url you use is not correct API url, the url for v1 speech API is https://www.google.com/speech-api/v1/recognize, however, it is turned down for quite some time already. See for details
Google speech Api v1 not working?
You might want to use streaming API v2 with Google, but those requires API key, see for details https://github.com/gillesdemey/google-speech-v2
Overall, I recommend you to use existing wrapper instead, it will hide all API complexity. This wrapper should be good:
https://pypi.python.org/pypi/SpeechRecognition/
You still need an API key from google.
Alternatively you can use other API endpoint, like Project Oxford from Microsoft.