Add go-api-examples (#219)

This commit is contained in:
Fangjun Kuang
2023-07-21 17:56:09 +08:00
committed by GitHub
parent 6125d9e063
commit e63d205b3d
28 changed files with 1624 additions and 14 deletions

View File

@@ -0,0 +1 @@
real-time-speech-recognition-from-microphone

View File

@@ -0,0 +1,23 @@
# Introduction
This examples shows how to use the golang package of [sherpa-onnx][sherpa-onnx]
for real-time speech recognition from microphone.
It uses <https://github.com/gordonklaus/portaudio>
to read the microphone and you have to install `portaudio` first.
On macOS, you can use
```
brew install portaudio
```
and it will install `portaudio` into `/usr/local/Cellar/portaudio/19.7.0`.
You need to set the following environment variable
```
export PKG_CONFIG_PATH=/usr/local/Cellar/portaudio/19.7.0
```
so that `pkg-config --cflags --libs portaudio-2.0` can run successfully.
[sherpa-onnx]: https://github.com/k2-fsa/sherpa-onnx

View File

@@ -0,0 +1,15 @@
module real-time-speech-recognition-from-microphone
go 1.20
require (
github.com/gordonklaus/portaudio v0.0.0-20230709114228-aafa478834f5
github.com/k2-fsa/sherpa-onnx-go v1.5.3-alpha.8
github.com/spf13/pflag v1.0.5
)
require (
github.com/k2-fsa/sherpa-onnx-go-linux v1.5.3-alpha.6 // indirect
github.com/k2-fsa/sherpa-onnx-go-macos v1.5.3-alpha.4 // indirect
github.com/k2-fsa/sherpa-onnx-go-windows v1.5.3-alpha.5 // indirect
)

View File

@@ -0,0 +1,12 @@
github.com/gordonklaus/portaudio v0.0.0-20230709114228-aafa478834f5 h1:5AlozfqaVjGYGhms2OsdUyfdJME76E6rx5MdGpjzZpc=
github.com/gordonklaus/portaudio v0.0.0-20230709114228-aafa478834f5/go.mod h1:WY8R6YKlI2ZI3UyzFk7P6yGSuS+hFwNtEzrexRyD7Es=
github.com/k2-fsa/sherpa-onnx-go v1.5.3-alpha.8 h1:BXc31pWwd7CJLM6m9HavxqiyYJdN3Jc9I26pd4x+JHE=
github.com/k2-fsa/sherpa-onnx-go v1.5.3-alpha.8/go.mod h1:kszL/pwg9XTpRGi1AYW/aSwdhRBqb6LN0SjXR0jnsBo=
github.com/k2-fsa/sherpa-onnx-go-linux v1.5.3-alpha.6 h1:o0+l4Wr3IWkWH+kdt8ZZP55L8mRSpW7h1KzvXcfx9FM=
github.com/k2-fsa/sherpa-onnx-go-linux v1.5.3-alpha.6/go.mod h1:9kU02PSdDdzBApwIDBmE2jWS54WvZbRafOEvW/PVLxE=
github.com/k2-fsa/sherpa-onnx-go-macos v1.5.3-alpha.4 h1:Xri23R3+tQFkNwO6zVxqZbjMRJP40z7JtoSqW6UP6sM=
github.com/k2-fsa/sherpa-onnx-go-macos v1.5.3-alpha.4/go.mod h1:a+AJZKNQkFO+JyzGkHySysYfBzzdcoJI5ITFsnhVcmo=
github.com/k2-fsa/sherpa-onnx-go-windows v1.5.3-alpha.5 h1:OR1LLoptR8W35j5u06KrYUblG35B83HHCPWrVK31uHY=
github.com/k2-fsa/sherpa-onnx-go-windows v1.5.3-alpha.5/go.mod h1:ermMOETZUv0nM7MmnXWqeHREMR6zQXBhJpEP2fbHIZo=
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=

View File

@@ -0,0 +1,110 @@
package main
import (
"github.com/gordonklaus/portaudio"
sherpa "github.com/k2-fsa/sherpa-onnx-go/sherpa_onnx"
flag "github.com/spf13/pflag"
"strings"
"fmt"
"log"
)
func main() {
err := portaudio.Initialize()
if err != nil {
log.Fatalf("Unable to initialize portaudio: %v\n", err)
}
defer portaudio.Terminate()
default_device, err := portaudio.DefaultInputDevice()
if err != nil {
log.Fatal("Failed to get default input device: %v\n", err)
}
fmt.Printf("Select default input device: %s\n", default_device.Name)
param := portaudio.StreamParameters{}
param.Input.Device = default_device
param.Input.Channels = 1
param.Input.Latency = default_device.DefaultLowInputLatency
param.SampleRate = 16000
param.FramesPerBuffer = 0
param.Flags = portaudio.ClipOff
config := sherpa.OnlineRecognizerConfig{}
config.FeatConfig = sherpa.FeatureConfig{SampleRate: 16000, FeatureDim: 80}
flag.StringVar(&config.ModelConfig.Encoder, "encoder", "", "Path to the encoder model")
flag.StringVar(&config.ModelConfig.Decoder, "decoder", "", "Path to the decoder model")
flag.StringVar(&config.ModelConfig.Joiner, "joiner", "", "Path to the joiner model")
flag.StringVar(&config.ModelConfig.Tokens, "tokens", "", "Path to the tokens file")
flag.IntVar(&config.ModelConfig.NumThreads, "num-threads", 1, "Number of threads for computing")
flag.IntVar(&config.ModelConfig.Debug, "debug", 0, "Whether to show debug message")
flag.StringVar(&config.ModelConfig.ModelType, "model-type", "", "Optional. Used for loading the model in a faster way")
flag.StringVar(&config.ModelConfig.Provider, "provider", "cpu", "Provider to use")
flag.StringVar(&config.DecodingMethod, "decoding-method", "greedy_search", "Decoding method. Possible values: greedy_search, modified_beam_search")
flag.IntVar(&config.MaxActivePaths, "max-active-paths", 4, "Used only when --decoding-method is modified_beam_search")
flag.IntVar(&config.EnableEndpoint, "enable-endpoint", 1, "Whether to enable endpoint")
flag.Float32Var(&config.Rule1MinTrailingSilence, "rule1-min-trailing-silence", 2.4, "Threshold for rule1")
flag.Float32Var(&config.Rule2MinTrailingSilence, "rule2-min-trailing-silence", 1.2, "Threshold for rule2")
flag.Float32Var(&config.Rule3MinUtteranceLength, "rule3-min-utterance-length", 20, "Threshold for rule3")
flag.Parse()
log.Println("Initializing recognizer (may take several seconds)")
recognizer := sherpa.NewOnlineRecognizer(&config)
log.Println("Recognizer created!")
defer sherpa.DeleteOnlineRecognizer(recognizer)
stream := sherpa.NewOnlineStream(recognizer)
// you can choose another value for 0.1 if you want
samplesPerCall := int32(param.SampleRate * 0.1) // 0.1 second
samples := make([]float32, samplesPerCall)
s, err := portaudio.OpenStream(param, samples)
if err != nil {
log.Fatalf("Failed to open the stream")
}
defer s.Close()
chk(s.Start())
var last_text string
segment_idx := 0
fmt.Println("Started! Please speak")
for {
chk(s.Read())
stream.AcceptWaveform(int(param.SampleRate), samples)
for recognizer.IsReady(stream) {
recognizer.Decode(stream)
}
text := recognizer.GetResult(stream).Text
if len(text) != 0 && last_text != text {
last_text = strings.ToLower(text)
fmt.Printf("\r%d: %s", segment_idx, last_text)
}
if recognizer.IsEndpoint(stream) {
if len(text) != 0 {
segment_idx++
fmt.Println()
}
recognizer.Reset(stream)
}
}
chk(s.Stop())
return
}
func chk(err error) {
if err != nil {
panic(err)
}
}

View File

@@ -0,0 +1,15 @@
#!/usr/bin/env bash
# Please refer to
# https://k2-fsa.github.io/sherpa/onnx/pretrained_models/online-transducer/zipformer-transducer-models.html#pkufool-icefall-asr-zipformer-streaming-wenetspeech-20230615-chinese
# to download the model
# before you run this script.
#
# You can switch to different online models if you need
./real-time-speech-recognition-from-microphone \
--encoder ./icefall-asr-zipformer-streaming-wenetspeech-20230615/exp/encoder-epoch-12-avg-4-chunk-16-left-128.onnx \
--decoder ./icefall-asr-zipformer-streaming-wenetspeech-20230615/exp/decoder-epoch-12-avg-4-chunk-16-left-128.onnx \
--joiner ./icefall-asr-zipformer-streaming-wenetspeech-20230615/exp/joiner-epoch-12-avg-4-chunk-16-left-128.onnx \
--tokens ./icefall-asr-zipformer-streaming-wenetspeech-20230615/data/lang_char/tokens.txt \
--model-type zipformer2