-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvoice-control.py
72 lines (65 loc) · 1.61 KB
/
voice-control.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#!/usr/bin/env python
import time
import sys
import pyjulius
import Queue
import urllib2
import os
import subprocess
# score threshold value, words with smaller score will be discarded
threshold = 70
client = pyjulius.Client('localhost', 10500)
try:
print "Starting julius..."
FNULL = open(os.devnull, 'w')
proc = subprocess.Popen(["julius/julius/julius", "-C", "settings.jconf", "-dnnconf", "dnn.jconf"], stdout=FNULL, stderr=FNULL)
except:
print "Error during startup."
proc.kill()
sys.exit(1)
print "Done."
while 1:
try:
client.connect()
print "Connected. Ready for user input."
break
except pyjulius.ConnectionError:
print "Waiting for julius to fully start up..."
try:
time.sleep(1)
except KeyboardInterrupt:
print "Stopped."
proc.kill()
sys.exit(1)
# main loop
client.start()
try:
while 1:
try:
result = client.results.get(False)
except Queue.Empty:
continue
if isinstance(result, pyjulius.Sentence):
if (result.score >= threshold):
print "Word: %s\tScore: %.2f" % (result, result.score)
if str(result) == "lumos":
try:
urllib2.urlopen("http://lumen.local/?state=on")
except:
print "Error: lumen.local is unreachable, check your network connection."
elif str(result) == "nox":
try:
urllib2.urlopen("http://lumen.local/?state=off")
except:
print "Error: lumen.local is unreachable, check your network connection."
else:
print "Discarded.\tScore: %.2f" % (result.score)
# handle user interrupt
except KeyboardInterrupt:
print "Stopped."
# cleanup
finally:
client.stop()
client.join()
client.disconnect()
proc.kill()