mirror of
https://github.com/openmv/openmv.git
synced 2025-11-04 14:49:50 +08:00
scripts/libraries: Add a non-blocking mode to MicroSpeech.
This commit is contained in:
parent
976aee80ae
commit
fab5368329
@ -61,6 +61,7 @@ class MicroSpeech:
|
|||||||
self.audio_buffer = np.zeros((1, _SAMPLES_PER_STEP * 3), dtype=np.int16)
|
self.audio_buffer = np.zeros((1, _SAMPLES_PER_STEP * 3), dtype=np.int16)
|
||||||
self.spectrogram = np.zeros((1, _SLICE_COUNT * _SLICE_SIZE), dtype=np.int8)
|
self.spectrogram = np.zeros((1, _SLICE_COUNT * _SLICE_SIZE), dtype=np.int8)
|
||||||
self.pred_history = np.zeros((_AVERAGE_WINDOW_SAMPLES, _CATEGORY_COUNT), dtype=np.float)
|
self.pred_history = np.zeros((_AVERAGE_WINDOW_SAMPLES, _CATEGORY_COUNT), dtype=np.float)
|
||||||
|
self.audio_started = False
|
||||||
audio.init(channels=1, frequency=_AUDIO_FREQUENCY, gain_db=24, samples=_SAMPLES_PER_STEP * 2)
|
audio.init(channels=1, frequency=_AUDIO_FREQUENCY, gain_db=24, samples=_SAMPLES_PER_STEP * 2)
|
||||||
|
|
||||||
def audio_callback(self, buf):
|
def audio_callback(self, buf):
|
||||||
@ -78,11 +79,19 @@ class MicroSpeech:
|
|||||||
self.pred_history = np.roll(self.pred_history, -1, axis=0)
|
self.pred_history = np.roll(self.pred_history, -1, axis=0)
|
||||||
self.pred_history[-1] = self.micro_speech.predict(self.spectrogram)[0]
|
self.pred_history[-1] = self.micro_speech.predict(self.spectrogram)[0]
|
||||||
|
|
||||||
def listen(self, timeout=0, callback=None, threshold=0.65, filter=["Yes", "No"]):
|
def start_audio_streaming(self):
|
||||||
|
if self.audio_started is False:
|
||||||
self.spectrogram[:] = 0
|
self.spectrogram[:] = 0
|
||||||
self.pred_history[:] = 0
|
self.pred_history[:] = 0
|
||||||
# Start audio streaming
|
|
||||||
audio.start_streaming(self.audio_callback)
|
audio.start_streaming(self.audio_callback)
|
||||||
|
self.audio_started = True
|
||||||
|
|
||||||
|
def stop_audio_streaming(self):
|
||||||
|
audio.stop_streaming()
|
||||||
|
self.audio_started = False
|
||||||
|
|
||||||
|
def listen(self, timeout=0, callback=None, threshold=0.65, filter=["Yes", "No"]):
|
||||||
|
self.start_audio_streaming()
|
||||||
stat_ms = time.ticks_ms()
|
stat_ms = time.ticks_ms()
|
||||||
while True:
|
while True:
|
||||||
average_scores = np.mean(self.pred_history, axis=0)
|
average_scores = np.mean(self.pred_history, axis=0)
|
||||||
@ -93,10 +102,12 @@ class MicroSpeech:
|
|||||||
self.pred_history[:] = 0
|
self.pred_history[:] = 0
|
||||||
self.spectrogram[:] = 0
|
self.spectrogram[:] = 0
|
||||||
if callback is None:
|
if callback is None:
|
||||||
audio.stop_streaming()
|
if timeout != -1: # non-blocking mode
|
||||||
|
self.stop_audio_streaming()
|
||||||
return (label, average_scores)
|
return (label, average_scores)
|
||||||
callback(label, average_scores)
|
callback(label, average_scores)
|
||||||
|
if timeout == -1: # non-blocking mode
|
||||||
|
return (None, average_scores)
|
||||||
if timeout != 0 and (time.ticks_ms() - stat_ms) > timeout:
|
if timeout != 0 and (time.ticks_ms() - stat_ms) > timeout:
|
||||||
audio.stop_streaming()
|
self.stop_audio_streaming()
|
||||||
return (None, None)
|
|
||||||
time.sleep_ms(1)
|
time.sleep_ms(1)
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user