sherpa-onnx icon indicating copy to clipboard operation
sherpa-onnx copied to clipboard

Keyword Spotter not detecting keywords - always returns null/empty

Open dlangamandla opened this issue 7 months ago • 8 comments

I need assistance with understanding why my mic cant detect any keywords :

this is how i set it up , howvever everytime i try to print the keyword it shows null/empty--

Future record() async { final spotter = sherpa_onnx.KeywordSpotter(config!); final stream = spotter.createStream(); assert(_mRecorderIsInited && mPlayer!.isStopped); var sink = await createFile(); var recordingDataController = StreamController<Uint8List>(); _mRecordingDataSubscription = recordingDataController.stream.listen((buffer) { sink.add(buffer); bufferUint8.add(buffer); final samplesFloat32 = convertBytesToFloat32(buffer);

  stream.acceptWaveform(
    samples: samplesFloat32,
    sampleRate: 16000, 
  );

  while (spotter.isReady(stream)) {
    spotter.decode(stream);
    final keyword = spotter.getResult(stream).keyword;
    if (keyword.isNotEmpty) {
      print('Detected: $keyword');
      spotter.reset(stream); 
    } else {
      print("undetected");
    }
  }
});

await mRecorder!.startRecorder(
  toStream: recordingDataController.sink,
  codec: Codec.pcm16,
  numChannels: 1,
  sampleRate: 16000,
);

// Start a timer that stops the recorder after 10 seconds

notifyListeners();

}

Future initKWS() async { sherpa_onnx.initBindings(); print("init kws"); await copyAllAssetFiles();

String decoder = await getLocalFilePathKWS('decoder-epoch.int8.onnx');
String encoder = await getLocalFilePathKWS('encoder-epoch.int8.onnx');
String tokens = await getLocalFilePathKWS('tokens.txt');
String keywords = await getLocalFilePathKWS('keywords.txt');
String bpeModel = await getLocalFilePathKWS('bpe.model');
String joiner = await getLocalFilePathKWS('joiner-epoch.int8.onnx');

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 keywordConfig = sherpa_onnx.KeywordSpotterConfig(
  model: modelConfig,
  keywordsFile: keywords,
);

config = keywordConfig;

}

dlangamandla avatar Apr 09 '25 17:04 dlangamandla