forked from JamezQ/Palaver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
send_speech.py
executable file
·35 lines (28 loc) · 986 Bytes
/
send_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
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
import httplib
import json
import sys
import os
class Stt:
def get_language(self):
filePath = os.environ['HOME'] + '/.palaver.d/UserInfo'
if os.path.isfile(filePath):
content = open(filePath, 'r')
for line in content:
if 'LANGUAGE' in line:
return str(line).partition('=')[2].strip()
return str('en')
def speech_to_text(self, audio):
url = 'www.google.com'
path = '/speech-api/v1/recognize?client=chromium&lang=' + self.get_language()
headers = {'Content-type': 'audio/x-flac; rate=16000' };
conn = httplib.HTTPSConnection(url)
conn.request('POST', path, audio, headers)
response = conn.getresponse()
data = response.read()
try:
jsdata = json.loads(data)
return jsdata['hypotheses'][0]['utterance']
except Exception:
return ''