Add Dart API for keyword spotter (#1162)

This commit is contained in:
Fangjun Kuang
2024-07-22 10:53:34 +08:00
committed by GitHub
parent 22a262f5e4
commit ac8223bd8a
17 changed files with 518 additions and 5 deletions

View File

@@ -1 +1,28 @@
!run*.sh
# See https://www.dartlang.org/guides/libraries/private-files
# Files and directories created by pub
.dart_tool/
.packages
build/
# If you're building an application, you may want to check-in your pubspec.lock
pubspec.lock
# Directory created by dartdoc
# If you don't generate documentation locally you can remove this line.
doc/api/
# dotenv environment variables file
.env*
# Avoid committing generated Javascript files:
*.dart.js
*.info.json # Produced by the --dump-info flag.
*.js # When generated by dart2js. Don't specify *.js if your
# project includes source files written in JavaScript.
*.js_
*.js.deps
*.js.map
.flutter-plugins
.flutter-plugins-dependencies

View File

@@ -0,0 +1,3 @@
# https://dart.dev/guides/libraries/private-files
# Created by `dart pub`
.dart_tool/

View File

@@ -0,0 +1,3 @@
## 1.0.0
- Initial version.

View File

@@ -0,0 +1,4 @@
# Introduction
This directory contains keyword spotting examples using
Dart API from [sherpa-onnx](https://github.com/k2-fsa/sherpa-onnx)

View File

@@ -0,0 +1,30 @@
# This file configures the static analysis results for your project (errors,
# warnings, and lints).
#
# This enables the 'recommended' set of lints from `package:lints`.
# This set helps identify many issues that may lead to problems when running
# or consuming Dart code, and enforces writing Dart using a single, idiomatic
# style and format.
#
# If you want a smaller set of lints you can change this to specify
# 'package:lints/core.yaml'. These are just the most critical lints
# (the recommended set includes the core lints).
# The core lints are also what is used by pub.dev for scoring packages.
include: package:lints/recommended.yaml
# Uncomment the following section to specify additional rules.
# linter:
# rules:
# - camel_case_types
# analyzer:
# exclude:
# - path/to/excluded/files/**
# For more information about the core and recommended set of lints, see
# https://dart.dev/go/core-lints
# For additional information about configuring this file, see
# https://dart.dev/guides/language/analysis-options

View File

@@ -0,0 +1 @@
../../vad/bin/init.dart

View File

@@ -0,0 +1,98 @@
// Copyright (c) 2024 Xiaomi Corporation
import 'dart:io';
import 'dart:typed_data';
import 'package:args/args.dart';
import 'package:sherpa_onnx/sherpa_onnx.dart' as sherpa_onnx;
import './init.dart';
void main(List<String> arguments) async {
await initSherpaOnnx();
final parser = ArgParser()
..addOption('encoder', help: 'Path to the encoder model')
..addOption('decoder', help: 'Path to decoder model')
..addOption('joiner', help: 'Path to joiner model')
..addOption('tokens', help: 'Path to tokens.txt')
..addOption('keywords-file', help: 'Path to keywords.txt')
..addOption('input-wav', help: 'Path to input.wav to transcribe');
final res = parser.parse(arguments);
if (res['encoder'] == null ||
res['decoder'] == null ||
res['joiner'] == null ||
res['tokens'] == null ||
res['keywords-file'] == null ||
res['input-wav'] == null) {
print(parser.usage);
exit(1);
}
final encoder = res['encoder'] as String;
final decoder = res['decoder'] as String;
final joiner = res['joiner'] as String;
final tokens = res['tokens'] as String;
final keywordsFile = res['keywords-file'] as String;
final inputWav = res['input-wav'] as String;
final transducer = sherpa_onnx.OnlineTransducerModelConfig(
encoder: encoder,
decoder: decoder,
joiner: joiner,
);
final modelConfig = sherpa_onnx.OnlineModelConfig(
transducer: transducer,
tokens: tokens,
debug: true,
numThreads: 1,
);
final config = sherpa_onnx.KeywordSpotterConfig(
model: modelConfig,
keywordsFile: keywordsFile,
);
final spotter = sherpa_onnx.KeywordSpotter(config);
final waveData = sherpa_onnx.readWave(inputWav);
var stream = spotter.createStream();
// simulate streaming. You can choose an arbitrary chunk size.
// chunkSize of a single sample is also ok, i.e, chunkSize = 1
final chunkSize = 1600; // 0.1 second for 16kHz
final numChunks = waveData.samples.length ~/ chunkSize;
for (int i = 0; i != numChunks; ++i) {
int start = i * chunkSize;
stream.acceptWaveform(
samples:
Float32List.sublistView(waveData.samples, start, start + chunkSize),
sampleRate: waveData.sampleRate,
);
while (spotter.isReady(stream)) {
spotter.decode(stream);
final result = spotter.getResult(stream);
if (result.keyword != '') {
print('Detected: ${result.keyword}');
}
}
}
// 0.5 seconds, assume sampleRate is 16kHz
final tailPaddings = Float32List(8000);
stream.acceptWaveform(
samples: tailPaddings,
sampleRate: waveData.sampleRate,
);
while (spotter.isReady(stream)) {
spotter.decode(stream);
final result = spotter.getResult(stream);
if (result.keyword != '') {
print('Detected: ${result.keyword}');
}
}
stream.free();
spotter.free();
}

View File

@@ -0,0 +1,19 @@
name: keyword_spotter
description: >
This example demonstrates how to use the Dart API for keyword spotting
version: 1.0.0
environment:
sdk: ^3.4.0
dependencies:
sherpa_onnx: ^1.10.17
# sherpa_onnx:
# path: ../../flutter/sherpa_onnx
path: ^1.9.0
args: ^2.5.0
dev_dependencies:
lints: ^3.0.0

View File

@@ -0,0 +1,21 @@
#!/usr/bin/env bash
set -ex
dart pub get
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
dart run \
./bin/zipformer-transducer.dart \
--encoder ./sherpa-onnx-kws-zipformer-wenetspeech-3.3M-2024-01-01/encoder-epoch-12-avg-2-chunk-16-left-64.onnx \
--decoder ./sherpa-onnx-kws-zipformer-wenetspeech-3.3M-2024-01-01/decoder-epoch-12-avg-2-chunk-16-left-64.onnx \
--joiner ./sherpa-onnx-kws-zipformer-wenetspeech-3.3M-2024-01-01/joiner-epoch-12-avg-2-chunk-16-left-64.onnx \
--tokens ./sherpa-onnx-kws-zipformer-wenetspeech-3.3M-2024-01-01/tokens.txt \
--keywords-file ./sherpa-onnx-kws-zipformer-wenetspeech-3.3M-2024-01-01/test_wavs/test_keywords.txt \
--input-wav ./sherpa-onnx-kws-zipformer-wenetspeech-3.3M-2024-01-01/test_wavs/3.wav