Add VAD demo for Java API (#928)
This commit is contained in:
@@ -56,3 +56,15 @@ The punctuation model supports both English and Chinese.
|
||||
```bash
|
||||
./run-speaker-identification.sh
|
||||
```
|
||||
|
||||
## VAD (Remove silence)
|
||||
|
||||
```bash
|
||||
./run-vad-remove-slience.sh
|
||||
```
|
||||
|
||||
## VAD + Non-streaming Paraformer for speech recognition
|
||||
|
||||
```bash
|
||||
./run-vad-non-streaming-paraformer.sh
|
||||
```
|
||||
|
||||
104
java-api-examples/VadNonStreamingParaformer.java
Normal file
104
java-api-examples/VadNonStreamingParaformer.java
Normal file
@@ -0,0 +1,104 @@
|
||||
// Copyright 2024 Xiaomi Corporation
|
||||
|
||||
// This file shows how to use a silero_vad model with a non-streaming Paraformer
|
||||
// for speech recognition.
|
||||
|
||||
import com.k2fsa.sherpa.onnx.*;
|
||||
import java.util.Arrays;
|
||||
|
||||
public class VadNonStreamingParaformer {
|
||||
public static Vad createVad() {
|
||||
// please download ./silero_vad.onnx from
|
||||
// https://github.com/k2-fsa/sherpa-onnx/releases/tag/asr-models
|
||||
String model = "./silero_vad.onnx";
|
||||
SileroVadModelConfig sileroVad =
|
||||
SileroVadModelConfig.builder()
|
||||
.setModel(model)
|
||||
.setThreshold(0.5f)
|
||||
.setMinSilenceDuration(0.25f)
|
||||
.setMinSpeechDuration(0.5f)
|
||||
.setWindowSize(512)
|
||||
.build();
|
||||
|
||||
VadModelConfig config =
|
||||
VadModelConfig.builder()
|
||||
.setSileroVadModelConfig(sileroVad)
|
||||
.setSampleRate(16000)
|
||||
.setNumThreads(1)
|
||||
.setDebug(true)
|
||||
.setProvider("cpu")
|
||||
.build();
|
||||
|
||||
return new Vad(config);
|
||||
}
|
||||
|
||||
public static OfflineRecognizer createOfflineRecognizer() {
|
||||
// please refer to
|
||||
// https://k2-fsa.github.io/sherpa/onnx/pretrained_models/offline-paraformer/paraformer-models.html#csukuangfj-sherpa-onnx-paraformer-zh-2023-03-28-chinese-english
|
||||
// to download model files
|
||||
String model = "./sherpa-onnx-paraformer-zh-2023-03-28/model.int8.onnx";
|
||||
String tokens = "./sherpa-onnx-paraformer-zh-2023-03-28/tokens.txt";
|
||||
|
||||
String waveFilename = "./sherpa-onnx-paraformer-zh-2023-03-28/test_wavs/3-sichuan.wav";
|
||||
|
||||
WaveReader reader = new WaveReader(waveFilename);
|
||||
|
||||
OfflineParaformerModelConfig paraformer =
|
||||
OfflineParaformerModelConfig.builder().setModel(model).build();
|
||||
|
||||
OfflineModelConfig modelConfig =
|
||||
OfflineModelConfig.builder()
|
||||
.setParaformer(paraformer)
|
||||
.setTokens(tokens)
|
||||
.setNumThreads(1)
|
||||
.setDebug(true)
|
||||
.build();
|
||||
|
||||
OfflineRecognizerConfig config =
|
||||
OfflineRecognizerConfig.builder()
|
||||
.setOfflineModelConfig(modelConfig)
|
||||
.setDecodingMethod("greedy_search")
|
||||
.build();
|
||||
|
||||
return new OfflineRecognizer(config);
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
Vad vad = createVad();
|
||||
OfflineRecognizer recognizer = createOfflineRecognizer();
|
||||
|
||||
// You can download the test file from
|
||||
// https://github.com/k2-fsa/sherpa-onnx/releases/tag/asr-models
|
||||
String testWaveFilename = "./lei-jun-test.wav";
|
||||
WaveReader reader = new WaveReader(testWaveFilename);
|
||||
|
||||
int numSamples = reader.getSamples().length;
|
||||
int numIter = numSamples / 512;
|
||||
|
||||
for (int i = 0; i != numIter; ++i) {
|
||||
int start = i * 512;
|
||||
int end = start + 512;
|
||||
float[] samples = Arrays.copyOfRange(reader.getSamples(), start, end);
|
||||
vad.acceptWaveform(samples);
|
||||
if (vad.isSpeechDetected()) {
|
||||
while (!vad.empty()) {
|
||||
SpeechSegment segment = vad.front();
|
||||
float startTime = segment.getStart() / 16000.0f;
|
||||
float duration = segment.getSamples().length / 16000.0f;
|
||||
|
||||
OfflineStream stream = recognizer.createStream();
|
||||
stream.acceptWaveform(segment.getSamples(), 16000);
|
||||
recognizer.decode(stream);
|
||||
String text = recognizer.getResult(stream).getText();
|
||||
|
||||
if (!text.isEmpty()) {
|
||||
System.out.printf("%.3f--%.3f: %s\n", startTime, startTime + duration, text);
|
||||
}
|
||||
|
||||
vad.pop();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
79
java-api-examples/VadRemoveSilence.java
Normal file
79
java-api-examples/VadRemoveSilence.java
Normal file
@@ -0,0 +1,79 @@
|
||||
// Copyright 2024 Xiaomi Corporation
|
||||
|
||||
// This file shows how to use a silero_vad model to remove silences from
|
||||
// a wave file.
|
||||
|
||||
import com.k2fsa.sherpa.onnx.*;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
|
||||
public class VadRemoveSilence {
|
||||
public static void main(String[] args) {
|
||||
// please download ./silero_vad.onnx from
|
||||
// https://github.com/k2-fsa/sherpa-onnx/releases/tag/asr-models
|
||||
String model = "./silero_vad.onnx";
|
||||
SileroVadModelConfig sileroVad =
|
||||
SileroVadModelConfig.builder()
|
||||
.setModel(model)
|
||||
.setThreshold(0.5f)
|
||||
.setMinSilenceDuration(0.25f)
|
||||
.setMinSpeechDuration(0.5f)
|
||||
.setWindowSize(512)
|
||||
.build();
|
||||
|
||||
VadModelConfig config =
|
||||
VadModelConfig.builder()
|
||||
.setSileroVadModelConfig(sileroVad)
|
||||
.setSampleRate(16000)
|
||||
.setNumThreads(1)
|
||||
.setDebug(true)
|
||||
.setProvider("cpu")
|
||||
.build();
|
||||
|
||||
Vad vad = new Vad(config);
|
||||
|
||||
// You can download the test file from
|
||||
// https://github.com/k2-fsa/sherpa-onnx/releases/tag/asr-models
|
||||
String testWaveFilename = "./lei-jun-test.wav";
|
||||
WaveReader reader = new WaveReader(testWaveFilename);
|
||||
|
||||
int numSamples = reader.getSamples().length;
|
||||
int numIter = numSamples / 512;
|
||||
|
||||
ArrayList<float[]> segments = new ArrayList<float[]>();
|
||||
|
||||
for (int i = 0; i != numIter; ++i) {
|
||||
int start = i * 512;
|
||||
int end = start + 512;
|
||||
float[] samples = Arrays.copyOfRange(reader.getSamples(), start, end);
|
||||
vad.acceptWaveform(samples);
|
||||
if (vad.isSpeechDetected()) {
|
||||
while (!vad.empty()) {
|
||||
|
||||
// if you want to get the starting time of this segment, you can use
|
||||
/* float startTime = vad.front().getStart() / 16000.0f; */
|
||||
|
||||
segments.add(vad.front().getSamples());
|
||||
vad.pop();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// get total number of samples
|
||||
int n = 0;
|
||||
for (float[] s : segments) {
|
||||
n += s.length;
|
||||
}
|
||||
|
||||
float[] allSamples = new float[n];
|
||||
int i = 0;
|
||||
for (float[] s : segments) {
|
||||
System.arraycopy(s, 0, allSamples, i, s.length);
|
||||
i += s.length;
|
||||
}
|
||||
|
||||
String outFilename = "lei-jun-test-no-silence.wav";
|
||||
WaveWriter.write(outFilename, allSamples, 16000);
|
||||
System.out.printf("Saved to %s\n", outFilename);
|
||||
}
|
||||
}
|
||||
46
java-api-examples/run-vad-non-streaming-paraformer.sh
Executable file
46
java-api-examples/run-vad-non-streaming-paraformer.sh
Executable file
@@ -0,0 +1,46 @@
|
||||
#!/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 ./silero_vad.onnx ]; then
|
||||
curl -SL -O https://github.com/k2-fsa/sherpa-onnx/releases/download/asr-models/silero_vad.onnx
|
||||
fi
|
||||
|
||||
if [ ! -f ./lei-jun-test.wav ]; then
|
||||
curl -SL -O https://github.com/k2-fsa/sherpa-onnx/releases/download/asr-models/lei-jun-test.wav
|
||||
fi
|
||||
|
||||
if [ ! -f ./sherpa-onnx-paraformer-zh-2023-03-28/tokens.txt ]; then
|
||||
curl -SL -O https://github.com/k2-fsa/sherpa-onnx/releases/download/asr-models/sherpa-onnx-paraformer-zh-2023-03-28.tar.bz2
|
||||
|
||||
tar xvf sherpa-onnx-paraformer-zh-2023-03-28.tar.bz2
|
||||
rm sherpa-onnx-paraformer-zh-2023-03-28.tar.bz2
|
||||
fi
|
||||
|
||||
java \
|
||||
-Djava.library.path=$PWD/../build/lib \
|
||||
-cp ../sherpa-onnx/java-api/build/sherpa-onnx.jar \
|
||||
./VadNonStreamingParaformer.java
|
||||
39
java-api-examples/run-vad-remove-slience.sh
Executable file
39
java-api-examples/run-vad-remove-slience.sh
Executable file
@@ -0,0 +1,39 @@
|
||||
#!/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 ./silero_vad.onnx ]; then
|
||||
curl -SL -O https://github.com/k2-fsa/sherpa-onnx/releases/download/asr-models/silero_vad.onnx
|
||||
fi
|
||||
|
||||
if [ ! -f ./lei-jun-test.wav ]; then
|
||||
curl -SL -O https://github.com/k2-fsa/sherpa-onnx/releases/download/asr-models/lei-jun-test.wav
|
||||
fi
|
||||
|
||||
java \
|
||||
-Djava.library.path=$PWD/../build/lib \
|
||||
-cp ../sherpa-onnx/java-api/build/sherpa-onnx.jar \
|
||||
./VadRemoveSilence.java
|
||||
Reference in New Issue
Block a user