Repository navigation
Expand file tree
/
Copy pathListener.py
More file actions
54 lines (46 loc) · 1.92 KB
/
Copy pathListener.py
File metadata and controls
54 lines (46 loc) · 1.92 KB
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
import threading
import speech_recognition as sr
import Miscellaneous
import ConnectionChecker
WIT_AI_KEY = "VR76YG3PY55TB6TWK7O3LBKS3QL3KKUH"
class Listener(threading.Thread):
def __init__(self):
self.running = True
self.command = "nothing"
self.ready = False
self.listening = False
self.inet = ConnectionChecker.ConnectionChecker()
threading.Thread.__init__(self)
self.start()
def run(self):
self.rec = sr.Recognizer()
with sr.Microphone() as self.source:
self.rec.adjust_for_ambient_noise(self.source, duration=2)
self.ready = True
while(self.running):
print('Listening')
with sr.Microphone() as self.source:
self.listening = True
self.audio = self.rec.listen(self.source)
self.listening = False
if(not self.inet.isConnected):
try:
self.command = self.rec.recognize_sphinx(self.audio)
print("Sphinx: " + self.command)
except sr.UnknownValueError:
print("Sphinx: Could not understand what you said")
except sr.RequestError as e:
print("Sphinx error: {0}".format(e))
else:
try:
self.command = self.rec.recognize_wit(self.audio, WIT_AI_KEY)
print("Wit: " + self.command)
except sr.UnknownValueError:
print("Wit: Could not understand what you said")
except sr.RequestError as e:
print("ERR: {0}".format(e))
print("Lost internet connection. Using Sphinx to interpret voice commands. Results may be less accurate")
self.inet.isConnected = False
except:
self.running = False
print("THREAD DIED IDK WHY")