二、安装方法
建议使用 NuGet 包管理器完成安装操作。以下是三种可行的安装方式:
(一)通过 Package Manager Console 安装
打开 Visual Studio 中的「Package Manager Console」,执行如下命令:
Install-Package ManySpeech.AliParaformerAsr
(二)使用 .NET CLI 工具安装
在任意命令行环境中运行以下指令进行包的添加:
dotnet add package ManySpeech.AliParaformerAsr
(三)手动通过 NuGet 界面安装
进入 Visual Studio 的 NuGet 包管理界面,搜索关键词“ManySpeech.AliParaformerAsr”,找到对应包后点击“安装”按钮即可完成集成。
三、配置文件说明(参考 asr.yaml)
解码过程中所依赖的 asr.yaml 配置文件中,多数参数保持默认即可,但部分关键选项可根据需求调整:
- use_itn: true:当使用 sensevoicesmall 模型时,启用该参数可开启逆文本正则化功能。例如,将数字“123”自动转换为“一百二十三”,使输出文本更贴近自然语言表达习惯。
四、代码调用示例
(一)离线(非流式)模型调用流程
1. 引用命名空间
在项目代码中引入必要的命名空间:
using ManySpeech.AliParaformerAsr;
using ManySpeech.AliParaformerAsr.Model;
2. 模型初始化与参数设置
paraformer 模型初始化示例:
首先确定应用根目录,并构建各文件路径:
string applicationBase = AppDomain.CurrentDomain.BaseDirectory;
string modelName = "speech_paraformer-large_asr_nat-zh-cn-16k-common-vocab8404-onnx";
string modelFilePath = applicationBase + "./" + modelName + "/model_quant.onnx";
string configFilePath = applicationBase + "./" + modelName + "/asr.yaml";
string mvnFilePath = applicationBase + "./" + modelName + "/am.mvn";
string tokensFilePath = applicationBase + "./" + modelName + "/tokens.txt";
随后创建离线识别器实例:
OfflineRecognizer offlineRecognizer = new OfflineRecognizer(modelFilePath, configFilePath, mvnFilePath, tokensFilePath);
SeACo-paraformer 模型初始化步骤:
若采用 SeACo 版本模型,需额外处理热词文件:
- 进入模型所在目录,查找或创建名为 hotword.txt 的文件。
- 每行输入一个中文词汇,用于添加自定义热词(如专业术语、人名等),以提升特定词汇的识别准确率。
代码中需新增热词文件路径参数,示例如下:
string applicationBase = AppDomain.CurrentDomain.BaseDirectory;
string modelName = "paraformer-seaco-large-zh-timestamp-onnx-offline";
string modelFilePath = applicationBase + "./" + modelName + "/model.int8.onnx";
string modelebFilePath = applicationBase + "./" + modelName + "/model_eb.int8.onnx";
string configFilePath = applicationBase + "./" + modelName + "/asr.yaml";
string mvnFilePath = applicationBase + "./" + modelName + "/am.mvn";
string hotwordFilePath = applicationBase + "./" + modelName + "/hotword.txt";
string tokensFilePath = applicationBase + "./" + modelName + "/tokens.txt";
[此处为图片1]模型初始化与离线识别调用流程如下:
OfflineRecognizer offlineRecognizer = new OfflineRecognizer(
modelFilePath: modelFilePath,
configFilePath: configFilePath,
mvnFilePath: mvnFilePath,
tokensFilePath: tokensFilePath,
modelebFilePath: modelebFilePath,
hotwordFilePath: hotwordFilePath);
进行语音识别时,首先准备音频数据并转换为样本列表:
List<float> samples = new List<float>();
// 此处省略将 wav 文件转换为 samples 的相关代码,详细实现可参考 ManySpeech.AliParaformerAsr.Examples 示例项目
随后创建对应的离线处理流,并逐个添加音频样本:
List<OfflineStream> streams = new List<OfflineStream>();
foreach (var sample in samples)
{
OfflineStream stream = offlineRecognizer.CreateOfflineStream();
stream.AddSamples(sample);
streams.Add(stream);
}
完成所有流的构建后,执行批量识别并获取结果:
List<OfflineRecognizerResultEntity> results = offlineRecognizer.GetResults(streams);
[此处为图片1]
识别输出示例:
he must be home now for the light is on他一定在家因为灯亮着就是有一种推理或者解释的那种感觉
elapsed_milliseconds:1502.8828125
total_duration:40525.6875
rtf:0.037084696280599808
该语音识别模型由达摩院研发,具备良好的识别性能和使用便捷性。
(二)实时流式识别模型调用方法
在项目中引入必要的命名空间:
using ManySpeech.AliParaformerAsr;
using ManySpeech.AliParaformerAsr.Model;
配置模型所需文件路径,并完成在线识别器的初始化:
string encoderFilePath = applicationBase + "./" + modelName + "/encoder.int8.onnx";
string decoderFilePath = applicationBase + "./" + modelName + "/decoder.int8.onnx";
string configFilePath = applicationBase + "./" + modelName + "/asr.yaml";
string mvnFilePath = applicationBase + "./" + modelName + "/am.mvn";
string tokensFilePath = applicationBase + "./" + modelName + "/tokens.txt";
OnlineRecognizer onlineRecognizer = new OnlineRecognizer(
encoderFilePath,
decoderFilePath,
configFilePath,
mvnFilePath,
tokensFilePath);
[此处为图片2]
进行流式识别时,支持批处理模式。以下为批量处理示例:
List<float> samples = new List<float>();
// 省略 wav 转换为 samples 的具体实现
List<OnlineStream> streams = new List<OnlineStream>();
foreach (var sample in samples)
{
OnlineStream stream = onlineRecognizer.CreateOnlineStream();
stream.AddSamples(sample);
streams.Add(stream);
}
List<OnlineRecognizerResultEntity> results = onlineRecognizer.GetResults(streams);
对于单一流处理场景,可直接创建单一 stream 并获取识别结果:
OnlineStream stream = onlineRecognizer.CreateOnlineStream();
stream.AddSamples(sample);
OnlineRecognizerResultEntity result = onlineRecognizer.GetResult(stream);
更多细节请参阅 ManySpeech.AliParaformerAsr.Examples 中提供的完整示例代码。
五、相关工程
在处理长音频时,为了实现合理的切分,可引入语音端点检测技术。推荐使用 ManySpeech.AliFsmnVad 库来完成该任务,安装方式如下:
dotnet add package ManySpeech.AliFsmnVad
针对语音识别结果中普遍缺乏标点的问题,建议集成文本标点预测功能。可通过添加 ManySpeech.AliCTTransformerPunc 库进行支持,安装命令为:
dotnet add package ManySpeech.AliCTTransformerPunc
关于上述库的具体调用方法和使用示例,可查阅其官方文档或参考 ManySpeech.AliParaformerAsr.Examples 项目。该项目为控制台及桌面端示例程序,主要用于演示基础语音识别功能,包括离线语音转写、实时流式识别等常见操作场景。
七、模型下载(支持的 ONNX 模型)
以下是 ManySpeech.AliParaformerAsr 所支持的 ONNX 模型列表,包含各模型的名称、类型、语言支持范围、是否包含标点与时间戳信息以及对应的下载地址,便于根据实际应用场景选择合适的模型:
| 模型名称 | 类型 | 支持语言 | 标点 | 时间戳 | 下载地址 |
|---|---|---|---|---|---|
| paraformer-large-zh-en-onnx-offline | 非流式 | 中文、英文 | 否 | 否 | (https://huggingface.co/manyeyes/speech_paraformer-large_asr_nat-zh-cn-16k-common-vocab8404-onnx ), (https://www.modelscope.cn/models/manyeyes/paraformer-large-zh-en-onnx-offline ) |
| paraformer-large-zh-en-timestamp-onnx-offline | 非流式 | 中文、英文 | 否 | 是 | https://www.modelscope.cn/models/manyeyes/paraformer-large-zh-en-timestamp-onnx-offline |
| paraformer-large-en-onnx-offline | 非流式 | 英文 | 否 | 否 | https://www.modelscope.cn/models/manyeyes/paraformer-large-en-onnx-offline |
| paraformer-large-zh-en-onnx-online | 流式 | 中文、英文 | 否 | 否 | https://www.modelscope.cn/models/manyeyes/paraformer-large-zh-en-onnx-online |
| paraformer-large-zh-yue-en-timestamp-onnx-offline-dengcunqin-20240805 | 非流式 | 中文、粤语、英文 | 否 | 是 | https://www.modelscope.cn/models/manyeyes/paraformer-large-zh-yue-en-timestamp-onnx-offline-dengcunqin-20240805 |
| paraformer-large-zh-yue-en-onnx-offline-dengcunqin-20240805 | 非流式 | 中文、粤语、英文 | 否 | 否 | https://www.modelscope.cn/models/manyeyes/paraformer-large-zh-yue-en-onnx-offline-dengcunqin-20240805 |
六、其他说明
测试用例:以 ManySpeech.AliParaformerAsr.Examples 项目作为主要测试案例,用于验证各项功能的正确性与性能表现。
测试 CPU 配置:测试所使用的处理器为 Intel? Core? i7-10750H CPU @ 2.60GHz(实际运行频率 2.59 GHz)。
平台支持情况:
- Windows:支持 Windows 7 SP1 及以上版本操作系统。
- macOS:兼容 macOS 10.13 (High Sierra) 及更高版本,同时适用于 iOS 设备。
- Linux:可在主流 Linux 发行版上运行,但需满足特定依赖条件,具体要求请参考 .NET 6 所支持的 Linux 版本列表。
- Android:支持 Android 5.0 (API 级别 21) 及以上系统版本。
八、模型介绍
(一)模型用途
Paraformer 是由达摩院语音团队研发的一种高效非自回归端到端语音识别框架。项目中所采用的 Paraformer 中文通用语音识别模型,基于工业级数万小时标注音频数据进行训练,具备出色的通用性与较高的识别准确率。该模型适用于多种实际应用场景,如语音输入法、语音导航系统、智能会议纪要生成等。
(二)模型结构
Paraformer 的整体架构主要由五个核心组件构成:Encoder、Predictor、Sampler、Decoder 和 Loss function。其结构示意图可查看此处,各模块功能如下:
- Encoder:支持多种网络结构,例如 self-attention、conformer 和 SAN-M,主要用于从输入音频中提取声学特征。
- Predictor:由两层前馈神经网络(FFN)组成,负责预测目标文本的字符数量,并抽取对应的文字级声学向量,为后续识别提供关键信息。
- Sampler:无参数模块,能够根据输入的声学向量和目标向量生成富含语义的特征表示,从而增强模型对上下文语义的理解能力。
- Decoder:结构上类似于传统自回归模型,但采用双向建模方式(而非单向),能更有效地捕捉上下文依赖关系,提升识别准确性。
- Loss function:包含多个优化目标,包括交叉熵(CE)、MWER(最小词错误率)以及 Predictor 的 MAE(平均绝对误差)损失,通过多任务学习机制保障模型整体精度。
(三)主要技术亮点
- Predictor 模块设计:采用 Continuous integrate-and-fire (CIF) 机制构建预测器,可精准估计语音段落中应输出的文字数量,并提取对应的声学特征向量,显著提高识别的稳定性与准确性。
- 语义增强型采样策略(Sampler):通过对声学与文本向量进行融合变换,生成具有语义含义的中间特征,结合双向 Decoder 实现更强的上下文建模能力,使输出结果更符合语言逻辑。
- 基于负样本采样的 MWER 训练准则:在训练过程中引入区分性训练目标,利用负例优化模型参数,有效降低识别错误率,全面提升系统的鲁棒性与性能表现。
(四)相关资源与模型链接
以下为部分可用模型及其特性说明:
| 模型名称 | 处理模式 | 支持语言/功能 | 是否支持标点 | 是否带时间戳 | 模型地址 |
|---|---|---|---|---|---|
| paraformer-large-zh-yue-en-onnx-online-dengcunqin-20240208 | 流式 | 中文、粤语、英文 | 否 | 否 | https://www.modelscope.cn/models/manyeyes/paraformer-large-zh-yue-en-onnx-online-dengcunqin-20240208 |
| paraformer-seaco-large-zh-timestamp-onnx-offline | 非流式 | 中文、热词 | 否 | 是 | https://www.modelscope.cn/models/manyeyes/paraformer-seaco-large-zh-timestamp-onnx-offline |
| SenseVoiceSmall | 非流式 | 中文、粤语、英文、日语、韩语 | 是 | 否 | https://www.modelscope.cn/models/manyeyes/sensevoice-small-onnx, https://www.modelscope.cn/models/manyeyes/sensevoice-small-split-embed-onnx |
| sensevoice-small-wenetspeech-yue-int8-onnx | 非流式 | 粤语、中文、英文、日语、韩语 | 是 | 否 | https://www.modelscope.cn/models/manyeyes/sensevoice-small-wenetspeech-yue-int8-onnx |
[此处为图片1]
参考文献与扩展资料
- 相关模型系列:
- paraformer-large-offline(非流式)
- paraformer-large-online(流式)
- SenseVoiceSmall(非流式)
- 学术论文:Paraformer: Fast and Accurate Parallel Transformer for Non-autoregressive End-to-End Speech Recognition
- 论文解读:Paraformer: 高识别率、高计算效率的单轮非自回归端到端语音识别模型


雷达卡


京公网安备 11010802022788号







