forked from rcorcs/NatI
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
57 lines (44 loc) · 1.16 KB
/
main.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
48
49
50
51
52
53
54
55
56
57
import sys
import alsaaudio, wave
import numpy as np
import psw
import gapi
import commands
speech = gapi.Speech('en-uk')
if len(sys.argv)==2:
if sys.argv[1] in gapi.languages.keys():
speech.lang = gapi.languages[sys.argv[1]]
elif sys.argv[1] in gapi.languages.values():
speech.lang = sys.argv[1]
def handler(fileName):
global speech
translator = gapi.Translator(speech.lang, 'en-uk')
try:
cfileName = psw.convert(fileName)
phrase = speech.getText(cfileName)
import os
os.remove(fileName)
os.remove(cfileName)
if phrase!=None:
phrase = phrase.lower()
if len(phrase.strip())>0:
print 'text:',phrase
#psw.play(speech.getAudio(phrase))
cmd = translator.translate(phrase).strip().lower()
print 'cmd:',cmd
commands.execute(cmd, speech)
except Exception, e:
print "Unexpected error:", sys.exc_info()[0], e
return True
mic = psw.Microphone()
print 'sampling...'
sample = np.array(mic.sample(200))
print 'done'
#import matplotlib.pyplot as plt
#plt.plot(sample)
#plt.show()
#from scipy import ndimage
#sample = ndimage.gaussian_filter(sample, sigma=3)
#plt.plot(sample)
#plt.show()
mic.listen(handler, sample.mean(), sample.std())