-
Notifications
You must be signed in to change notification settings - Fork 477
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Go API for Keyword spotting (#1662)
- Loading branch information
1 parent
38d64a6
commit 49154c9
Showing
10 changed files
with
268 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
module keyword-spotting-from-file | ||
|
||
go 1.12 | ||
|
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,79 @@ | ||
package main | ||
|
||
import ( | ||
sherpa "github.com/k2-fsa/sherpa-onnx-go/sherpa_onnx" | ||
"log" | ||
) | ||
|
||
func main() { | ||
log.SetFlags(log.LstdFlags | log.Lmicroseconds) | ||
|
||
config := sherpa.KeywordSpotterConfig{} | ||
|
||
// Please download the models from | ||
// https://github.com/k2-fsa/sherpa-onnx/releases/tag/kws-models | ||
|
||
config.ModelConfig.Transducer.Encoder = "./sherpa-onnx-kws-zipformer-wenetspeech-3.3M-2024-01-01/encoder-epoch-12-avg-2-chunk-16-left-64.onnx" | ||
config.ModelConfig.Transducer.Decoder = "./sherpa-onnx-kws-zipformer-wenetspeech-3.3M-2024-01-01/decoder-epoch-12-avg-2-chunk-16-left-64.onnx" | ||
config.ModelConfig.Transducer.Joiner = "./sherpa-onnx-kws-zipformer-wenetspeech-3.3M-2024-01-01/joiner-epoch-12-avg-2-chunk-16-left-64.onnx" | ||
config.ModelConfig.Tokens = "./sherpa-onnx-kws-zipformer-wenetspeech-3.3M-2024-01-01/tokens.txt" | ||
config.KeywordsFile = "./sherpa-onnx-kws-zipformer-wenetspeech-3.3M-2024-01-01/test_wavs/test_keywords.txt" | ||
config.ModelConfig.NumThreads = 1 | ||
config.ModelConfig.Debug = 1 | ||
|
||
spotter := sherpa.NewKeywordSpotter(&config) | ||
defer sherpa.DeleteKeywordSpotter(spotter) | ||
|
||
wave_filename := "./sherpa-onnx-kws-zipformer-wenetspeech-3.3M-2024-01-01/test_wavs/3.wav" | ||
|
||
wave := sherpa.ReadWave(wave_filename) | ||
if wave == nil { | ||
log.Printf("Failed to read %v\n", wave_filename) | ||
return | ||
} | ||
|
||
log.Println("----------Use pre-defined keywords----------") | ||
|
||
stream := sherpa.NewKeywordStream(spotter) | ||
defer sherpa.DeleteOnlineStream(stream) | ||
|
||
stream.AcceptWaveform(wave.SampleRate, wave.Samples) | ||
|
||
for spotter.IsReady(stream) { | ||
spotter.Decode(stream) | ||
result := spotter.GetResult(stream) | ||
if result.Keyword != "" { | ||
log.Printf("Detected %v\n", result.Keyword) | ||
} | ||
} | ||
|
||
log.Println("----------Use pre-defined keywords + add a new keyword----------") | ||
|
||
stream2 := sherpa.NewKeywordStreamWithKeywords(spotter, "y ǎn y uán @演员") | ||
defer sherpa.DeleteOnlineStream(stream2) | ||
|
||
stream2.AcceptWaveform(wave.SampleRate, wave.Samples) | ||
|
||
for spotter.IsReady(stream2) { | ||
spotter.Decode(stream2) | ||
result := spotter.GetResult(stream2) | ||
if result.Keyword != "" { | ||
log.Printf("Detected %v\n", result.Keyword) | ||
} | ||
} | ||
|
||
log.Println("----------Use pre-defined keywords + add 2 new keywords----------") | ||
|
||
stream3 := sherpa.NewKeywordStreamWithKeywords(spotter, "y ǎn y uán @演员/zh ī m íng @知名") | ||
defer sherpa.DeleteOnlineStream(stream3) | ||
|
||
stream3.AcceptWaveform(wave.SampleRate, wave.Samples) | ||
|
||
for spotter.IsReady(stream3) { | ||
spotter.Decode(stream3) | ||
result := spotter.GetResult(stream3) | ||
if result.Keyword != "" { | ||
log.Printf("Detected %v\n", result.Keyword) | ||
} | ||
} | ||
} |
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,13 @@ | ||
#!/usr/bin/env bash | ||
|
||
set -ex | ||
|
||
if [ ! -f ./sherpa-onnx-kws-zipformer-wenetspeech-3.3M-2024-01-01/tokens.txt ]; then | ||
curl -SL -O https://github.com/k2-fsa/sherpa-onnx/releases/download/kws-models/sherpa-onnx-kws-zipformer-wenetspeech-3.3M-2024-01-01.tar.bz2 | ||
tar xvf sherpa-onnx-kws-zipformer-wenetspeech-3.3M-2024-01-01.tar.bz2 | ||
rm sherpa-onnx-kws-zipformer-wenetspeech-3.3M-2024-01-01.tar.bz2 | ||
fi | ||
|
||
go mod tidy | ||
go build | ||
./keyword-spotting-from-file |
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 @@ | ||
keyword-spotting-from-file |
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,5 @@ | ||
module keyword-spotting-from-file | ||
|
||
go 1.12 | ||
|
||
replace github.com/k2-fsa/sherpa-onnx-go/sherpa_onnx => ../ |
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 @@ | ||
../../../../go-api-examples/keyword-spotting-from-file/main.go |
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 @@ | ||
../../../../go-api-examples/keyword-spotting-from-file/run.sh |
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