wake_word_module/
├── __init__.py # 模块初始化文件
├── config.py # 配置类
├── audio_capture.py # 音频采集器
├── detector.py # 唤醒词检测器
├── example.py # 使用示例
├── test.py # 测试脚本
├── simple_test.py # 简单导入测试
├── requirements.txt # 依赖包
└── README.md # 详细文档
语音唤醒配置类,负责管理所有检测参数。
主要功能:
关键参数:
model_path: 模型文件目录sample_rate: 音频采样率(默认16000Hz)keywords_threshold: 检测阈值(0.1-1.0,越小越灵敏)keywords_score: 检测分数(1.0-10.0,影响准确率和延迟)detection_cooldown: 冷却时间(防止重复触发)音频采集器,负责从麦克风采集音频数据。
主要功能:
关键方法:
start(): 启动音频采集stop(): 停止音频采集add_audio_listener(): 添加音频监听器list_devices(): 列出可用设备(静态方法)唤醒词检测器,基于sherpa-onnx实现核心检测逻辑。
主要功能:
关键方法:
start(): 启动检测stop(): 停止检测pause(): 暂停检测resume(): 恢复检测on_detected(): 设置检测回调on_error(): 设置错误回调pip install -r requirements.txt
主要依赖:
numpy>=1.26.0 - 数值计算sherpa-onnx>=1.12.0 - 唤醒词检测引擎sounddevice>=0.4.4 - 音频设备访问将以下文件放入 models/ 目录:
encoder.onnx - 编码器模型decoder.onnx - 解码器模型joiner.onnx - 连接器模型tokens.txt - 词汇表keywords.txt - 唤醒词配置注意: 模型文件可以从原项目的 models/ 目录复制。
import asyncio
from wake_word_module import WakeWordDetector, AudioCapture, WakeWordConfig
async def main():
# 1. 创建配置
config = WakeWordConfig(
model_path="models",
sample_rate=16000,
keywords_threshold=0.2,
)
# 2. 创建音频采集器
audio_capture = AudioCapture(sample_rate=16000, channels=1)
# 3. 创建检测器
detector = WakeWordDetector(config)
# 4. 设置回调
def on_wake_word_detected(result, full_text):
print(f"检测到唤醒词: {result}")
detector.on_detected(on_wake_word_detected)
# 5. 连接并启动
audio_capture.add_audio_listener(detector)
await audio_capture.start()
await detector.start()
# 6. 保持运行
try:
while True:
await asyncio.sleep(1)
except KeyboardInterrupt:
print("停止程序...")
# 7. 清理资源
await detector.stop()
await audio_capture.stop()
asyncio.run(main())
# 运行完整示例
python example.py
# 运行测试脚本
python test.py
# 运行简单导入测试
python simple_test.py
# 更灵敏的检测(可能误报)
config = WakeWordConfig(
model_path="models",
keywords_threshold=0.15, # 降低阈值
keywords_score=1.5, # 降低分数
)
# 更准确的检测(可能漏报)
config = WakeWordConfig(
model_path="models",
keywords_threshold=0.3, # 提高阈值
keywords_score=2.5, # 提高分数
)
# 快速响应(可能重复触发)
config = WakeWordConfig(
model_path="models",
detection_cooldown=0.5, # 0.5秒冷却
)
# 防止重复触发
config = WakeWordConfig(
model_path="models",
detection_cooldown=2.0, # 2秒冷却
)
# 先查看可用设备
AudioCapture.list_devices()
# 选择特定设备(假设设备ID为1)
audio_capture = AudioCapture(
sample_rate=16000,
channels=1,
device_id=1,
)
# 增加线程数(多核CPU)
config = WakeWordConfig(
model_path="models",
num_threads=8, # 使用8个线程
)
# 使用GPU加速(如果可用)
config = WakeWordConfig(
model_path="models",
provider="cuda", # 使用CUDA
)
从原项目 py-xiaozhi-main/models/ 目录复制所有模型文件到 wake_word_module/models/。
keywords_threshold 值keywords.txt 中的唤醒词配置编辑 models/keywords.txt 文件,按照格式添加新的唤醒词。
支持 Windows、macOS、Linux,需要相应的音频驱动。
在普通PC上,CPU占用率约5-15%,延迟约100-200ms。
# 在你的项目中导入
from wake_word_module import WakeWordDetector, AudioCapture, WakeWordConfig
# 初始化并使用
config = WakeWordConfig(model_path="path/to/models")
detector = WakeWordDetector(config)
detector.on_detected(your_callback_function)
# 将模块打包
cd wake_word_module
python setup.py sdist
# 安装到其他项目
pip install dist/wake_word_module-1.0.0.tar.gz
本模块从 py-xiaozhi 项目提取,遵循原项目的许可证。