micro_speech.py
# 本作品采用MIT许可证授权。
# 版权所有 (c) 2024 OpenMV LLC。保留所有权利。
# https://github.com/openmv/openmv/blob/master/LICENSE
#
# MicroSpeech模块专为微控制器平台上的实时音频处理和语音识别
# 而设计。它利用预训练模型进行音频预处理和
# 语音识别,专门针对检测“Yes”和“No”等关键词进行了优化。
import time
from ml.apps import MicroSpeech
def callback(label, scores):
print(f'\nHeard: "{label}" @{time.ticks_ms()}ms Scores: {scores}')
# 默认情况下,MicroSpeech对象分别使用内置的音频预处理器(浮点)和
# micro speech模块进行音频预处理和语音识别。
# 用户可以通过传入两个模型来覆盖两者:
# MicroSpeech(preprocessor=ml.Model(...), micro_speech=ml.Model(...), labels=["label",...])
speech = MicroSpeech()
# 启动音频流,并处理输入音频以识别语音命令。
# 如果传入了回调函数,listen()会无限循环,并在检测到关键词时
# 调用回调。另外,也可以带超时(毫秒)调用`listen()`,
# 若在检测到关键词前超时,则函数返回。
speech.listen(callback=callback, threshold=0.70)