-
Notifications
You must be signed in to change notification settings - Fork 474
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6686c7d
commit f7b3735
Showing
21 changed files
with
429 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
// Copyright 2024 Xiaomi Corporation | ||
|
||
// This file shows how to use an offline NeMo CTC model, i.e., non-streaming NeMo CTC model,, | ||
// to decode files. | ||
import com.k2fsa.sherpa.onnx.*; | ||
|
||
public class NonStreamingDecodeFileNemo { | ||
public static void main(String[] args) { | ||
// please refer to | ||
// https://github.com/k2-fsa/sherpa-onnx/releases/download/asr-models/sherpa-onnx-nemo-ctc-en-citrinet-512.tar.bz2 | ||
// to download model files | ||
String model = "./sherpa-onnx-nemo-ctc-en-citrinet-512/model.int8.onnx"; | ||
String tokens = "./sherpa-onnx-nemo-ctc-en-citrinet-512/tokens.txt"; | ||
|
||
String waveFilename = "./sherpa-onnx-nemo-ctc-en-citrinet-512/test_wavs/1.wav"; | ||
|
||
WaveReader reader = new WaveReader(waveFilename); | ||
|
||
OfflineNemoEncDecCtcModelConfig nemo = | ||
OfflineNemoEncDecCtcModelConfig.builder().setModel(model).build(); | ||
|
||
OfflineModelConfig modelConfig = | ||
OfflineModelConfig.builder() | ||
.setNemo(nemo) | ||
.setTokens(tokens) | ||
.setNumThreads(1) | ||
.setDebug(true) | ||
.build(); | ||
|
||
OfflineRecognizerConfig config = | ||
OfflineRecognizerConfig.builder() | ||
.setOfflineModelConfig(modelConfig) | ||
.setDecodingMethod("greedy_search") | ||
.build(); | ||
|
||
OfflineRecognizer recognizer = new OfflineRecognizer(config); | ||
OfflineStream stream = recognizer.createStream(); | ||
stream.acceptWaveform(reader.getSamples(), reader.getSampleRate()); | ||
|
||
recognizer.decode(stream); | ||
|
||
String text = recognizer.getResult(stream).getText(); | ||
|
||
System.out.printf("filename:%s\nresult:%s\n", waveFilename, text); | ||
|
||
stream.release(); | ||
recognizer.release(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
// Copyright 2024 Xiaomi Corporation | ||
|
||
// This file shows how to use an online CTC model, i.e., streaming CTC model, | ||
// to decode files. | ||
import com.k2fsa.sherpa.onnx.*; | ||
|
||
public class StreamingDecodeFileCtcHLG { | ||
public static void main(String[] args) { | ||
// please refer to | ||
// https://github.com/k2-fsa/sherpa-onnx/releases/download/asr-models/sherpa-onnx-streaming-zipformer-ctc-small-2024-03-18.tar.bz2 | ||
// to download model files | ||
String model = | ||
"./sherpa-onnx-streaming-zipformer-ctc-small-2024-03-18/ctc-epoch-30-avg-3-chunk-16-left-128.int8.onnx"; | ||
String tokens = "./sherpa-onnx-streaming-zipformer-ctc-small-2024-03-18/tokens.txt"; | ||
String hlg = "./sherpa-onnx-streaming-zipformer-ctc-small-2024-03-18/HLG.fst"; | ||
String waveFilename = "./sherpa-onnx-streaming-zipformer-ctc-small-2024-03-18/test_wavs/8k.wav"; | ||
|
||
WaveReader reader = new WaveReader(waveFilename); | ||
|
||
OnlineZipformer2CtcModelConfig ctc = | ||
OnlineZipformer2CtcModelConfig.builder().setModel(model).build(); | ||
|
||
OnlineModelConfig modelConfig = | ||
OnlineModelConfig.builder() | ||
.setZipformer2Ctc(ctc) | ||
.setTokens(tokens) | ||
.setNumThreads(1) | ||
.setDebug(true) | ||
.build(); | ||
|
||
OnlineCtcFstDecoderConfig ctcFstDecoderConfig = | ||
OnlineCtcFstDecoderConfig.builder().setGraph("hlg").build(); | ||
|
||
OnlineRecognizerConfig config = | ||
OnlineRecognizerConfig.builder() | ||
.setOnlineModelConfig(modelConfig) | ||
.setCtcFstDecoderConfig(ctcFstDecoderConfig) | ||
.build(); | ||
|
||
OnlineRecognizer recognizer = new OnlineRecognizer(config); | ||
OnlineStream stream = recognizer.createStream(); | ||
stream.acceptWaveform(reader.getSamples(), reader.getSampleRate()); | ||
|
||
float[] tailPaddings = new float[(int) (0.3 * reader.getSampleRate())]; | ||
stream.acceptWaveform(tailPaddings, reader.getSampleRate()); | ||
|
||
while (recognizer.isReady(stream)) { | ||
recognizer.decode(stream); | ||
} | ||
|
||
String text = recognizer.getResult(stream).getText(); | ||
|
||
System.out.printf("filename:%s\nresult:%s\n", waveFilename, text); | ||
|
||
stream.release(); | ||
recognizer.release(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
#!/usr/bin/env bash | ||
|
||
set -ex | ||
|
||
if [[ ! -f ../build/lib/libsherpa-onnx-jni.dylib && ! -f ../build/lib/libsherpa-onnx-jni.so ]]; then | ||
mkdir -p ../build | ||
pushd ../build | ||
cmake \ | ||
-DSHERPA_ONNX_ENABLE_PYTHON=OFF \ | ||
-DSHERPA_ONNX_ENABLE_TESTS=OFF \ | ||
-DSHERPA_ONNX_ENABLE_CHECK=OFF \ | ||
-DBUILD_SHARED_LIBS=ON \ | ||
-DSHERPA_ONNX_ENABLE_PORTAUDIO=OFF \ | ||
-DSHERPA_ONNX_ENABLE_JNI=ON \ | ||
.. | ||
|
||
make -j4 | ||
ls -lh lib | ||
popd | ||
fi | ||
|
||
if [ ! -f ../sherpa-onnx/java-api/build/sherpa-onnx.jar ]; then | ||
pushd ../sherpa-onnx/java-api | ||
make | ||
popd | ||
fi | ||
|
||
if [[ ! -f ../build/lib/libsherpa-onnx-jni.dylib && ! -f ../build/lib/libsherpa-onnx-jni.so ]]; then | ||
cmake \ | ||
-DSHERPA_ONNX_ENABLE_PYTHON=OFF \ | ||
-DSHERPA_ONNX_ENABLE_TESTS=OFF \ | ||
-DSHERPA_ONNX_ENABLE_CHECK=OFF \ | ||
-DBUILD_SHARED_LIBS=ON \ | ||
-DSHERPA_ONNX_ENABLE_PORTAUDIO=OFF \ | ||
-DSHERPA_ONNX_ENABLE_JNI=ON \ | ||
.. | ||
|
||
make -j4 | ||
ls -lh lib | ||
fi | ||
|
||
if [ ! -f ./sherpa-onnx-nemo-ctc-en-citrinet-512/tokens.txt ]; then | ||
curl -SL -O https://github.com/k2-fsa/sherpa-onnx/releases/download/asr-models/sherpa-onnx-nemo-ctc-en-citrinet-512.tar.bz2 | ||
tar xvf sherpa-onnx-nemo-ctc-en-citrinet-512.tar.bz2 | ||
rm sherpa-onnx-nemo-ctc-en-citrinet-512.tar.bz2 | ||
fi | ||
|
||
java \ | ||
-Djava.library.path=$PWD/../build/lib \ | ||
-cp ../sherpa-onnx/java-api/build/sherpa-onnx.jar \ | ||
NonStreamingDecodeFileNemo.java |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
#!/usr/bin/env bash | ||
set -ex | ||
|
||
if [[ ! -f ../build/lib/libsherpa-onnx-jni.dylib && ! -f ../build/lib/libsherpa-onnx-jni.so ]]; then | ||
mkdir -p ../build | ||
pushd ../build | ||
cmake \ | ||
-DSHERPA_ONNX_ENABLE_PYTHON=OFF \ | ||
-DSHERPA_ONNX_ENABLE_TESTS=OFF \ | ||
-DSHERPA_ONNX_ENABLE_CHECK=OFF \ | ||
-DBUILD_SHARED_LIBS=ON \ | ||
-DSHERPA_ONNX_ENABLE_PORTAUDIO=OFF \ | ||
-DSHERPA_ONNX_ENABLE_JNI=ON \ | ||
.. | ||
|
||
make -j4 | ||
ls -lh lib | ||
popd | ||
fi | ||
|
||
if [ ! -f ../sherpa-onnx/java-api/build/sherpa-onnx.jar ]; then | ||
pushd ../sherpa-onnx/java-api | ||
make | ||
popd | ||
fi | ||
|
||
if [ ! -f ./sherpa-onnx-streaming-zipformer-ctc-small-2024-03-18/tokens.txt ]; then | ||
curl -SL -O https://github.com/k2-fsa/sherpa-onnx/releases/download/asr-models/sherpa-onnx-streaming-zipformer-ctc-small-2024-03-18.tar.bz2 | ||
tar xvf sherpa-onnx-streaming-zipformer-ctc-small-2024-03-18.tar.bz2 | ||
rm sherpa-onnx-streaming-zipformer-ctc-small-2024-03-18.tar.bz2 | ||
fi | ||
|
||
java \ | ||
-Djava.library.path=$PWD/../build/lib \ | ||
-cp ../sherpa-onnx/java-api/build/sherpa-onnx.jar \ | ||
StreamingDecodeFileCtcHLG.java |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.