更新记录
0.0.1(2025-07-08)
- 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);
释放资源
在不再需要识别器时,应该释放资源:
// 释放资源
recognizer.dispose();
示例代码
完整的使用示例:
<text>{{txt}}</text>
<button @click="onClick">音频转文本</button>
UNIAPPX
import { uesSherpaOnnx, type LimeRecognizeResult , type UesSherpaOnnxOptions, type LimeRecognizeFileOptions } from '@/uni_modules/lime-sherpa-onnx'
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)
}
UNIAPP
import { uesSherpaOnnx } from '@/uni_modules/lime-sherpa-onnx'
const factory = 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)
}
})
export default {
data() {
return {
txt: ''
}
},
methods: {
onClick() {
factory.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)
}
})
}
}
}
API参考
UesSherpaOnnxOptions
初始化选项,用于配置语音识别器。
参数 | 类型 | 必填 | 说明 |
---|---|---|---|
modelPath | string | 是 | Paraformer模型文件路径 |
vadModelPath | string | 是 | VAD模型文件路径 |
tokensPath | string | 是 | Tokens文件路径 |
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官方详细文档和模型请访问以下站点: