From fb1e24bebb7f64df54299b470342200ea4ebd97e Mon Sep 17 00:00:00 2001 From: Fangjun Kuang Date: Sat, 25 Feb 2023 14:30:44 +0800 Subject: [PATCH] Fix endpointing with microphone (#64) --- ffmpeg-examples/sherpa-onnx-ffmpeg.c | 2 ++ ...-recognition-from-microphone-with-endpoint-detection.py | 5 +++-- sherpa-onnx/csrc/sherpa-onnx-alsa.cc | 7 +++++-- sherpa-onnx/csrc/sherpa-onnx-microphone.cc | 7 +++++-- 4 files changed, 15 insertions(+), 6 deletions(-) diff --git a/ffmpeg-examples/sherpa-onnx-ffmpeg.c b/ffmpeg-examples/sherpa-onnx-ffmpeg.c index 28027d83..3a8ce271 100644 --- a/ffmpeg-examples/sherpa-onnx-ffmpeg.c +++ b/ffmpeg-examples/sherpa-onnx-ffmpeg.c @@ -232,6 +232,8 @@ static void sherpa_decode_frame(const AVFrame *frame, SherpaOnnxOnlineRecognizer fprintf(stderr, "%s\n", r->text); } DestroyOnlineRecognizerResult(r); + + Reset(recognizer, stream); } nb_samples = 0; } diff --git a/python-api-examples/speech-recognition-from-microphone-with-endpoint-detection.py b/python-api-examples/speech-recognition-from-microphone-with-endpoint-detection.py index 4d2992ac..4d148d1a 100644 --- a/python-api-examples/speech-recognition-from-microphone-with-endpoint-detection.py +++ b/python-api-examples/speech-recognition-from-microphone-with-endpoint-detection.py @@ -68,8 +68,9 @@ def main(): last_result = result print(f"{segment_id}: {result}") - if result and is_endpoint: - segment_id += 1 + if is_endpoint: + if result: + segment_id += 1 recognizer.reset(stream) diff --git a/sherpa-onnx/csrc/sherpa-onnx-alsa.cc b/sherpa-onnx/csrc/sherpa-onnx-alsa.cc index f773617a..0d57b6c6 100644 --- a/sherpa-onnx/csrc/sherpa-onnx-alsa.cc +++ b/sherpa-onnx/csrc/sherpa-onnx-alsa.cc @@ -132,8 +132,11 @@ as the device_name. display.Print(segment_index, text); } - if (!text.empty() && is_endpoint) { - ++segment_index; + if (is_endpoint) { + if (!text.empty()) { + ++segment_index; + } + recognizer.Reset(stream.get()); } } diff --git a/sherpa-onnx/csrc/sherpa-onnx-microphone.cc b/sherpa-onnx/csrc/sherpa-onnx-microphone.cc index e8d4f56a..2150d48b 100644 --- a/sherpa-onnx/csrc/sherpa-onnx-microphone.cc +++ b/sherpa-onnx/csrc/sherpa-onnx-microphone.cc @@ -147,8 +147,11 @@ for a list of pre-trained models to download. display.Print(segment_index, text); } - if (!text.empty() && is_endpoint) { - ++segment_index; + if (is_endpoint) { + if (!text.empty()) { + ++segment_index; + } + recognizer.Reset(s.get()); }