302 lines
7.3 KiB
Markdown
302 lines
7.3 KiB
Markdown
|
|
# xwq-speech-to-text
|
|||
|
|
|
|||
|
|
* ### 安卓端封装VOSK模型语音识别功能
|
|||
|
|
|
|||
|
|
|
|||
|
|
<font size="4" color="#f00">注意!试用需要先将插件导入项目并且页面加入引用代码再打自定义基座</font>
|
|||
|
|
|
|||
|
|
<font size="4" color="#f00">注意!插件不包含模型文件,需要自行下载,因为模型文件比较大,加入无法云打包,可以打完基座后把模型拷贝到static目录下</font>
|
|||
|
|
|
|||
|
|
|
|||
|
|
#### [模型下载地址](https://alphacephei.com/vosk/models)
|
|||
|
|
|
|||
|
|
|
|||
|
|
### 使用步骤
|
|||
|
|
|
|||
|
|
* 第一步:使用VOSK
|
|||
|
|
```
|
|||
|
|
import {downloadModel,initVoskModel,startSpeechVoice,stopSpeechVoice} from '@/uni_modules/xwq-speech-to-text';
|
|||
|
|
import { CallbackType, RedultType } from '@/uni_modules/xwq-speech-to-text/utssdk/interface.uts';
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
* 第二步:初始化模型
|
|||
|
|
```
|
|||
|
|
/**
|
|||
|
|
* 初始化模型
|
|||
|
|
* 将返回的模型地址缓存起来,在第二次用到的时候直接传入初始化,避免重复解压
|
|||
|
|
*/
|
|||
|
|
const init = () => {
|
|||
|
|
let path = '/static/vosk-model-small-cn-0.22.zip';
|
|||
|
|
initVoskModel({
|
|||
|
|
zipModelPath: path
|
|||
|
|
}, (result : CallbackType) => {
|
|||
|
|
console.log('模型地址====', result.data)
|
|||
|
|
modelPath.value = result.data.modelPath;
|
|||
|
|
})
|
|||
|
|
};
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
* 第三步:开始语音识别
|
|||
|
|
```
|
|||
|
|
/**
|
|||
|
|
* 开始语音识别
|
|||
|
|
*/
|
|||
|
|
const start = () => {
|
|||
|
|
startSpeechVoice((res : RedultType) => {
|
|||
|
|
console.log('识别结果===', res.data.text)
|
|||
|
|
content.value += res.data.text
|
|||
|
|
})
|
|||
|
|
};
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
|
|||
|
|
* 停止语音识别
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* 停止语音识别
|
|||
|
|
*/
|
|||
|
|
const stop = () => {
|
|||
|
|
stopSpeechVoice()
|
|||
|
|
};
|
|||
|
|
```
|
|||
|
|
* 如果已经解压过模型,存在解压路径时初始化
|
|||
|
|
```
|
|||
|
|
/**
|
|||
|
|
* 存在模型解压路径(即已经初始化过一次)
|
|||
|
|
*/
|
|||
|
|
initVoskModel({
|
|||
|
|
modelPath: modelPath.value,
|
|||
|
|
zipModelPath: ''
|
|||
|
|
}, (result : CallbackType) => {
|
|||
|
|
console.log('模型地址====', result.data)
|
|||
|
|
})
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
* 在线下载模型
|
|||
|
|
```
|
|||
|
|
const downModel = () => {
|
|||
|
|
const url = 'https://example.com/vosk-model-small-cn-0.22.zip';//需要换成真实的模型下载地址
|
|||
|
|
downloadModel(url, (res) => {
|
|||
|
|
console.log(res)
|
|||
|
|
if (res.code == 0 && res.filePath != '') {
|
|||
|
|
initVoskModel({
|
|||
|
|
zipModelPath: res.filePath
|
|||
|
|
}, (result : CallbackType) => {
|
|||
|
|
console.log('模型地址====', result.data)
|
|||
|
|
modelPath.value = result.data.modelPath;
|
|||
|
|
})
|
|||
|
|
}
|
|||
|
|
})
|
|||
|
|
};
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
|
|||
|
|
### 使用完整示例(UniappX)
|
|||
|
|
```
|
|||
|
|
<template>
|
|||
|
|
<view>
|
|||
|
|
<view class="area">
|
|||
|
|
<button type="default" @click="downModel">在线下载vosk模型</button>
|
|||
|
|
<button type="default" @click="init">初始化vosk模型</button>
|
|||
|
|
<button type="default" @click="start">开始语音识别</button>
|
|||
|
|
<button type="default" @click="stop">结束语音识别</button>
|
|||
|
|
<button type="default" @click="unzipInnit">存在模型解压地址初始化</button>
|
|||
|
|
</view>
|
|||
|
|
<view>
|
|||
|
|
<view class="title">
|
|||
|
|
<text>识别结果:</text>
|
|||
|
|
</view>
|
|||
|
|
<view class="content">
|
|||
|
|
<textarea :value="content" disabled style="width: 100%;padding: 15px;"></textarea>
|
|||
|
|
</view>
|
|||
|
|
</view>
|
|||
|
|
</view>
|
|||
|
|
</template>
|
|||
|
|
|
|||
|
|
<script setup>
|
|||
|
|
import {downloadModel,initVoskModel,startSpeechVoice,stopSpeechVoice} from '@/uni_modules/xwq-speech-to-text';
|
|||
|
|
import { CallbackType, RedultType } from '@/uni_modules/xwq-speech-to-text/utssdk/interface.uts';
|
|||
|
|
|
|||
|
|
const modelPath = ref('');//储存模型解压地址
|
|||
|
|
const content = ref('');//识别结果
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* 在线下载模型
|
|||
|
|
*/
|
|||
|
|
const downModel = () => {
|
|||
|
|
const url = 'https://example.com/vosk-model-small-cn-0.22.zip'
|
|||
|
|
downloadModel(url, (res) => {
|
|||
|
|
console.log(res)
|
|||
|
|
if (res.code == 0 && res.filePath != '') {
|
|||
|
|
initVoskModel({
|
|||
|
|
zipModelPath: res.filePath
|
|||
|
|
}, (result : CallbackType) => {
|
|||
|
|
console.log('模型地址====', result.data)
|
|||
|
|
modelPath.value = result.data.modelPath;
|
|||
|
|
})
|
|||
|
|
}
|
|||
|
|
})
|
|||
|
|
};
|
|||
|
|
/**
|
|||
|
|
* 初始化模型
|
|||
|
|
* 将返回的模型地址缓存起来,在第二次用到的时候直接传入初始化,避免重复解压
|
|||
|
|
*/
|
|||
|
|
const init = () => {
|
|||
|
|
let path = '/static/vosk-model-small-cn-0.22.zip';
|
|||
|
|
initVoskModel({
|
|||
|
|
zipModelPath: path
|
|||
|
|
}, (result : CallbackType) => {
|
|||
|
|
console.log('模型地址====', result.data)
|
|||
|
|
modelPath.value = result.data.modelPath;
|
|||
|
|
})
|
|||
|
|
};
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* 存在模型解压路径(即已经初始化过一次)
|
|||
|
|
*/
|
|||
|
|
const unzipInnit = () => {
|
|||
|
|
initVoskModel({
|
|||
|
|
modelPath: modelPath.value,
|
|||
|
|
zipModelPath: ''
|
|||
|
|
}, (result : CallbackType) => {
|
|||
|
|
console.log('模型地址====', result.data)
|
|||
|
|
})
|
|||
|
|
};
|
|||
|
|
/**
|
|||
|
|
* 开始语音识别
|
|||
|
|
*/
|
|||
|
|
const start = () => {
|
|||
|
|
startSpeechVoice((res : RedultType) => {
|
|||
|
|
console.log('识别结果===', res.data.text)
|
|||
|
|
content.value += res.data.text
|
|||
|
|
})
|
|||
|
|
};
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* 停止语音识别
|
|||
|
|
*/
|
|||
|
|
const stop = () => {
|
|||
|
|
stopSpeechVoice()
|
|||
|
|
};
|
|||
|
|
</script>
|
|||
|
|
|
|||
|
|
<style>
|
|||
|
|
.title {
|
|||
|
|
margin: 20px 0 10px 0;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
.content {
|
|||
|
|
width: 100%;
|
|||
|
|
height: 100%;
|
|||
|
|
border: 1px solid #ccc;
|
|||
|
|
background-color: #f2f2f2;
|
|||
|
|
}
|
|||
|
|
</style>
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
### 使用完整示例(Uniapp)
|
|||
|
|
```
|
|||
|
|
<template>
|
|||
|
|
<view>
|
|||
|
|
<view class="area">
|
|||
|
|
<button type="default" @click="downModel">在线下载vosk模型</button>
|
|||
|
|
<button type="default" @click="init">初始化vosk模型</button>
|
|||
|
|
<button type="default" @click="start">开始语音识别</button>
|
|||
|
|
<button type="default" @click="stop">结束语音识别</button>
|
|||
|
|
<button type="default" @click="unzipInnit">存在模型解压地址初始化</button>
|
|||
|
|
</view>
|
|||
|
|
<view>
|
|||
|
|
<view class="title">
|
|||
|
|
<text>识别结果:</text>
|
|||
|
|
</view>
|
|||
|
|
<view class="content">
|
|||
|
|
<textarea :value="content" disabled style="width: 100%;padding: 15px;"></textarea>
|
|||
|
|
</view>
|
|||
|
|
</view>
|
|||
|
|
</view>
|
|||
|
|
</template>
|
|||
|
|
|
|||
|
|
<script setup lang="ts">
|
|||
|
|
import {downloadModel,initVoskModel,startSpeechVoice,stopSpeechVoice} from '@/uni_modules/xwq-speech-to-text';
|
|||
|
|
import { CallbackType, RedultType } from '@/uni_modules/xwq-speech-to-text/utssdk/interface.uts';
|
|||
|
|
|
|||
|
|
const modelPath = ref('');//储存模型解压地址
|
|||
|
|
const content = ref('');//识别结果
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* 在线下载模型
|
|||
|
|
*/
|
|||
|
|
const downModel = () => {
|
|||
|
|
const url = 'https://example.com/vosk-model-small-cn-0.22.zip';//需要换成真实的模型下载地址
|
|||
|
|
downloadModel(url, (res) => {
|
|||
|
|
console.log(res)
|
|||
|
|
if (res.code == 0 && res.filePath != '') {
|
|||
|
|
initVoskModel({
|
|||
|
|
zipModelPath: res.filePath
|
|||
|
|
}, (result) => {
|
|||
|
|
console.log('模型地址====', result.data)
|
|||
|
|
modelPath.value = result.data.modelPath;
|
|||
|
|
})
|
|||
|
|
}
|
|||
|
|
})
|
|||
|
|
};
|
|||
|
|
/**
|
|||
|
|
* 初始化模型
|
|||
|
|
* 将返回的模型地址缓存起来,在第二次用到的时候直接传入初始化,避免重复解压
|
|||
|
|
*/
|
|||
|
|
const init = () => {
|
|||
|
|
let path = '/static/vosk-model-small-cn-0.22.zip';
|
|||
|
|
const staticPath = plus.io.convertLocalFileSystemURL(path);
|
|||
|
|
initVoskModel({
|
|||
|
|
zipModelPath: staticPath
|
|||
|
|
}, (result) => {
|
|||
|
|
console.log('模型地址====', result.data)
|
|||
|
|
modelPath.value = result.data.modelPath;
|
|||
|
|
})
|
|||
|
|
};
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* 存在模型解压路径(即已经初始化过一次)
|
|||
|
|
*/
|
|||
|
|
const unzipInnit = () => {
|
|||
|
|
initVoskModel({
|
|||
|
|
modelPath: modelPath.value,
|
|||
|
|
zipModelPath: ''
|
|||
|
|
}, (result) => {
|
|||
|
|
console.log('模型地址====', result.data)
|
|||
|
|
})
|
|||
|
|
};
|
|||
|
|
/**
|
|||
|
|
* 开始语音识别
|
|||
|
|
*/
|
|||
|
|
const start = () => {
|
|||
|
|
startSpeechVoice((res) => {
|
|||
|
|
console.log('识别结果===', res.data.text)
|
|||
|
|
content.value += res.data.text
|
|||
|
|
})
|
|||
|
|
};
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* 停止语音识别
|
|||
|
|
*/
|
|||
|
|
const stop = () => {
|
|||
|
|
stopSpeechVoice()
|
|||
|
|
};
|
|||
|
|
</script>
|
|||
|
|
|
|||
|
|
<style>
|
|||
|
|
.title {
|
|||
|
|
margin: 20px 0 10px 0;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
.content {
|
|||
|
|
width: 100%;
|
|||
|
|
height: 100%;
|
|||
|
|
border: 1px solid #ccc;
|
|||
|
|
background-color: #f2f2f2;
|
|||
|
|
}
|
|||
|
|
</style>
|
|||
|
|
```
|
|||
|
|
|