一、安装k2
二、编译 sherpa-onnx c++ server
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++程序
cd /workspace/shrepa-onnx
1)启动websocket server,并指定模型:
帮助:
./build/bin/sherpa-onnx-online-websocket-server --help 执行:
./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
帮助:
./build/bin/sherpa-onnx-online-websocket-client --help 执行:
./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:
帮助:
./build/bin/sherpa-onnx-microphone --help 执行:
./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) 识别一个音频文件:
帮助:
./build/bin/sherpa-onnx --help 执行:
./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示例:
cd /workspace/shrepa-onnx
1)识别一个音频文件:
执行:
./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解码:
apt-get install libportaudio2
pip install sounddevice -i https://pypi.tuna.tsinghua.edu.cn/simple 执行:
./python-api-examples/online-websocket-client-microphone.py \
--server-addr 127.0.0.1 \
--server-port 6006
3)使用microphone
pip install sherpa-onnx -i https://pypi.tuna.tsinghua.edu.cn/simple 执行
./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,带端点检测
pip install sherpa-onnx -i https://pypi.tuna.tsinghua.edu.cn/simple 执行:
./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推理模型,资源消耗减少,并发量及解码速度有较大提升。