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)