From 30c2709e8cc838d30ce5e2e958fdd67455daebc7 Mon Sep 17 00:00:00 2001 From: jiegeaiai Date: Sat, 2 Nov 2024 10:59:59 +0800 Subject: [PATCH] modify asr test --- asr/sherpa_ncnn_asr.py | 43 +++++++++++++++++++++--------------------- test/test_asr_only.py | 36 +++++++++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+), 21 deletions(-) create mode 100644 test/test_asr_only.py diff --git a/asr/sherpa_ncnn_asr.py b/asr/sherpa_ncnn_asr.py index 35df115..d329193 100644 --- a/asr/sherpa_ncnn_asr.py +++ b/asr/sherpa_ncnn_asr.py @@ -64,24 +64,25 @@ class SherpaNcnnAsr(AsrBase): self._notify_complete('介绍中国5000年历史文学') segment_id += 1 time.sleep(60) - # - # with sd.InputStream(channels=1, dtype="float32", samplerate=self._sample_rate) as s: - # while not self._stop_event.is_set(): - # samples, _ = s.read(self._samples_per_read) # a blocking read - # samples = samples.reshape(-1) - # self._recognizer.accept_waveform(self._sample_rate, samples) - # - # is_endpoint = self._recognizer.is_endpoint - # - # result = self._recognizer.text - # if result and (last_result != result): - # last_result = result - # print("\r{}:{}".format(segment_id, result), end=".", flush=True) - # self._notify_process(result) - # - # if is_endpoint: - # if result: - # print("\r{}:{}".format(segment_id, result), flush=True) - # self._notify_complete(result) - # segment_id += 1 - # self._recognizer.reset() +''' + with sd.InputStream(channels=1, dtype="float32", samplerate=self._sample_rate) as s: + while not self._stop_event.is_set(): + samples, _ = s.read(self._samples_per_read) # a blocking read + samples = samples.reshape(-1) + self._recognizer.accept_waveform(self._sample_rate, samples) + + is_endpoint = self._recognizer.is_endpoint + + result = self._recognizer.text + if result and (last_result != result): + last_result = result + print("\r{}:{}".format(segment_id, result), end=".", flush=True) + self._notify_process(result) + + if is_endpoint: + if result: + print("\r{}:{}".format(segment_id, result), flush=True) + self._notify_complete(result) + segment_id += 1 + self._recognizer.reset() +''' diff --git a/test/test_asr_only.py b/test/test_asr_only.py new file mode 100644 index 0000000..83f4670 --- /dev/null +++ b/test/test_asr_only.py @@ -0,0 +1,36 @@ +#encoding = utf8 + +import sys +import time + +from asr import SherpaNcnnAsr + +try: + import sounddevice as sd +except ImportError as e: + print("Please install sounddevice first. You can use") + print() + print(" pip install sounddevice") + print() + print("to install it") + sys.exit(-1) + + +def main(): + print("Started! Please speak") + asr = SherpaNcnnAsr() + time.sleep(20) + print("Stop! ") + asr.stop() + + +if __name__ == "__main__": + devices = sd.query_devices() + print(devices) + default_input_device_idx = sd.default.device[0] + print(f'Use default device: {devices[default_input_device_idx]["name"]}') + + try: + main() + except KeyboardInterrupt: + print("\nCaught Ctrl + C. Exiting")