littlebot
Published on 2025-04-13 / 0 Visits
0

【源码】基于Python的实时语音识别系统

项目简介

本项目是基于Python的实时语音识别系统,借助Sherpa - ncnn库达成对音频的实时语音识别。系统既能从麦克风实时获取音频输入并输出识别结果,也支持从文件读取音频数据进行识别。

项目的主要特性和功能

  1. 实时语音识别:可从麦克风实时获取音频输入,利用Sherpa - ncnn库进行实时语音识别。
  2. 文件识别:支持用户上传音频文件并对其进行语音识别。
  3. 端点检测:具备端点检测功能,能自动检测语音的起始和结束点。
  4. 多语言支持:语音识别模型支持中英文等多种语言。
  5. 灵活性:支持使用不同的预训练模型进行语音识别,用户可按需选择。

安装使用步骤

1. 环境准备

  • 安装Python(推荐Python 3.7及以上版本)
  • 安装sounddevice库(用于音频输入)
  • 安装sherpa_ncnn库(用于语音识别)

2. 导入库

python import sounddevice as sd import sherpa_ncnn

3. 创建识别器

```python def create_recognizer(): model_config = sherpa_ncnn.TransducerModelConfig(...) feature_config = sherpa_ncnn.FeatureConfig(...) decoder_config = sherpa_ncnn.TransducerDecoderConfig(...)

online_recognizer = sherpa_ncnn.create_online_recognizer(model_config, feature_config, decoder_config)
return online_recognizer

```

4. 使用识别器

```python def main(): recognizer = create_recognizer()

stream = sd.InputStream(callback=recognizer.accept_waveform, channels=1)
stream.start()

try:
    while True:
        if recognizer.is_ready():
            result = recognizer.decode()
            if result.is_endpoint:
                print(result.text)
                recognizer.reset()
except KeyboardInterrupt:
    stream.stop()

recognizer.destroy()

if name == "main": main() ``` 注意:运行代码前,需下载并放置正确的模型文件,模型文件可从Sherpa的官方网站获取。

下载地址

点击下载 【提取码: 4003】【解压密码: www.makuang.net】