/
voice.py
executable file
·157 lines (116 loc) · 5.05 KB
/
voice.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#!/usr/bin/env python2
import os
import sys
import traceback
import shutil
import yaml
from client.diagnose import Diagnostics
from client import vocabcompiler, stt
from client import speaker
from client.conversation import Conversation
from client.mic import Mic
from client import GetLocations
#for interprocess communication
import smokesignal
from easyNav_pi_dispatcher import DispatcherClient
import json
class Voice(object):
def __init__(self):
# Set $JASPER_HOME
if not os.getenv('VOICE'):
os.environ["VOICE"] = os.path.abspath(os.path.join(os.path.dirname(__file__), os.pardir))
# # for testing
# if len(sys.argv) > 1 and "--local" in sys.argv[1:]:
# from client.local_mic import Mic
# else:
# from client.mic import Mic
# Change CWD to $JASPER_HOME/jasper/client
client_path = os.path.join(os.getenv("VOICE"), "easyNav-Voice", "client")
os.chdir(client_path)
# Add $JASPER_HOME/jasper/client to sys.path
sys.path.append(client_path)
self.speaker = speaker.newSpeaker()
#interprocess
self.DISPATCHER_PORT = 9002
self.dispatcherClient = DispatcherClient(port=self.DISPATCHER_PORT)
## Attach event listeners
# self.attachEvents(self.speaker)
def start(self):
def testConnection():
if Diagnostics.check_network_connection():
print "CONNECTED TO INTERNET"
else:
print "COULD NOT CONNECT TO NETWORK"
self.speaker.say(
"Warning: I was unable to connect to a network. Parts of the system may not work correctly, depending on your setup.")
def fail(message):
traceback.print_exc()
self.speaker.say(message)
sys.exit(1)
def configure():
try:
print "COMPILING DICTIONARY FOR MODULES"
vocabcompiler.compile(
"sentences.txt", "dictionary.dic", "languagemodel.lm")
print "COMPILING DICTIONARY OF LOCATIONS OPTIONS"
vocabcompiler.compileLocations(
"sentences_locations.txt", "dictionary_locations.dic", "languagemodel_locations.lm");
print "STARTING CLIENT PROGRAM"
except OSError:
print "BOOT FAILURE: OSERROR"
fail(
"There was a problem starting EasyNav. You may be missing the language model and associated files. Please read the documentation to configure your Raspberry Pi.")
except IOError:
print "BOOT FAILURE: IOERROR"
fail(
"There was a problem starting EasyNav. You may have set permissions incorrectly on some part of the filesystem. Please read the documentation to configure your Raspberry Pi.")
except:
print "BOOT FAILURE"
fail(
"There was a problem starting EasyNav.")
old_client = os.path.abspath(os.path.join(os.pardir, "old_client"))
if os.path.exists(old_client):
shutil.rmtree(old_client)
print "==========================================================="
print " JASPER The Talking Computer "
print " Copyright 2013 Shubhro Saha & Charlie Marsh "
print "==========================================================="
self.speaker.say("Hello.... I am EASYNAV... Please wait one moment while I configure.")
configure()
profile = yaml.safe_load(open("profile.yml", "r"))
try:
api_key = profile['keys']['GOOGLE_SPEECH']
except KeyError:
api_key = None
try:
stt_engine_type = profile['stt_engine']
except KeyError:
print "stt_engine not specified in profile, defaulting to PocketSphinx"
stt_engine_type = "sphinx"
mic = Mic(self.speaker, stt.PocketSphinxSTT(),
stt.newSTTEngine(stt_engine_type, api_key=api_key))
mic.say("Hi...i'm EasyNav.....your friendly navigation assistant....")
mic.say("To invoke me, please say my Name......and i will beep....then proceed with your command")
mic.say("To find out about commands...please say help......")
self.dispatcherClient.start()
#GetLocations.getLoc()
conversation = Conversation("EASYNAV", mic, profile, self.dispatcherClient)
conversation.handleForever()
# def attachEvents(self, mic):
# ## clear all signals
# smokesignal.clear()
# text = ""
# @smokesignal.on('say')
# def onSay(args):
# print "Info from Nav"
# infoFromNav = eval(args.get('payload'))
# print infoFromNav
# infotosay = infoFromNav["text"]
# print infotosay
# print "Info from Nav before Mic"
# mic.say(infotosay)
def runMain():
voice = Voice()
voice.start()
if __name__ == '__main__':
runMain()