更新记录
0.0.5(2025-07-12)
- chore: 更新文档
0.0.4(2025-07-10)
- feat: 增加在线模式,用于实时识别
0.0.3(2025-07-09)
- feat: 更新文档
平台兼容性
云端兼容性
阿里云 | 腾讯云 | 支付宝云 |
---|---|---|
√ | √ | √ |
uni-app(4.56)
Vue2 | Vue3 | Chrome | Safari | app-vue | app-nvue | Android | iOS | 鸿蒙 |
---|---|---|---|---|---|---|---|---|
- | - | - | - | √ | √ | 5.1 | √ | - |
微信小程序 | 支付宝小程序 | 抖音小程序 | 百度小程序 | 快手小程序 | 京东小程序 | 鸿蒙元服务 | QQ小程序 | 飞书小程序 | 快应用-华为 | 快应用-联盟 |
---|---|---|---|---|---|---|---|---|---|---|
- | - | - | - | - | - | - | - | - | - | - |
uni-app x(4.61)
Chrome | Safari | Android | iOS | 鸿蒙 | 微信小程序 |
---|---|---|---|---|---|
- | - | 5.1 | √ | - | - |
lime-sherpa-onnx
一个基于Sherpa-onnx的语音识别插件,可以在没有网络连接的情况下进行语音识别。支持Android和iOS平台。
文档链接
📚 组件详细文档请访问以下站点:
安装方法
- 在uni-app插件市场中搜索并导入
lime-sherpa-onnx
- 导入后在页面引入方法再自定义基座
代码演示
初始化
首先,需要初始化语音识别器,指定模型路径和相关配置:
import { UesSherpaOnnx, type UesSherpaOnnxOptions, type LimeSherpaOnnxRecognizer } from '@/uni_modules/lime-sherpa-onnx';
// 初始化选项
const options: UesSherpaOnnxOptions = {
modelPath: '/static/models/paraformer.onnx',
vadModelPath: '/static/models/silero_vad.onnx',
tokensPath: '/static/models/tokens.txt'
};
// 初始化识别器
let recognizer = UesSherpaOnnx(options);
识别音频文件
使用初始化好的识别器识别音频文件:
import { type LimeRecognizeFileOptions } from '@/uni_modules/lime-sherpa-onnx';
// 识别选项
const recognizeOptions:LimeRecognizeFileOptions = {
audioPath: '/static/audio/test.wav',
success: (result) => {
console.log('识别结果:', result.text);
console.log('文本片段:', result.segments);
}
};
// 开始识别
recognizer.recognizeFile(recognizeOptions);
实时识别
目前实时还是在线的比较准确,在安卓上测试。IOS未测
import { uesSherpaOnnx, type LimeStartRecorderOptions } from '@/uni_modules/lime-sherpa-onnx';
const recognizer = uesSherpaOnnx({
encoder: '/static/sherpa-onnx-streaming-zipformer-bilingual-zh-en-2023-02-20/encoder-epoch-99-avg-1.int8.onnx',
decoder: '/static/sherpa-onnx-streaming-zipformer-bilingual-zh-en-2023-02-20/decoder-epoch-99-avg-1.onnx',
joiner: '/static/sherpa-onnx-streaming-zipformer-bilingual-zh-en-2023-02-20/joiner-epoch-99-avg-1.onnx',
tokensPath: '/static/sherpa-onnx-streaming-zipformer-bilingual-zh-en-2023-02-20/tokens.txt',
modelType: 'zipformer',
mode: 'online',
success() {
console.log('uesSherpaOnnx success')
},
fail(err) {
console.log('uesSherpaOnnx err', err)
}
} as UesSherpaOnnxOptions)
// 监听实时录音
recognizer.onRecorder((res)=> {
console.log('onRecorder res', res)
})
// 开始录音
recognizer.startRecorder({
success(res) {
console.log('startRecorder success', res)
},
fail(err) {
console.log('startRecorder fail', err)
}
} as LimeStartRecorderOptions)
// 停止录音
recognizer.stopRecorder()
释放资源
在不再需要识别器时,应该释放资源:
// 释放资源
recognizer.dispose();
示例代码
完整的使用示例:
<text>{{txt}}</text>
<button @click="onClick">音频转文本</button>
<button @click="startRecord">开始录制音频</button>
<button @click="stopRecord">停止录制音频</button>
UniappX
import { uesSherpaOnnx, type LimeRecognizeResult , type UesSherpaOnnxOptions, type LimeRecognizeFileOptions } from '@/uni_modules/lime-sherpa-onnx'
//录制音频并非一定要使用这个插件,你可以使用其它能录制wav的,官方的API有问题
import { getRecorderManager, type LimeRecorderManagerStartOption } from '@/uni_modules/lime-recorder'
const txt = ref('测试');
const recognizer = uesSherpaOnnx({
modelPath: '/static/sherpa_models/model.int8.onnx',
vadModelPath: '/static/sherpa_models/silero_vad.onnx',
tokensPath: '/static/sherpa_models/tokens.txt',
success() {
console.log('uesSherpaOnnx success')
},
fail(err) {
console.log('uesSherpaOnnx err', err)
}
} as UesSherpaOnnxOptions)
const onClick = () => {
recognizer.recognizeFile({
audioPath: '/static/ttsmaker-file-2025-7-2-18-42-28.wav',
success(res) {
console.log('recognizeFile success', res)
txt.value = res.text
},
fail(err) {
console.log('recognizeFile err', err)
}
} as LimeRecognizeFileOptions)
}
//录制音频文件
const recorderManager = getRecorderManager()
recorderManager.onStop((res) => {
factory.recognizeFile({
audioPath: res.tempFilePath,
success(res) {
console.log('recognizeFile success', res)
txt.value = res.text
},
fail(err) {
console.log('recognizeFile err', err)
}
} as LimeRecognizeFileOptions)
})
const startRecord = () => {
const options:LimeRecorderManagerStartOption = {
format: 'wav',
numberOfChannels: 1,
}
recorderManager.start(options)
}
const stopRecord = () => {
recorderManager.stop()
}
Uniapp
import { uesSherpaOnnx } from '@/uni_modules/lime-sherpa-onnx'
//录制音频并非一定要使用这个插件,你可以使用其它能录制wav的,官方的API有问题
import { getRecorderManager } from '@/uni_modules/lime-recorder'
const recognizer = uesSherpaOnnx({
modelPath: '/static/sherpa_models/model.int8.onnx',
vadModelPath: '/static/sherpa_models/silero_vad.onnx',
tokensPath: '/static/sherpa_models/tokens.txt',
success() {
console.log('uesSherpaOnnx success')
},
fail(err) {
console.log('uesSherpaOnnx err', err)
}
})
//录制音频文件
const recorderManager = getRecorderManager()
export default {
data() {
return {
txt: ''
}
},
mounted() {
recorderManager.onStop((res) => {
factory.recognizeFile({
audioPath: res.tempFilePath,
success: (res) => {
console.log('recognizeFile success', res)
this.txt = res.text
},
fail(err) {
console.log('recognizeFile err', err)
}
})
})
},
methods: {
onClick() {
recognizer.recognizeFile({
audioPath: '/static/ttsmaker-file-2025-7-2-18-42-28.wav',
success: (res) => {
console.log('recognizeFile success', res)
this.txt = res.text
},
fail(err) {
console.log('recognizeFile err', err)
}
})
},
startRecord(){
const options = {
format: 'wav',
numberOfChannels: 1,
}
recorderManager.start(options)
},
stopRecord(){
recorderManager.stop()
}
}
}
API参考
UesSherpaOnnxOptions
初始化选项,用于配置语音识别器。
参数 | 类型 | 必填 | 说明 |
---|---|---|---|
modelPath | string | 否 | 模型文件路径 |
encoder | string | 否 | encoder模型文件路径 |
decoder | string | 否 | decoder模型文件路径 |
joiner | string | 否 | joiner模型文件路径 |
vadModelPath | string | 否 | VAD模型文件路径 |
tokensPath | string | 否 | Tokens文件路径 |
modelType | string | 否 | 模型类型 |
mode | 'online' | 'offline' | 否 | 在线或离线,默认离线,在线主要用于实时识别语音 |
UesSherpaOnnx(options: UesSherpaOnnxOptions): LimeSherpaOnnxRecognizer
初始化函数,返回语音识别器实例。
LimeSherpaOnnxRecognizer
语音识别器接口。
recognizeFile(options: LimeRecognizeFileOptions): void
识别音频文件。
参数 | 类型 | 必填 | 说明 |
---|---|---|---|
options | LimeRecognizeFileOptions | 是 | 识别选项 |
dispose(): void
释放资源。
LimeRecognizeFileOptions
识别选项。
参数 | 类型 | 必填 | 说明 |
---|---|---|---|
audioPath | string | 是 | 音频文件路径 |
onRecognitionResult | (result: LimeRecognizeResult) => void | 是 | 识别结果回调函数 |
LimeRecognizeResult
识别结果。
属性 | 类型 | 说明 |
---|---|---|
text | string | 完整识别文本 |
segments | Array |
文本片段数组 |
错误处理
插件可能会抛出以下错误:
错误码 | 错误信息 | 说明 |
---|---|---|
9010001 | 无效音频文件路径 | 提供的音频文件路径无效 |
9010002 | 无法读取音频文件 | 无法读取指定的音频文件 |
9010003 | 初始化失败 | 识别器初始化失败 |
9010004 | 识别失败 | 音频识别过程中出错 |
9010005 | 模型加载失败 | 无法加载指定的模型文件 |
9010006 | VAD模型加载失败 | 无法加载VAD模型文件 |
9010007 | Paraformer模型加载失败 | 无法加载Paraformer模型文件 |
9010008 | Tokens文件加载失败 | 无法加载Tokens文件 |
资源下载
📚 SherpaOnnx官方详细文档和模型请访问以下站点: