FunASR 是由阿里巴巴达摩院开发的一个开源自动语音识别(ASR)系统。它旨在提供一个易用、高效的平台,帮助开发者和研究人员快速构建自己的语音识别应用。该项目结合了深度学习技术和大规模数据处理能力,实现了高精度的实时语音转文本功能。

核心功能

  • FunASR是一个基础语音识别工具包,提供多种功能,包括语音识别(ASR)、语音端点检测(VAD)、标点恢复、语言模型、说话人验证、说话人分离和多人对话语音识别等。FunASR提供了便捷的脚本和教程,支持预训练好的模型的推理与微调。
  • 我们在ModelScopehuggingface上发布了大量开源数据集或者海量工业数据训练的模型,可以通过我们的模型仓库了解模型的详细信息。代表性的Paraformer非自回归端到端语音识别模型具有高精度、高效率、便捷部署的优点,支持快速构建语音识别服务,详细信息可以阅读(服务部署文档)。

安装教程

  • 安装funasr之前,确保已经安装了下面依赖环境:

    python>=3.8
    torch>=1.13
    torchaudio
  • pip安装

    pip3 install -U funasr
  • 或者从源代码安装

    git clone https://github.com/alibaba/FunASR.git && cd FunASR
    pip3 install -e ./

如果需要使用工业预训练模型,安装modelscope与huggingface_hub(可选)

pip3 install -U modelscope huggingface huggingface_hub

模型仓库

FunASR开源了大量在工业数据上预训练模型,您可以在模型许可协议下自由使用、复制、修改和分享FunASR模型,下面列举代表性的模型,更多模型请参考 模型仓库

(注:⭐ 表示ModelScope模型仓库,🤗 表示Huggingface模型仓库,🍀表示OpenAI模型仓库)

模型名字 任务详情 训练数据 参数量
SenseVoiceSmall
( 🤗 )
多种语音理解能力,涵盖了自动语音识别(ASR)、语言识别(LID)、情感识别(SER)以及音频事件检测(AED) 400000小时,中文 330M
paraformer-zh
( 🤗 )
语音识别,带时间戳输出,非实时 60000小时,中文 220M
paraformer-zh-streaming
( 🤗 )
语音识别,实时 60000小时,中文 220M
paraformer-en
( 🤗 )
语音识别,非实时 50000小时,英文 220M
conformer-en
( 🤗 )
语音识别,非实时 50000小时,英文 220M
ct-punc
( 🤗 )
标点恢复 100M,中文与英文 290M
fsmn-vad
( 🤗 )
语音端点检测,实时 5000小时,中文与英文 0.4M
fa-zh
( 🤗 )
字级别时间戳预测 50000小时,中文 38M
cam++
( 🤗 )
说话人确认/分割 5000小时 7.2M
Whisper-large-v3
( 🍀 )
语音识别,带时间戳输出,非实时 多语言 1550 M
Qwen-Audio
( 🤗 )
音频文本多模态大模型(预训练) 多语言 8B
Qwen-Audio-Chat
( 🤗 )
音频文本多模态大模型(chat版本) 多语言 8B
emotion2vec+large
( 🤗 )
情感识别模型 40000小时,4种情感类别 300M

快速开始

