k2语音识别:如何使用sherpa-onnx
从孑开始
编辑于 2023年03月16日 15:01

一、安装k2

*参看环境部署​

二、编译 sherpa-onnx c++ server

代码块
JavaScript
自动换行
复制代码
cd /workspace
git clone https://github.com/k2-fsa/sherpa-onnx.git
cd shrepa-onnx
mkdir build
cd build
cmake -DCMAKE_BUILD_TYPE=Release ..
make -j6
复制成功

三、官方文档:

https://k2-fsa.github.io/sherpa/onnx/index.html

四、模型下载:

https://k2-fsa.github.io/sherpa/onnx/pretrained_models/index.html

五、使用c++程序

代码块
JavaScript
自动换行
复制代码
cd /workspace/shrepa-onnx
复制成功

1)启动websocket server,并指定模型:

帮助:

代码块
Shell
自动换行
复制代码
./build/bin/sherpa-onnx-online-websocket-server --help
复制成功

执行:

代码块
Shell
自动换行
复制代码
./build/bin/sherpa-onnx-online-websocket-server \
  --port=6006 \
  --num-work-threads=3 \
  --num-io-threads=2 \
  --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 \
  --log-file=./log.txt \
  --max-batch-size=5 \
  --loop-interval-ms=20
复制成功

2) 启动sherpa-onnx-online-websocket-client

帮助:

代码块
Shell
自动换行
复制代码
./build/bin/sherpa-onnx-online-websocket-client --help
复制成功

执行:

代码块
Shell
自动换行
复制代码
./build/bin/sherpa-onnx-online-websocket-client \
  --server-ip=127.0.0.1 \
  --server-port=6006 \
  --samples-per-message=8000 \
  --seconds-per-message=0.2 \
  /workspace/test_wavs/0.wav
复制成功

ps:也可以使用sherpa(https://github.com/k2-fsa/sherpa.git)启动web client,调用sherpa-onnx c++ websocket server

3)启动microphone test:

帮助:

代码块
Shell
自动换行
复制代码
./build/bin/sherpa-onnx-microphone --help
复制成功

执行:

代码块
Shell
自动换行
复制代码
./build/bin/sherpa-onnx-microphone  \
  ./sherpa-onnx-streaming-zipformer-bilingual-zh-en-2023-02-20/tokens.txt \
  ./sherpa-onnx-streaming-zipformer-bilingual-zh-en-2023-02-20/encoder-epoch-99-avg-1.onnx \
  ./sherpa-onnx-streaming-zipformer-bilingual-zh-en-2023-02-20/decoder-epoch-99-avg-1.onnx \
  ./sherpa-onnx-streaming-zipformer-bilingual-zh-en-2023-02-20/joiner-epoch-99-avg-1.onnx \
  2 \
  greedy_search
复制成功

4) 识别一个音频文件:

帮助:

代码块
Shell
自动换行
复制代码
./build/bin/sherpa-onnx --help
复制成功

执行:

代码块
Shell
自动换行
复制代码
./build/bin/sherpa-onnx  \
  ./sherpa-onnx-streaming-zipformer-bilingual-zh-en-2023-02-20/tokens.txt \
  ./sherpa-onnx-streaming-zipformer-bilingual-zh-en-2023-02-20/encoder-epoch-99-avg-1.onnx \
  ./sherpa-onnx-streaming-zipformer-bilingual-zh-en-2023-02-20/decoder-epoch-99-avg-1.onnx \
  ./sherpa-onnx-streaming-zipformer-bilingual-zh-en-2023-02-20/joiner-epoch-99-avg-1.onnx \
  /workspace/test_wavs/0.wav \
  2 \
  greedy_search

复制成功

六、使用python-api-example示例:

代码块
Shell
自动换行
复制代码
cd /workspace/shrepa-onnx
复制成功

1)识别一个音频文件:

执行:

代码块
Shell
自动换行
复制代码
./python-api-examples/online-websocket-client-decode-file.py \
      --server-addr localhost \
      --server-port 6006 \
      --seconds-per-message 0.1 \
      --samples-per-message 8000 \
      /workspace/test_wavs/0.wav
复制成功

2)使用麦克风输入流式数据,调用websocket解码:

代码块
Shell
自动换行
复制代码
apt-get install libportaudio2

pip install sounddevice -i https://pypi.tuna.tsinghua.edu.cn/simple
复制成功

执行:

代码块
Shell
自动换行
复制代码
./python-api-examples/online-websocket-client-microphone.py  \
  --server-addr 127.0.0.1 \
  --server-port 6006
复制成功

3)使用microphone

代码块
Shell
自动换行
复制代码
pip install sherpa-onnx -i https://pypi.tuna.tsinghua.edu.cn/simple
复制成功

执行

代码块
Shell
自动换行
复制代码
./python-api-examples/speech-recognition-from-microphone.py  \
  --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 \
  --decoding-method=greedy_search
复制成功

4) 使用microphone,带端点检测

代码块
Shell
自动换行
复制代码
pip install sherpa-onnx -i https://pypi.tuna.tsinghua.edu.cn/simple
复制成功

执行:

代码块
Shell
自动换行
复制代码
./python-api-examples/speech-recognition-from-microphone-with-endpoint-detection.py  \
  --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 \
  --decoding-method=greedy_search
复制成功

七、对Android的支持,集成到移动端App

https://k2-fsa.github.io/sherpa/onnx/android/build-sherpa-onnx.html

八、对IOS的支持,集成到移动端App

https://k2-fsa.github.io/sherpa/onnx/ios/build-sherpa-onnx-swift.html

可见sherpa的技术支持是比较全面的。

经过实验对比,使用Onnxruntime推理模型,资源消耗减少,并发量及解码速度有较大提升。