当前位置: 首页 > news >正文

如何构建FunASR的本地语音识别服务

FunASR 简介

FunASR 是阿里巴巴达摩院开源的高性能语音识别工具包,支持离线识别实时流式识别两种模式。其核心特点包括:

  • 支持多种语音任务:ASR(自动语音识别)、VAD(语音活动检测)、标点恢复、关键词检测等。
  • 提供预训练模型:覆盖中文、英文等多语言,支持不同场景(通用、会议、直播等)。
  • 支持多种部署方式:本地 Python、Docker 容器、ONNX 推理优化等。
  • 开源地址:GitHub - FunASR

一、本地 Python 部署

1. 克隆仓库
git clone https://github.com/modelscope/FunASR.git cd FunASR
2. 创建环境并安装依赖
# 创建 Conda 环境 conda create -n funasr python=3.9 conda activate funasr # 安装 PyTorch(CUDA 11.8 版本,CPU 版本需移除 `-c nvidia`) conda install pytorch==2.3.1 torchvision==0.18.1 torchaudio==2.3.1 pytorch-cuda=11.8 -c pytorch -c nvidia # 安装 FunASR 及相关库 pip install -U funasr pip install -U modelscope huggingface huggingface_hub
3. 模型使用示例
from funasr import AutoModel # 加载 ASR 模型(中文通用) model = AutoModel( model="damo/speech_paraformer-large_asr_nat-zh-cn-16k-common-vocab8404-pytorch", model_revision="v2.0.4" ) # 加载标点恢复模型(可选) punc_model = AutoModel( model="damo/punc_ct-transformer_zh-cn-common-vocab272727-pytorch", model_revision="v1.0.4" ) # 语音识别示例(输入为 16kHz 采样率的 WAV 文件) audio_file = "your_audio.wav" result = model(audio_in=audio_file) # 添加标点(可选) if result and punc_model: text = result[0]["text"] result_with_punc = punc_model(text) print("识别结果(带标点):", result_with_punc[0]["text"])

二、Docker 容器部署

1. 拉取 Docker 镜像
# CPU 版本 docker pull registry.cn-hangzhou.aliyuncs.com/funasr_repo/funasr:funasr-runtime-sdk-cpu-0.4.6 # GPU 版本(需 NVIDIA 驱动和 Docker 支持) docker pull registry.cn-hangzhou.aliyuncs.com/funasr_repo/funasr:funasr-runtime-sdk-gpu-0.4.6
2. 创建模型目录并启动容器
# 创建模型存储目录 mkdir -p ./funasr-runtime-resources/models # 启动 CPU 容器(映射端口 10095) docker run -p 10095:10095 -it --privileged=true \ -v $PWD/funasr-runtime-resources/models:/workspace/models \ registry.cn-hangzhou.aliyuncs.com/funasr_repo/funasr:funasr-runtime-sdk-cpu-0.4.6
3. 启动服务(容器内执行)
cd FunASR/runtime # 启动完整服务(包含 VAD、ASR、标点恢复、语言模型等) nohup bash run_server.sh \ --download-model-dir /workspace/models \ --vad-dir damo/speech_fsmn_vad_zh-cn-16k-common-onnx \ --model-dir damo/speech_paraformer-large-vad-punc_asr_nat-zh-cn-16k-common-vocab8404-onnx \ --punc-dir damo/punc_ct-transformer_zh-cn-common-vad_realtime-vocab272727-onnx \ --lm-dir damo/speech_ngram_lm_zh-cn-ai-wesp-fst \ --itn-dir thuduj12/fst_itn_zh \ --hotword /workspace/models/hotwords.txt > log.txt 2>&1 & # 关闭 SSL(添加 --certfile 0 参数) nohup bash run_server.sh ... --certfile 0 > log.txt 2>&1 &

三、基于 FastAPI 的 Web 服务实现

