-
Notifications
You must be signed in to change notification settings - Fork 0
/
simon.py
executable file
·140 lines (119 loc) · 3.93 KB
/
simon.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
#!/usr/bin/env python3
import logging
import time
import sys
from threading import Timer, Thread, Event
from respeaker import Microphone
from pixel_ring import pixel_ring
from gpiozero import LED
from speech_recognition import Microphone, Recognizer, WaitTimeoutError, UnknownValueError
import pyalmond
import pyttsx3
logging.basicConfig(stream=sys.stdout, level=logging.DEBUG)
power = LED(5)
power.on()
name = 'simon'
tts = pyttsx3.init()
def get_greeting():
from random import choice
greetings = [
"How can I help?",
]
return choice(greetings)
class Assistant():
def __init__(self, name, mic):
self.name = name
self.mic = mic
self.recognizer = Recognizer()
self.awake = False
def listen(self):
with self.mic:
self.recognizer.adjust_for_ambient_noise(self.mic, 2)
print("Starting listener...")
self.stop = self.recognizer.listen_in_background(self.mic, self._proc_audio)
def stop(self):
raise RuntimeError("Attempted to stop assistant before starting it")
def wake_up(self):
self.awake = True
def _timeout():
self.awake = False
timer = Timer(30, _timeout)
timer.start()
def _proc_audio(self, recog, audio):
try:
# decoded = recog.recognize_sphinx(audio, show_all=True)
# txt = decoded.hyp().hypstr
txt = recog.recognize_google(audio, show_all=True)
print(f"recognized utterance: {txt}")
except UnknownValueError as e:
# txt = recog.recognize_google(audio)
print("Did not recognize utterance")
# print(txt)
if self.name in txt:
self.wake_up()
if self.awake and not txt.endswith(self.name):
print(f"Sending commands: {txt}")
pixel_ring.think()
# send command to HA here
pixel_ring.off()
self.awake = False
else:
print(f"Not awake or name was give without command")
return
#def task(quit_event):
# #mic = Microphone(quit_event=quit_event)
# pass
def get_mic():
mic_index = None
for i, microphone_name in enumerate(Microphone.list_microphone_names()):
if 'seeed' in microphone_name:
mic_index = i
print("Using microphone {}".format(microphone_name))
break
if not mic_index:
print("Could not find a proper microphone")
exit()
return Microphone(device_index=mic_index)
def task(quit_event):
with Microphone(device_index=mic_index, quit_event=quit_event) as mic:
recognizer = Recognizer()
while not quit_event.is_set():
pixel_ring.off()
print("Listening for keyword")
data = recognizer.listen(source=mic)
kw_text = recognizer.recognize_sphinx(data)
print("Heard '{}' while listening for keyword".format(kw_text))
if kw_text == name:
print('Wake up')
pixel_ring.listen()
data = recognizer.listen(mic)
pixel_ring.think()
text = recognizer.recognize_sphinx(data)
print('Done listening')
pixel_ring.off()
if text:
print('Recognized {}'.format(text))
tts.say(text)
#tts.runAndWait()
def main():
logging.basicConfig(level=logging.DEBUG)
simon = Assistant('simon', get_mic())
quit_event = Event()
#thread = Thread(target=task, args=(quit_event,))
#thread.daemon = True
#thread.start()
pixel_ring.set_brightness(9)
simon.listen()
tts.startLoop()
print("Starting main loop")
while True:
try:
time.sleep(1)
except KeyboardInterrupt:
print('Quit')
quit_event.set()
simon.stop()
break
time.sleep(1)
if __name__ == '__main__':
main()