-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconnected.py
More file actions
180 lines (136 loc) · 6.25 KB
/
connected.py
File metadata and controls
180 lines (136 loc) · 6.25 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
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
from kivy.app import App
from kivy.uix.screenmanager import Screen, SlideTransition
from kivy.uix.dropdown import DropDown
from kivy.uix.button import Button
from kivy.uix.boxlayout import BoxLayout
from kivy.core.window import Window
from kivy.uix.image import Image
from gql import gql, Client
from gql.transport.requests import RequestsHTTPTransport
import os
import time
import threading
from threading import Thread
import speech_recognition as sr
import settings
class Connected(Screen):
microphone_list = []
listening_is_on = False
transport = None
client = None
app = None
audio_thread = None
kill_mic_thread = None
transport = None
def on_enter(self, *args):
self.kill_mic_thread = threading.Event()
global apiURL
self.transport = RequestsHTTPTransport(
url=settings.apiURL,
use_json=True
)
self.transport.headers = {'Authorization': 'Bearer ' + self.app.jwt_token}
self.client = Client(
retries=3,
transport=self.transport,
fetch_schema_from_transport=True,
)
query = gql("query { user { listenerCommand commands { from, to, type } } }")
try:
self.app.commands = self.client.execute(query)
print(self.app.commands)
except Exception as err:
print(err)
def __init__(self, **kwargs):
self.name='connected'
super(Screen,self).__init__(**kwargs)
self.app = App.get_running_app()
self.r = sr.Recognizer()
self.m = sr.Microphone()
self.microphone_list = sr.Microphone.list_microphone_names()
layout = BoxLayout(orientation='vertical', size=(Window.size[0],Window.size[1]), pos=(0, 0), size_hint=(None, None))
dropdown = DropDown()
for microphone in self.microphone_list:
btn = Button(text=microphone, size_hint_y=None, height=44)
btn.bind(on_release=lambda btn: dropdown.select(btn.text))
dropdown.add_widget(btn)
# create a big main button
self.dropdown_btn = Button(text=self.microphone_list[0], pos=(0, 0), size_hint = (1, None))
self.dropdown_btn.bind(on_release=dropdown.open)
dropdown.bind(on_select=self.on_select_microphone)
self.loading_img = Image(source = 'images/listening_off.jpeg', allow_stretch=True)
self.listening_btn = Button(text="Make me listen...", pos=(0, 0), size_hint = (1, None), on_press=self.change_listening_status)
self.disconnect_btn = Button(text="Disconnect", pos=(0, 0), size_hint = (1, None), on_press=self.disconnect)
layout.add_widget(self.listening_btn)
layout.add_widget(self.loading_img)
layout.add_widget(self.dropdown_btn)
layout.add_widget(self.disconnect_btn)
self.add_widget(layout)
def change_listening_status(self, instance):
if self.listening_is_on == True:
self.stop_active_listening()
else:
self.start_active_listen()
def on_select_microphone(self, instance, selected_microphone):
setattr(self.dropdown_btn, 'text', selected_microphone)
if self.audio_thread:
self.stop_active_listening()
microphones = self.m.list_microphone_names()
self.m = sr.Microphone(device_index=microphones.index(selected_microphone))
def listen_in_background(self, microphone, recognizer, data, stop_event):
while not stop_event.wait(1):
print("-> Waiting control command!!!")
try:
with microphone as source:
audio = recognizer.listen(source, timeout=3, phrase_time_limit=5)
print("-> Checking control command!!!")
call_command = recognizer.recognize_google(audio) # recognize_google
print(call_command)
if data['user']['listenerCommand'] in call_command:
os.system('say Tell me a command!')
print("-> Waiting command!!!")
with microphone as source:
audio = recognizer.listen(source, timeout=3, phrase_time_limit=5)
print("-> Checking command!!!")
exec_command = recognizer.recognize_google(audio) # recognize_sphinx
print(exec_command)
for command in data['user']['commands']:
if command['type'] != "voiceCommand":
continue
if command['from'] in exec_command:
os.system('say Command accepted!')
mutation = gql("mutation { user { sendCommand(fromCommand: \"" + exec_command +"\", type: \""+ command['type'] +"\") } }")
try:
self.client.execute(mutation)
os.system('say Command executed!')
except Exception as err:
print(err)
print("------")
except sr.UnknownValueError:
print("Could not understand audio")
except sr.RequestError as e:
print("Error; {0}".format(e))
except Exception as e:
print("Error; {0}".format(e))
def start_active_listen(self):
print("Start Listening!!!")
self.listening_is_on = True;
self.listening_btn.text = "Stop listening!"
self.loading_img.source = 'images/listening_on.png'
self.loading_img.reload()
self.audio_thread = Thread(target = self.listen_in_background, args=(self.m, self.r, self.app.commands, self.kill_mic_thread))
self.audio_thread.start()
def stop_active_listening(self):
print("Stopped Listening!!!")
self.listening_is_on = False;
self.listening_btn.text = "Make me listen!"
self.loading_img.source = 'images/listening_off.jpeg'
self.loading_img.reload()
self.kill_mic_thread.set()
if self.audio_thread:
self.audio_thread.join()
def disconnect(self, instance):
self.stop_active_listening()
self.manager.transition = SlideTransition(direction="right")
self.manager.current = 'login'
self.manager.get_screen('login').resetForm()