ml.apps — ML Apps¶
The ml.apps module contains high-level ML application classes built on top of ml.Model.
class MicroSpeech – Speech Recognition¶
The MicroSpeech object recognizes simple spoken words using the MicroSpeech model from
TensorFlow Lite for Microcontrollers. The default model recognizes "Yes" and "No".
- class ml.apps.MicroSpeech(preprocessor: ml.Model = None, micro_speech: ml.Model = None, labels: list[str] = None, **kwargs)¶
Creates a MicroSpeech object.
preprocessoris the audio preprocessorml.Model. IfNone,/rom/audio_preprocessor.tfliteis loaded.micro_speechis the speech recognitionml.Model. IfNone,/rom/micro_speech.tfliteis loaded.labelsis a list of label strings matching the model output categories. IfNone, the labels are taken frommicro_speech.labels.Any additional keyword arguments are forwarded to
audio.init()(the audio peripheral is initialized withchannels=1,frequency=16000, andsamples=320).- audio_callback(buf: bytes) None¶
Internal audio streaming callback. Appends new samples from
bufinto the rolling audio buffer, updates the spectrogram by running thepreprocessormodel on the latest window, and updates the prediction history by running themicro_speechmodel on the spectrogram. Not normally called directly.
- start_audio_streaming() None¶
Clears the spectrogram and prediction history, then starts audio streaming with
MicroSpeech.audio_callbackas the callback. No-op if streaming is already started.
- listen(timeout: int = 0, callback: callable = None, threshold: float = 0.65, filter: list[str] = [Yes, No]) tuple[str, numpy.ndarray]¶
Listens for a spoken word and returns a tuple of
(label, average_scores)once a label whose averaged score is abovethresholdand is contained infilteris detected. CallsMicroSpeech.start_audio_streamingif not already streaming.timeoutis the maximum time in milliseconds to listen. If0, listens indefinitely until a word is recognized. If-1, runs in non-blocking mode and returns immediately with(None, average_scores)if no word is recognized; audio streaming is left running. For any positive value, listens for that many milliseconds and then returns(None, average_scores)on timeout.callbackis an optional callable invoked ascallback(label, average_scores)when a word is recognized instead of returning. Combined withtimeout=0, this allows continuous recognition.thresholdis the minimum averaged confidence required to accept a recognition.filteris the list of label strings to accept. Recognitions outside this list are ignored.