From 4d6b3110da7018d87c1a612dec1205c6dff1189e Mon Sep 17 00:00:00 2001 From: Anthony Pontet Date: Wed, 10 Jun 2020 19:46:53 +0200 Subject: [PATCH 1/3] fix divison by zero on alt_conf variable --- grpc/stt_server.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/grpc/stt_server.py b/grpc/stt_server.py index 3ddf6dc..dc38e54 100755 --- a/grpc/stt_server.py +++ b/grpc/stt_server.py @@ -68,7 +68,10 @@ def get_response(self, json_res): else: words = [self.get_word_info(x) for x in res.get('result', [])] confs = [w.confidence for w in words] - alt_conf = sum(confs) / len(confs) + if len(confs) == 0: + alt_conf = 0 + else : + alt_conf = sum(confs) / len(confs) alternatives = [stt_service_pb2.SpeechRecognitionAlternative(text=res['text'], words=words, confidence=alt_conf)] chunks = [stt_service_pb2.SpeechRecognitionChunk(alternatives=alternatives, final=True)] return stt_service_pb2.StreamingRecognitionResponse(chunks=chunks) From de3b9097df3f2bdecb300564d8ea44398169cf13 Mon Sep 17 00:00:00 2001 From: Anthony Pontet Date: Thu, 11 Jun 2020 15:42:05 +0200 Subject: [PATCH 2/3] add grpc client example to test asr with microphone --- grpc/mic_stt_client.py | 66 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100755 grpc/mic_stt_client.py diff --git a/grpc/mic_stt_client.py b/grpc/mic_stt_client.py new file mode 100755 index 0000000..d12e3ac --- /dev/null +++ b/grpc/mic_stt_client.py @@ -0,0 +1,66 @@ +#!/usr/bin/python3 + +import argparse +import grpc + +import pyaudio + +import stt_service_pb2 +import stt_service_pb2_grpc + +CHUNK_SIZE = 4000 +CHUNK = 8000 +FORMAT = pyaudio.paInt16 +CHANNELS = 1 +RATE = 16000 + +def gen(audio_file_name): + specification = stt_service_pb2.RecognitionSpec( + partial_results=True, + audio_encoding='LINEAR16_PCM', + sample_rate_hertz=16000 + ) + streaming_config = stt_service_pb2.RecognitionConfig(specification=specification) + + yield stt_service_pb2.StreamingRecognitionRequest(config=streaming_config) + + p = pyaudio.PyAudio() + stream = p.open(format=FORMAT, channels=CHANNELS, rate=RATE, input=True, frames_per_buffer=CHUNK) + stream.start_stream() + + while True: + data = stream.read(CHUNK_SIZE) + if len(data) == 0: + break + else: + yield stt_service_pb2.StreamingRecognitionRequest(audio_content=data) + + + +def run(audio_file_name): + channel = grpc.insecure_channel('localhost:5001') + stub = stt_service_pb2_grpc.SttServiceStub(channel) + it = stub.StreamingRecognize(gen(audio_file_name)) + + try: + for r in it: + try: + print('Start chunk: ') + for alternative in r.chunks[0].alternatives: + print('alternative: ', alternative.text) + print('alternative_confidence: ', alternative.confidence) + print('words: ', alternative.words) + print('Is final: ', r.chunks[0].final) + print('') + except LookupError: + print('No available chunks') + except grpc._channel._Rendezvous as err: + print('Error code %s, message: %s' % (err._state.code, err._state.details)) + + +if __name__ == '__main__': + parser = argparse.ArgumentParser() + parser.add_argument('--path', required=True, help='audio file path') + args = parser.parse_args() + + run(args.path) From 21876eb00b300cbf4005e5bbd72cc8a2f1a5daa5 Mon Sep 17 00:00:00 2001 From: Anthony Pontet Date: Fri, 12 Jun 2020 21:23:06 +0200 Subject: [PATCH 3/3] allow user to select the right input device and use the default rate of the selected input to configure the stream --- grpc/mic_stt_client.py | 50 ++++++++++++++++++++++++++++++++++-------- 1 file changed, 41 insertions(+), 9 deletions(-) diff --git a/grpc/mic_stt_client.py b/grpc/mic_stt_client.py index d12e3ac..9629b48 100755 --- a/grpc/mic_stt_client.py +++ b/grpc/mic_stt_client.py @@ -4,17 +4,31 @@ import grpc import pyaudio +import sys import stt_service_pb2 import stt_service_pb2_grpc +# config CHUNK_SIZE = 4000 -CHUNK = 8000 +BUFFER_SIZE = 8000 FORMAT = pyaudio.paInt16 CHANNELS = 1 -RATE = 16000 +RATE = None +DEVICE_INDEX = None -def gen(audio_file_name): +p = pyaudio.PyAudio() + + +def list_devices(): + print("List of all devices detected by PyAudio (Index - Name) :") + print("-------------------------------------------------------") + for x in range(p.get_device_count()): + info_dic = p.get_device_info_by_index(x) + if info_dic['maxInputChannels'] > 0: + print(str(x) + " - " + info_dic['name']) + +def gen(): specification = stt_service_pb2.RecognitionSpec( partial_results=True, audio_encoding='LINEAR16_PCM', @@ -24,8 +38,14 @@ def gen(audio_file_name): yield stt_service_pb2.StreamingRecognitionRequest(config=streaming_config) - p = pyaudio.PyAudio() - stream = p.open(format=FORMAT, channels=CHANNELS, rate=RATE, input=True, frames_per_buffer=CHUNK) + stream = p.open( + format=FORMAT + ,channels=CHANNELS + ,rate=int(RATE) + ,input=True + ,input_device_index=DEVICE_INDEX + ,frames_per_buffer=BUFFER_SIZE) + stream.start_stream() while True: @@ -37,10 +57,10 @@ def gen(audio_file_name): -def run(audio_file_name): +def run(): channel = grpc.insecure_channel('localhost:5001') stub = stt_service_pb2_grpc.SttServiceStub(channel) - it = stub.StreamingRecognize(gen(audio_file_name)) + it = stub.StreamingRecognize(gen()) try: for r in it: @@ -60,7 +80,19 @@ def run(audio_file_name): if __name__ == '__main__': parser = argparse.ArgumentParser() - parser.add_argument('--path', required=True, help='audio file path') + parser.add_argument('--device-index', required=False, help='input device index') + parser.add_argument('--list-device', required=False, action="store_true", help='display device list') #just a flag args = parser.parse_args() - run(args.path) + if args.list_device == True: + list_devices() + sys.exit() + + if args.device_index is not None: + DEVICE_INDEX = args.device_index + RATE = p.get_device_info_by_index[DEVICE_INDEX]['defaultSampleRate'] + else : + DEVICE_INDEX = p.get_default_input_device_info()['index'] + RATE = p.get_default_input_device_info()['defaultSampleRate'] + + run()