-
Notifications
You must be signed in to change notification settings - Fork 11
/
speech.py
48 lines (36 loc) · 1.09 KB
/
speech.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#!/usr/bin/python
import sys
import urllib2
import os
import json
import subprocess as sp
url = "https://www.google.com/speech-api/v1/recognize?xjerr=1&client=chromium&lang=en-US"
fileName = str(sys.argv[1])
fileExtension = os.path.splitext(fileName)[1]
converted = False
if fileExtension != ".flac":
fnull = open(os.devnull, 'w')
sp.call("pacpl --overwrite -t flac " + fileName, shell = True, stdout = fnull, stderr = fnull)
fnull.close()
fileName = os.path.splitext(fileName)[0] + '.flac'
converted = True
try:
binary_audio = open(fileName, 'rb')
except:
print "Failed to get binary data."
size_of_audio = os.path.getsize(fileName)
if converted:
os.remove(fileName)
request = urllib2.Request(url)
request.add_header('Content-type','audio/x-flac; rate=16000')
request.add_header('Content-length', str(size_of_audio))
request.add_data(binary_audio)
try:
response = urllib2.urlopen(request)
except urllib2.URLError, e:
print "Unable to connect"
except urllib2.HTTPError, e:
print "Oops, bad request"
content = response.read()
data = json.loads(content)
print data["hypotheses"][0]["utterance"]