-
Notifications
You must be signed in to change notification settings - Fork 473
/
KeywordSpotterFromFile.java
65 lines (51 loc) · 2.24 KB
/
KeywordSpotterFromFile.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
// Copyright 2024 Xiaomi Corporation
// This file shows how to use a keyword spotter model to spot keywords from
// a file.
import com.k2fsa.sherpa.onnx.*;
public class KyewordSpotterFromFile {
public static void main(String[] args) {
// please download test files from https://github.com/k2-fsa/sherpa-onnx/releases/tag/kws-models
String encoder =
"./sherpa-onnx-kws-zipformer-wenetspeech-3.3M-2024-01-01/encoder-epoch-12-avg-2-chunk-16-left-64.onnx";
String decoder =
"./sherpa-onnx-kws-zipformer-wenetspeech-3.3M-2024-01-01/decoder-epoch-12-avg-2-chunk-16-left-64.onnx";
String joiner =
"./sherpa-onnx-kws-zipformer-wenetspeech-3.3M-2024-01-01/joiner-epoch-12-avg-2-chunk-16-left-64.onnx";
String tokens = "./sherpa-onnx-kws-zipformer-wenetspeech-3.3M-2024-01-01/tokens.txt";
String keywordsFile =
"./sherpa-onnx-kws-zipformer-wenetspeech-3.3M-2024-01-01/test_wavs/test_keywords.txt";
String waveFilename = "./sherpa-onnx-kws-zipformer-wenetspeech-3.3M-2024-01-01/test_wavs/3.wav";
OnlineTransducerModelConfig transducer =
OnlineTransducerModelConfig.builder()
.setEncoder(encoder)
.setDecoder(decoder)
.setJoiner(joiner)
.build();
OnlineModelConfig modelConfig =
OnlineModelConfig.builder()
.setTransducer(transducer)
.setTokens(tokens)
.setNumThreads(1)
.setDebug(true)
.build();
KeywordSpotterConfig config =
KeywordSpotterConfig.builder()
.setOnlineModelConfig(modelConfig)
.setKeywordsFile(keywordsFile)
.build();
KeywordSpotter kws = new KeywordSpotter(config);
OnlineStream stream = kws.createStream();
WaveReader reader = new WaveReader(waveFilename);
stream.acceptWaveform(reader.getSamples(), reader.getSampleRate());
float[] tailPaddings = new float[(int) (0.8 * reader.getSampleRate())];
stream.acceptWaveform(tailPaddings, reader.getSampleRate());
while (kws.isReady(stream)) {
kws.decode(stream);
String keyword = kws.getResult(stream).getKeyword();
if (!keyword.isEmpty()) {
System.out.printf("Detected keyword: %s\n", keyword);
}
}
kws.release();
}
}