背景:为了在不联网的情况下,在终端仍然能够实现语音识别功能,为后续采用语音来控制机械臂执行任务做铺垫
虚拟机配置:
内存:8G
处理器:4核
硬盘:80G
1 源码编译
由于虚拟机环境下无法使用GPU环境,所以我装载的是CPU版本。
下载和编译参考以下链接(强烈推荐官方文档):
具体执行
git clone https://github.com/k2-fsa/sherpa-onnx
cd sherpa-onnx
mkdir build
cd build
cmake -DCMAKE_BUILD_TYPE=Release ..
make -j6
这里是一个下载于编译 sherpa-onnx 框架的过程,如果编译成功会在 build/bin 文件夹下生成可执行文件。
我在执行第一步时由于网络缘故,没有下载成功,所以我直接去https://github.com/k2-fsa/sherpa-onnx 网址下载源文件,解压后再进行编译的。
2 Python库下载
同样的,参照官方文档安装Python依赖包,由于我是安装的CPU版本,所以我采用的Method1安装。
具体执行
# 安装Python依赖包
pip install sherpa-onnx
# 检测是否安装成功
python3 -c "import sherpa_onnx; print(sherpa_onnx.__file__)"
which sherpa-onnx
sherpa-onnx --help
ls -lh $(dirname $(which sherpa-onnx))/sherpa-onnx*
运行指令后在终端有对应的输出即为安装成功
3 安装预训练模型
由于我需要的功能是通过麦克风输入音频,并转换为文本,所以我装载的预训练模型为sherpa-onnx-streaming-zipformer-bilingual-zh-en-2023-02-20 (Bilingual, Chinese + English)
具体执行
cd /path/to/sherpa-onnx #/path/to/ 需要修改为sherpa-onnx源文件存在的真实路径
wget https://github.com/k2-fsa/sherpa-onnx/releases/download/asr-models/sherpa-onnx-streaming-zipformer-bilingual-zh-en-2023-02-20.tar.bz2
tar xvf sherpa-onnx-streaming-zipformer-bilingual-zh-en-2023-02-20.tar.bz2
rm sherpa-onnx-streaming-zipformer-bilingual-zh-en-2023-02-20.tar.bz2
随后检测模型是否安装完整
cd /path/to/sherpa-onnx/sherpa-onnx-streaming-zipformer-bilingual-zh-en-2023-02-20
ls -lh *.onnx
有六项对应的输出即为安装成功,输出如下:
-rw-r–r-- 1 kuangfangjun root 13M Mar 31 21:11 decoder-epoch-99-avg-1.int8.onnx
-rw-r–r-- 1 kuangfangjun root 14M Feb 20 20:13 decoder-epoch-99-avg-1.onnx
-rw-r–r-- 1 kuangfangjun root 174M Mar 31 21:11 encoder-epoch-99-avg-1.int8.onnx
-rw-r–r-- 1 kuangfangjun root 315M Feb 20 20:13 encoder-epoch-99-avg-1.onnx
-rw-r–r-- 1 kuangfangjun root 3.1M Mar 31 21:11 joiner-epoch-99-avg-1.int8.onnx
-rw-r–r-- 1 kuangfangjun root 13M Feb 20 20:13 joiner-epoch-99-avg-1.onnx
随后对模型文件进行测试,执行如下:
cd /path/to/sherpa-onnx
## 下面的tokens、encoder、decoder、joiner为对sherpa-onnx进行配置
./build/bin/sherpa-onnx \
--tokens=./sherpa-onnx-streaming-zipformer-bilingual-zh-en-2023-02-20/tokens.txt \
--encoder=./sherpa-onnx-streaming-zipformer-bilingual-zh-en-2023-02-20/encoder-epoch-99-avg-1.onnx \
--decoder=./sherpa-onnx-streaming-zipformer-bilingual-zh-en-2023-02-20/decoder-epoch-99-avg-1.onnx \
--joiner=./sherpa-onnx-streaming-zipformer-bilingual-zh-en-2023-02-20/joiner-epoch-99-avg-1.onnx \
./sherpa-onnx-streaming-zipformer-bilingual-zh-en-2023-02-20/test_wavs/1.wav #测试文件的文件路径,有其他的.wav文件也可以替换掉
在终端有对应的输出结果代表该模型已经可以正常使用,测试结果如下:
进一步的开发学习可以跟着官方Python案例进行学习,在运行python文件时是按 Ctrl + C 退出,有任何问题和指教欢迎留言交流