下面为快速上手教程,测试音频(中文英文

可执行命令行

funasr ++model=paraformer-zh ++vad_model="fsmn-vad" ++punc_model="ct-punc" ++input=asr_example_zh.wav

注:支持单条音频文件识别,也支持文件列表,列表为kaldi风格wav.scp:wav_id wav_path

非实时语音识别

from funasr import AutoModel
# paraformer-zh is a multi-functional asr model
# use vad, punc, spk or not as you need
model = AutoModel(model="paraformer-zh",  vad_model="fsmn-vad", punc_model="ct-punc", 
                  # spk_model="cam++"
                  )
res = model.generate(input=f"{model.model_path}/example/asr_example.wav", 
            batch_size_s=300, 
            hotword='魔搭')
print(res)

注:hub:表示模型仓库,ms为选择modelscope下载,hf为选择huggingface下载。

实时语音识别

from funasr import AutoModel

chunk_size = [0, 10, 5] #[0, 10, 5] 600ms, [0, 8, 4] 480ms
encoder_chunk_look_back = 4 #number of chunks to lookback for encoder self-attention
decoder_chunk_look_back = 1 #number of encoder chunks to lookback for decoder cross-attention

model = AutoModel(model="paraformer-zh-streaming")

import soundfile
import os

wav_file = os.path.join(model.model_path, "example/asr_example.wav")
speech, sample_rate = soundfile.read(wav_file)
chunk_stride = chunk_size[1] * 960 # 600ms

cache = {}
total_chunk_num = int(len((speech)-1)/chunk_stride+1)
for i in range(total_chunk_num):
    speech_chunk = speech[i*chunk_stride:(i+1)*chunk_stride]
    is_final = i == total_chunk_num - 1
    res = model.generate(input=speech_chunk, cache=cache, is_final=is_final, chunk_size=chunk_size, encoder_chunk_look_back=encoder_chunk_look_back, decoder_chunk_look_back=decoder_chunk_look_back)
    print(res)

注:chunk_size为流式延时配置,[0,10,5]表示上屏实时出字粒度为10*60=600ms,未来信息为5*60=300ms。每次推理输入为600ms(采样点数为16000*0.6=960),输出为对应文字,最后一个语音片段输入需要设置is_final=True来强制输出最后一个字。

更多例子

语音端点检测(非实时)

from funasr import AutoModel

model = AutoModel(model="fsmn-vad")

wav_file = f"{model.model_path}/example/vad_example.wav"
res = model.generate(input=wav_file)
print(res)

注:VAD模型输出格式为:[[beg1, end1], [beg2, end2], .., [begN, endN]],其中begN/endN表示第N个有效音频片段的起始点/结束点,
单位为毫秒。

语音端点检测(实时)

from funasr import AutoModel

chunk_size = 200 # ms
model = AutoModel(model="fsmn-vad")

import soundfile

wav_file = f"{model.model_path}/example/vad_example.wav"
speech, sample_rate = soundfile.read(wav_file)
chunk_stride = int(chunk_size * sample_rate / 1000)

cache = {}
total_chunk_num = int(len((speech)-1)/chunk_stride+1)
for i in range(total_chunk_num):
    speech_chunk = speech[i*chunk_stride:(i+1)*chunk_stride]
    is_final = i == total_chunk_num - 1
    res = model.generate(input=speech_chunk, cache=cache, is_final=is_final, chunk_size=chunk_size)
    if len(res[0]["value"]):
        print(res)

注:流式VAD模型输出格式为4种情况:

  • [[beg1, end1], [beg2, end2], .., [begN, endN]]:同上离线VAD输出结果。
  • [[beg, -1]]:表示只检测到起始点。
  • [[-1, end]]:表示只检测到结束点。
  • []:表示既没有检测到起始点,也没有检测到结束点

输出结果单位为毫秒,从起始点开始的绝对时间。

标点恢复

from funasr import AutoModel

model = AutoModel(model="ct-punc")

res = model.generate(input="那今天的会就到这里吧 happy new year 明年见")
print(res)

时间戳预测

from funasr import AutoModel

model = AutoModel(model="fa-zh")

wav_file = f"{model.model_path}/example/asr_example.wav"
text_file = f"{model.model_path}/example/text.txt"
res = model.generate(input=(wav_file, text_file), data_type=("sound", "text"))
print(res)

情感识别

from funasr import AutoModel

model = AutoModel(model="emotion2vec_plus_large")

wav_file = f"{model.model_path}/example/test.wav"

res = model.generate(wav_file, output_dir="./outputs", granularity="utterance", extract_embedding=False)
print(res)

更详细(教程文档),
更多(模型示例

导出ONNX

从命令行导出

funasr-export ++model=paraformer ++quantize=false

从Python导出

from funasr import AutoModel

model = AutoModel(model="paraformer")

res = model.export(quantize=False)

测试ONNX

# pip3 install -U funasr-onnx
from funasr_onnx import Paraformer
model_dir = "damo/speech_paraformer-large_asr_nat-zh-cn-16k-common-vocab8404-pytorch"
model = Paraformer(model_dir, batch_size=1, quantize=True)

wav_path = ['~/.cache/modelscope/hub/damo/speech_paraformer-large_asr_nat-zh-cn-16k-common-vocab8404-pytorch/example/asr_example.wav']

result = model(wav_path)
print(result)

更多例子请参考 样例

服务部署

FunASR支持预训练或者进一步微调的模型进行服务部署。目前支持以下几种服务部署:

  • 中文离线文件转写服务(CPU版本),已完成
  • 中文流式语音识别服务(CPU版本),已完成
  • 英文离线文件转写服务(CPU版本),已完成
  • 中文离线文件转写服务(GPU版本),进行中
  • 更多支持中

详细信息可以参阅(服务部署文档)。

pip3 install -U funasr 或者从源码安装

git clone https://github.com/alibaba/FunASR.git && cd FunASR
pip3 install -e ./

为预训练模型安装 modelscope(可选) pip3 install -U modelscope

https://gitcode.com/alibaba-damo-academy/FunASR

作者:Jeebiz  创建时间:2024-07-09 12:43
最后编辑:Jeebiz  更新时间:2025-05-12 09:20