forked from bjoernkarmann/project_alias
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
179 lines (143 loc) · 5.33 KB
/
app.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# coding=utf-8
import numpy as np
import time
from threading import Thread
# Import modules
from modules import globals
from modules import connection
from modules import sound
from modules import ai
from modules import led
import atexit
# Global inits
#====================================================#
#init and setup RPI LEDs
led.LED.off()
#Initialize the sound objects
noise = sound.audioPlayer("data/noise.wav",-1,"noise",True)
wakeup = sound.audioPlayer("data/ok_google.wav",0,"wakeup", False)
# Socket connection between client
#====================================================#
@connection.socketio.on('msgEvent', namespace='/socket')
def test_message(message):
msg = message['data']
print(msg);
print("----------------------")
globals.PREDICT = False; #always stop prediction on button comman
#Add example to class 0 - Silence / background noise
if('class0' in msg and globals.EXAMPLE_READY):
example = sound.get_spectrogram()
ai.addExample(example,0)
globals.BG_EXAMPLES += 1
led.LED.listen()
#Add example to class 1 - WakeWord
elif('class1' in msg and globals.EXAMPLE_READY and not globals.UPDATE_BG_DATA):
example = sound.get_spectrogram()
ai.addExample(example,1)
globals.TR_EXAMPLES += 1
led.LED.listen()
#Receive train command
elif('train' in msg):
globals.TRAIN = True
#Receive reset command
elif('reset' in msg):
globals.RESET = True;
connection.send_response() #tell client that we are reseting
ai.reset_model()
globals.RESET = False;
globals.TR_EXAMPLES = 0
#Toogle Alias on and off
elif('onoff' in msg):
if(stream.is_active()):
stream.stop_stream()
noise.stop()
else:
stream.start_stream()
noise.play()
#Receive is Button is pressed or released
if('btn_release' in msg):
print("released")
globals.BUTTON_PRESSED = False
elif('class1' in msg or 'class0' in msg):
globals.BUTTON_PRESSED = True
#Check if system is ready to predict
if(globals.TRAIN or globals.RESET or globals.BUTTON_PRESSED or globals.TRIGGERED):
globals.PREDICT = False
else:
globals.PREDICT = True
connection.send_response()
# End of socket
#====================================================#
# Main thread
def main_thread():
noise.play() #Start noise
ai.create_model() # setup keras model
connection.send_response() #Send system info to client
# variables to control timing between predictions
prev_timer = 0;
interval = 5;
# Program loop start here
#====================================================#
try:
while True:
while stream.is_active():
time.sleep(0.01)
led.LED.off()
current_sec = time.time()
# If the mic is triggered an spectogram is not done, make a row more.
if(globals.MIC_TRIGGER and not globals.EXAMPLE_READY):
sound.make_spectrogram()
if globals.PREDICT and globals.EXAMPLE_READY and not globals.TRAIN and not globals.RESET:
sample = sound.get_spectrogram()
print("get spectogram")
print(globals.EXAMPLE_READY)
if globals.HAS_BEEN_TRAINED: #if model has been trained then predict
globals.RESULT = ai.predict(sample).item()
print("GLOBAL RESULT: %d" %globals.RESULT)
if globals.RESULT == 1:
noise.stop()
wakeup.play()
globals.TRIGGERED = True
globals.PREDICT = False
prev_timer = current_sec
connection.send_response()
elif globals.TRAIN:
ai.train_model()
globals.PREDICT = True
globals.TRAIN = False
connection.send_response() #tell client that we are done training
else:
globals.RESULT = 0
if current_sec - prev_timer > interval:
if globals.TRIGGERED:
noise.play()
print("start noise")
led.LED.off()
globals.TRIGGERED = False;
globals.PREDICT = True
connection.send_response()
except (KeyboardInterrupt, SystemExit):
exit_handler()
# Setup
#====================================================#
globals.initialize()
stream = sound.initialize()
stream.start_stream() # start stream
# Setup and start main thread
thread = Thread(target=main_thread)
thread.daemon = True
thread.start()
print('')
print("============================================")
print("SERVER RUNNING ON: http://" + str(connection.HOST) + ":" + str(connection.PORT))
print("============================================")
print('')
# Start socket io
if __name__ == '__main__':
connection.socketio.run(connection.app, host=connection.HOST, port=connection.PORT, debug=False, log_output=False)
def exit_handler():
led.LED.off()
stream.stop_stream()
stream.close()
sound.pyaudio.terminate()
atexit.register(exit_handler)