-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsensors_google.py
386 lines (278 loc) · 11.9 KB
/
sensors_google.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
from __future__ import division
from phidias.Types import *
import threading
import sys
import configparser
config = configparser.ConfigParser()
config.read('config.ini')
LOG_ACTIVE = config.getboolean('AGENT', 'LOG_ACTIVE')
class TIMEOUT(Reactor): pass
class STT(Reactor): pass
class HOTWORD_DETECTED(Reactor): pass
# ----------- Google section
from google.cloud import speech
import pyaudio
from six.moves import queue
import time
# Audio recording parameters
RATE = 16000
CHUNK = int(RATE / 10) # 100ms
# Audio recording parameters
STREAMING_LIMIT = 240000 # 4 minutes
SAMPLE_RATE = 16000
CHUNK_SIZE = int(SAMPLE_RATE / 10) # 100ms
RED = "\033[0;31m"
GREEN = "\033[0;32m"
YELLOW = "\033[0;33m"
CWHITE = '\33[0m'
BLUE = '\33[34m'
def get_current_time():
"""Return Current Time in MS."""
return int(round(time.time() * 1000))
class ResumableMicrophoneStream:
"""Opens a recording stream as a generator yielding the audio chunks."""
def __init__(self, rate, chunk_size):
self._rate = rate
self.chunk_size = chunk_size
self._num_channels = 1
self._buff = queue.Queue()
self.closed = True
self.start_time = get_current_time()
self.restart_counter = 0
self.audio_input = []
self.last_audio_input = []
self.result_end_time = 0
self.is_final_end_time = 0
self.final_request_end_time = 0
self.bridging_offset = 0
self.last_transcript_was_final = False
self.new_stream = True
self._audio_interface = pyaudio.PyAudio()
self._audio_stream = self._audio_interface.open(
format=pyaudio.paInt16,
channels=self._num_channels,
rate=self._rate,
input=True,
frames_per_buffer=self.chunk_size,
# Run the audio stream asynchronously to fill the buffer object.
# This is necessary so that the input device's buffer doesn't
# overflow while the calling thread makes network requests, etc.
stream_callback=self._fill_buffer,
)
def __enter__(self):
self.closed = False
return self
def __exit__(self, type, value, traceback):
self._audio_stream.stop_stream()
self._audio_stream.close()
self.closed = True
# Signal the generator to terminate so that the client's
# streaming_recognize method will not block the process termination.
self._buff.put(None)
self._audio_interface.terminate()
def _fill_buffer(self, in_data, *args, **kwargs):
"""Continuously collect data from the audio stream, into the buffer."""
self._buff.put(in_data)
return None, pyaudio.paContinue
def generator(self):
"""Stream Audio from microphone to API and to local buffer"""
while not self.closed:
data = []
if self.new_stream and self.last_audio_input:
chunk_time = STREAMING_LIMIT / len(self.last_audio_input)
if chunk_time != 0:
if self.bridging_offset < 0:
self.bridging_offset = 0
if self.bridging_offset > self.final_request_end_time:
self.bridging_offset = self.final_request_end_time
chunks_from_ms = round(
(self.final_request_end_time - self.bridging_offset)
/ chunk_time
)
self.bridging_offset = round(
(len(self.last_audio_input) - chunks_from_ms) * chunk_time
)
for i in range(chunks_from_ms, len(self.last_audio_input)):
data.append(self.last_audio_input[i])
self.new_stream = False
# Use a blocking get() to ensure there's at least one chunk of
# data, and stop iteration if the chunk is None, indicating the
# end of the audio stream.
chunk = self._buff.get()
self.audio_input.append(chunk)
if chunk is None:
return
data.append(chunk)
# Now consume whatever other data's still buffered.
while True:
try:
chunk = self._buff.get(block=False)
if chunk is None:
return
data.append(chunk)
self.audio_input.append(chunk)
except queue.Empty:
break
yield b"".join(data)
client = speech.SpeechClient()
config = speech.RecognitionConfig(
encoding=speech.RecognitionConfig.AudioEncoding.LINEAR16,
sample_rate_hertz=SAMPLE_RATE,
language_code="en-US",
max_alternatives=1,
enable_automatic_punctuation=True
)
streaming_config = speech.StreamingRecognitionConfig(
config=config, interim_results=True
)
# ----------- Porcupine section
import os
import struct
from datetime import datetime
import pvporcupine
# keywords available:
# alexa, americano, blueberry, bumblebee, computer, grapefruit, grasshopper, hey google, hey siri, jarvis, ok google, picovoice, porcupine, terminator
# -----------------------------------------------------------------------
class HotwordDetect(Sensor):
def on_start(self):
self.running = True
print("\nStarting Hotword detection...")
self.keywords = ["caspar"]
keyword_paths = [pvporcupine.KEYWORD_PATHS[x] for x in self.keywords]
self.sensitivities = [0.5] * len(keyword_paths)
self.keywords = list()
for x in keyword_paths:
self.keywords.append(os.path.basename(x).replace('.ppn', '').split('_')[0])
self.porcupine = pvporcupine.create(
library_path=pvporcupine.LIBRARY_PATH,
model_path=pvporcupine.MODEL_PATH,
keyword_paths=keyword_paths,
sensitivities=self.sensitivities)
self.pa = pyaudio.PyAudio()
self.audio_stream = self.pa.open(
rate=self.porcupine.sample_rate,
channels=1,
format=pyaudio.paInt16,
input=True,
frames_per_buffer=self.porcupine.frame_length,
input_device_index=None)
def on_stop(self):
print("\nStopping Hotword detection...")
self.running = False
def on_restart(self):
print("\nRestarting Hotword detection...")
self.running = True
def sense(self):
sys.stdout.write(BLUE)
print('\nListening {')
for keyword, sensitivity in zip(self.keywords, self.sensitivities):
print(' %s (%.2f)' % (keyword, sensitivity))
print('}')
while self.running:
pcm = self.audio_stream.read(self.porcupine.frame_length)
pcm = struct.unpack_from("h" * self.porcupine.frame_length, pcm)
result = self.porcupine.process(pcm)
if result >= 0:
print('[%s] Detected %s' % (str(datetime.now()), self.keywords[result]))
sys.stdout.write(CWHITE)
self.assert_belief(HOTWORD_DETECTED("ON"))
self.running = False
break
self.audio_stream.close()
self.pa.terminate()
self.porcupine.delete()
class UtteranceDetect(Sensor):
def on_start(self):
self.running = True
self.mic_manager = ResumableMicrophoneStream(SAMPLE_RATE, CHUNK_SIZE)
print("\nStarting utterance detection...")
def on_stop(self):
print("\n\n --- Stopping utterance detection...")
self.running = False
self.mic_manager.closed = True
def on_restart(self):
print("\nRestarting utterance detection...")
self.running = True
self.mic_manager.closed = False
def sense(self):
while self.running:
with self.mic_manager as stream:
while stream.closed is False:
start_time = time.time()
sys.stdout.write(YELLOW)
sys.stdout.write(
"\n" + str(STREAMING_LIMIT * stream.restart_counter) + ": NEW REQUEST\n")
stream.audio_input = []
audio_generator = stream.generator()
requests = (speech.StreamingRecognizeRequest(audio_content=content) for content in audio_generator)
responses = client.streaming_recognize(streaming_config, requests)
for response in responses:
if get_current_time() - stream.start_time > STREAMING_LIMIT:
stream.start_time = get_current_time()
break
if not response.results:
continue
result = response.results[0]
if not result.alternatives:
continue
transcript = result.alternatives[0].transcript
result_seconds = 0
result_micros = 0
if result.result_end_time.seconds:
result_seconds = result.result_end_time.seconds
if result.result_end_time.microseconds:
result_micros = result.result_end_time.microseconds
stream.result_end_time = int((result_seconds * 1000) + (result_micros / 1000))
corrected_time = (stream.result_end_time - stream.bridging_offset + (STREAMING_LIMIT * stream.restart_counter))
# Display interim results, but with a carriage return at the end of the
# line, so subsequent lines will overwrite them.
if result.is_final:
detection_time = time.time() - start_time
print("\nSTT Detection time: ", detection_time)
sys.stdout.write(GREEN)
sys.stdout.write("\033[K")
sys.stdout.write(str(corrected_time) + ": " + transcript + "\n")
stream.is_final_end_time = stream.result_end_time
stream.last_transcript_was_final = True
stream.closed = True
self.mic_manager.closed = True
self.running = False
# changing char/snipplets not dealing with the parsing
SWAP_STR = [["Turn on", "Change"]]
utterance = transcript.strip()
for s in SWAP_STR:
utterance = utterance.replace(s[0], s[1])
if LOG_ACTIVE:
with open("log.txt", "a") as myfile:
myfile.write("\n\nGoogle STT: " + utterance)
myfile.write("\nDetection time: "+str(detection_time))
self.assert_belief(STT(utterance))
else:
sys.stdout.write(RED)
sys.stdout.write("\033[K")
sys.stdout.write(str(corrected_time) + ": " + transcript + "\r")
stream.last_transcript_was_final = False
class Timer(Sensor):
def on_start(self, uTimeout):
evt = threading.Event()
self.event = evt
self.timeout = uTimeout()
self.do_restart = False
def on_restart(self, uTimeout):
self.do_restart = True
self.event.set()
def on_stop(self):
self.do_restart = False
self.event.set()
def sense(self):
while True:
self.event.wait(self.timeout)
self.event.clear()
if self.do_restart:
self.do_restart = False
continue
if self.stopped:
return
else:
self.assert_belief(TIMEOUT("ON"))
return