1. 服务端代码(asr_service.py
from fastapi import FastAPI, File, UploadFile, HTTPException from fastapi.responses import JSONResponse import uvicorn import wave import numpy as np from funasr import AutoModel app = FastAPI(title="FunASR 语音识别服务") # 初始化模型 try: model = AutoModel( model="damo/speech_paraformer-large_asr_nat-zh-cn-16k-common-vocab8404-pytorch", model_revision="v2.0.4" ) punc_model = AutoModel( model="damo/punc_ct-transformer_zh-cn-common-vocab272727-pytorch", model_revision="v1.0.4" ) except Exception as e: raise HTTPException(status_code=500, detail=f"模型加载失败: {str(e)}") @app.post("/asr") async def recognize_audio(file: UploadFile = File(...)): try: # 保存临时文件 temp_file = "temp_audio.wav" with open(temp_file, "wb") as f: f.write(await file.read()) # 检查音频格式(需 16kHz 采样率) with wave.open(temp_file, "rb") as wf: sample_rate = wf.getframerate() if sample_rate != 16000: return JSONResponse( content={"error": "请上传 16kHz 采样率的 WAV 文件"}, status_code=400 ) # 执行识别 result = model(audio_in=temp_file) # 添加标点(可选) if result and punc_model: text = result[0]["text"] result_with_punc = punc_model(text) result[0]["text_with_punctuation"] = result_with_punc[0]["text"] return {"result": result} except Exception as e: return JSONResponse(content={"error": str(e)}, status_code=500) finally: # 清理临时文件 import os if os.path.exists(temp_file): os.remove(temp_file) if __name__ == "__main__": uvicorn.run(app, host="0.0.0.0", port=8000)
2. 启动服务
python asr_service.py
3. 客户端调用示例
import requests url = "http://localhost:8000/asr" audio_file = "test.wav" # 16kHz WAV 文件 with open(audio_file, "rb") as f: response = requests.post(url, files={"file": f}) if response.status_code == 200: print("识别结果:", response.json()) else: print(f"错误: {response.status_code}, {response.text}")

四、性能优化建议

  1. 模型选择

    • 轻量级模型:damo/speech_paraformer-tiny_asr_nat-zh-cn-16k-common(更快)
    • 通用模型:damo/speech_paraformer-large_asr_nat-zh-cn-16k-common(更准)
  2. 模型量化
    使用 ONNX 或 TensorRT 对模型进行量化,减少内存占用并提升推理速度。

  3. 批处理
    对多个音频文件进行批量处理,提高吞吐量。

  4. 缓存机制
    对相同音频的识别结果进行缓存,避免重复计算。

五、常见问题及解决方案

  1. CUDA 兼容性问题

    • 确保 PyTorch 版本与本地 CUDA 驱动匹配。
    • CPU 环境使用无 CUDA 的 PyTorch 版本。
  2. 模型下载失败

    • 手动下载模型并放置到指定路径。
    • 配置网络代理或使用国内镜像源。
  3. Docker 权限问题

    • 添加--privileged=true参数或调整容器权限。

通过以上步骤,可以构建一个完整的本地语音识别服务,支持实时语音转文字功能。

启动Fay数字人的语音服务方法如下:

安装启动funasr

1、进入Fay/asr/funasr代码目录

2、安装依赖

在终端或cmd中分别执行以下命令

pip install torch pip install modelscope pip install testresources pip install websockets==10.4 pip install torchaudio pip install FunASR

3、终端或cmd启动funasr服务端(保持启动)

python -u ASR_server.py --host "0.0.0.0" --port 10197 --ngpu 1

说明:--ngpu 0——CPU运行

--ngpu 1——GPU运行

启动后,默认端口为10197,系统配置代码中调用方式为:

local_asr_ip =(本地语音服务的电脑IP) local_asr_port = 10197
http://www.jsqmd.com/news/240979/

相关文章:

  • 给定一个二叉树,求其最近公共祖先
  • 「测试面试官手记」海投三个月零面试,一招拿到了心仪Offer!
  • Arduino下载安装教程:板卡支持包添加方法
  • 图网络的度矩阵D/邻接矩阵A/拉普拉斯矩阵L以及图中的节点如何各自保存更新节点特征
  • 车载电子PCB工艺选型要求:项目应用解析
  • 自动驾驶场景下的Android HMI开发:资深工程师职位深度解析
  • XML处理:提取唯一ID的XSLT优化
  • 揭秘大模型 “胡说八道”:幻觉产生的底层原理与规避逻辑
  • 从文本到图像:多模态大模型跨域理解的核心技术原理
  • C语言中的逻辑与运算误区
  • 通过SMBus读取电源状态寄存器:操作指南
  • GeoPandas绘图技巧:如何优雅地在地图上标注县城信息
  • 别让错招毁了团队:入职背景调查,为企业把好人才第一关
  • 数据分析:自动计算近五个月平均值
  • 核心要点:如何判断是STLink损坏还是配置错误
  • AWS云从业者认证(AWS Certified Cloud Practitioner)
  • 深入浅出:Java邮件发送中的换行问题
  • Proteus仿真环境下单片机定时器配置实战案例
  • 深入理解XPath文本节点的选取
  • STLink与STM32怎么接线?一文说清基本连接步骤
  • 商标被抢注、许可失控?这两个隐形坑,拖垮不少中小企业
  • Spring Boot动态数据源实战,让数据库连接“随用随取”
  • 工业设备数据采集:SerialPort通信配置深度剖析
  • 图解Multisim主数据库配置流程:初学者轻松上手
  • 密度敏感哈希(DSH)学习算法详解
  • JFlash下载与Bootloader配合烧录技巧
  • STM32H7系列(MPU Cache)
  • 基于STM32的工业touch驱动开发操作指南
  • STLink驱动安装超详细版:从下载到配置全流程
  • 基于STM32的I2C时序分析:核心要点一文说清