Skip to content

Fix displaying streaming speech recognition results for Python. #2196

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 9, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ project(sherpa-onnx)
# Remember to update
# ./CHANGELOG.md
# ./new-release.sh
set(SHERPA_ONNX_VERSION "1.11.5")
set(SHERPA_ONNX_VERSION "1.11.6")

# Disable warning about
#
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
# to download pre-trained models

import argparse
import sys
from pathlib import Path

import sherpa_onnx


Expand Down Expand Up @@ -202,8 +202,8 @@ def main():

stream = recognizer.create_stream()

last_result = ""
segment_id = 0
display = sherpa_onnx.Display()

while True:
samples = alsa.read(samples_per_read) # a blocking read
stream.accept_waveform(sample_rate, samples)
Expand All @@ -214,13 +214,14 @@ def main():

result = recognizer.get_result(stream)

if result and (last_result != result):
last_result = result
print("\r{}:{}".format(segment_id, result), end="", flush=True)
display.update_text(result)
display.display()

if is_endpoint:
if result:
print("\r{}:{}".format(segment_id, result), flush=True)
segment_id += 1
display.finalize_current_sentence()
display.display()

recognizer.reset(stream)


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -192,8 +192,8 @@ def main():

stream = recognizer.create_stream()

last_result = ""
segment_id = 0
display = sherpa_onnx.Display()

with sd.InputStream(channels=1, dtype="float32", samplerate=sample_rate) as s:
while True:
samples, _ = s.read(samples_per_read) # a blocking read
Expand All @@ -206,13 +206,14 @@ def main():

result = recognizer.get_result(stream)

if result and (last_result != result):
last_result = result
print("\r{}:{}".format(segment_id, result), end="", flush=True)
display.update_text(result)
display.display()

if is_endpoint:
if result:
print("\r{}:{}".format(segment_id, result), flush=True)
segment_id += 1
display.finalize_current_sentence()
display.display()

recognizer.reset(stream)


Expand Down
14 changes: 7 additions & 7 deletions python-api-examples/speech-recognition-from-url.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,8 +192,7 @@ def main():

stream = recognizer.create_stream()

last_result = ""
segment_id = 0
display = sherpa_onnx.Display()

print("Started!")
while True:
Expand All @@ -213,13 +212,14 @@ def main():

result = recognizer.get_result(stream)

if result and (last_result != result):
last_result = result
print("\r{}:{}".format(segment_id, result), end="", flush=True)
display.update_text(result)
display.display()

if is_endpoint:
if result:
print("\r{}:{}".format(segment_id, result), flush=True)
segment_id += 1
display.finalize_current_sentence()
display.display()

recognizer.reset(stream)


Expand Down
15 changes: 8 additions & 7 deletions python-api-examples/streaming-paraformer-asr-microphone.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ def main():

stream = recognizer.create_stream()

last_result = ""
segment_id = 0
display = sherpa_onnx.Display()

with sd.InputStream(channels=1, dtype="float32", samplerate=sample_rate) as s:
while True:
samples, _ = s.read(samples_per_read) # a blocking read
Expand All @@ -88,13 +88,14 @@ def main():

result = recognizer.get_result(stream)

if result and (last_result != result):
last_result = result
print("\r{}:{}".format(segment_id, result), end="", flush=True)
display.update_text(result)
display.display()

if is_endpoint:
if result:
print("\r{}:{}".format(segment_id, result), flush=True)
segment_id += 1
display.finalize_current_sentence()
display.display()

recognizer.reset(stream)


Expand Down
25 changes: 6 additions & 19 deletions python-api-examples/two-pass-speech-recognition-from-microphone.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@
import argparse
import sys
from pathlib import Path
from typing import List

import numpy as np

Expand Down Expand Up @@ -375,8 +374,7 @@ def main():
samples_per_read = int(0.1 * sample_rate) # 0.1 second = 100 ms
stream = first_recognizer.create_stream()

last_result = ""
segment_id = 0
display = sherpa_onnx.Display()

sample_buffers = []
with sd.InputStream(channels=1, dtype="float32", samplerate=sample_rate) as s:
Expand All @@ -395,14 +393,8 @@ def main():
result = first_recognizer.get_result(stream)
result = result.lower().strip()

if last_result != result:
print(
"\r{}:{}".format(segment_id, " " * len(last_result)),
end="",
flush=True,
)
last_result = result
print("\r{}:{}".format(segment_id, result), end="", flush=True)
display.update_text(result)
display.display()

if is_endpoint:
if result:
Expand All @@ -419,14 +411,9 @@ def main():
sample_rate=sample_rate,
)
result = result.lower().strip()

print(
"\r{}:{}".format(segment_id, " " * len(last_result)),
end="",
flush=True,
)
print("\r{}:{}".format(segment_id, result), flush=True)
segment_id += 1
display.update_text(result)
display.finalize_current_sentence()
display.display()
else:
sample_buffers = []

Expand Down
2 changes: 1 addition & 1 deletion sherpa-onnx/python/sherpa_onnx/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
AudioTaggingModelConfig,
CircularBuffer,
DenoisedAudio,
Display,
FastClustering,
FastClusteringConfig,
OfflinePunctuation,
Expand Down Expand Up @@ -48,6 +47,7 @@
write_wave,
)

from .display import Display
from .keyword_spotter import KeywordSpotter
from .offline_recognizer import OfflineRecognizer
from .online_recognizer import OnlineRecognizer
Expand Down
41 changes: 41 additions & 0 deletions sherpa-onnx/python/sherpa_onnx/display.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# Copyright (c) 2025 Xiaomi Corporation
import os
from time import gmtime, strftime


def get_current_time():
return strftime("%Y-%m-%d %H:%M:%S", gmtime())


def clear_console():
os.system("cls" if os.name == "nt" else "clear")


class Display:
def __init__(self):
self.sentences = []
self.currentText = ""

def update_text(self, text):
self.currentText = text

def finalize_current_sentence(self):
if self.currentText.strip():
self.sentences.append((get_current_time(), self.currentText))

self.currentText = ""

def display(self):
clear_console()
print("=== Speech Recognition with Next-gen Kaldi ===")
print("Time:", get_current_time())
print("-" * 30)

# display history sentences
if self.sentences:
for i, (when, text) in enumerate(self.sentences):
print(f"[{when}] {i + 1}. {text}")
print("-" * 30)

if self.currentText.strip():
print("Recognizing:", self.currentText)
Loading