def main(input_uri, encoding, sample_rate, language_code='en-US'): service = cloud_speech.beta_create_Speech_stub( make_channel('speech.googleapis.com', 443)) # The method and parameters can be inferred from the proto from which the # grpc client lib was generated. See: # https://github.com/googleapis/googleapis/blob/master/google/cloud/speech/v1beta1/cloud_speech.proto response = service.SyncRecognize(cloud_speech.SyncRecognizeRequest( config=cloud_speech.RecognitionConfig( # There are a bunch of config options you can specify. See # https://goo.gl/KPZn97 for the full list. encoding=encoding, # one of LINEAR16, FLAC, MULAW, AMR, AMR_WB sample_rate=sample_rate, # the rate in hertz # See https://g.co/cloud/speech/docs/languages for a list of # supported languages. language_code=language_code, # a BCP-47 language tag ), audio=cloud_speech.RecognitionAudio( uri=input_uri, ) ), DEADLINE_SECS) # Print the recognition result alternatives and confidence scores. for result in response.results: print('Result:') for alternative in result.alternatives: print(u' ({}): {}'.format( alternative.confidence, alternative.transcript))
def main(input_uri, encoding, sample_rate, language_code='en-US'): service = cloud_speech.beta_create_Speech_stub( make_channel('speech.googleapis.com', 443)) # The method and parameters can be inferred from the proto from which the # grpc client lib was generated. See: # https://github.com/googleapis/googleapis/blob/master/google/cloud/speech/v1beta1/cloud_speech.proto response = service.SyncRecognize( cloud_speech.SyncRecognizeRequest( config=cloud_speech.RecognitionConfig( # There are a bunch of config options you can specify. See # https://goo.gl/KPZn97 for the full list. encoding=encoding, # one of LINEAR16, FLAC, MULAW, AMR, AMR_WB sample_rate=sample_rate, # the rate in hertz # See https://g.co/cloud/speech/docs/languages for a list of # supported languages. language_code=language_code, # a BCP-47 language tag ), audio=cloud_speech.RecognitionAudio(uri=input_uri, )), DEADLINE_SECS) # Print the recognition result alternatives and confidence scores. for result in response.results: print('Result:') for alternative in result.alternatives: print(u' ({}): {}'.format(alternative.confidence, alternative.transcript))
def main(): with cloud_speech.beta_create_Speech_stub( make_channel('speech.googleapis.com', 443)) as service: # stoprequest is event object which is set in `listen_print_loop` # to indicate that the trancsription should be stopped. # # The `_fill_buffer` thread checks this object, and closes # the `audio_stream` once it's set. stoprequest = threading.Event() # For streaming audio from the microphone, there are three threads. # First, a thread that collects audio data as it comes in with record_audio(RATE, CHUNK, stoprequest) as buffered_audio_data: # Second, a thread that sends requests with that data requests = request_stream(buffered_audio_data, RATE) # Third, a thread that listens for transcription responses recognize_stream = service.StreamingRecognize( requests, DEADLINE_SECS) # Exit things cleanly on interrupt signal.signal(signal.SIGINT, lambda *_: recognize_stream.cancel()) # Now, put the transcription responses to use. try: listen_print_loop(recognize_stream, stoprequest) recognize_stream.cancel() except face.CancellationError: # This happens because of the interrupt handler pass
def run_recognition_loop(): global frames global silent_frames global is_recording global should_finish_stream if len(silent_frames) > 4: silent_frames = silent_frames[-4:] while not is_recording: time.sleep(args.frame_seconds // 4) if len(frames) > 4: for frame_index in range(4): data = frames[frame_index] rms = audioop.rms(data, 2) decibel = 20 * math.log10(rms) if rms > 0 else 0 if decibel < args.silent_decibel: silent_frames += frames[0:frame_index+1] del frames[0:frame_index + 1] return is_recording = True frames = silent_frames + frames silent_frames = [] with cloud_speech_pb2.beta_create_Speech_stub(make_channel(args.host, args.ssl_port)) as service: try: listen_loop(service.StreamingRecognize(request_stream(), args.deadline_seconds)) printr(" ".join((bold(recognition_result.transcription), " ", "confidence: ", str(int(recognition_result.confidence * 100)), "%"))) print() except Exception as e: print(str(e))
def run_recognition_loop(): global frames global silent_frames global is_recording global should_finish_stream if len(silent_frames) > 4: silent_frames = silent_frames[-4:] #print ("silent_frames:",silent_frames) while not is_recording: #print("---sleep---") time.sleep(args.frame_seconds // 4) #処理を一時停止する #print("----on-----") #なんのif文かわからん if len(frames) > 2: for frame_index in range(4): data = frames[frame_index] rms = audioop.rms(data, 2) decibel = 20 * math.log10(rms) if rms > 0 else 0 if decibel < args.silent_decibel: silent_frames += frames[0:frame_index + 1] del frames[0:frame_index + 1] #リストの部分を削除 return is_recording = True frames = silent_frames + frames silent_frames = [] #音声をテキストにする with cloud_speech_pb2.beta_create_Speech_stub( make_channel(args.host, args.ssl_port)) as service: #このフォルダをservice関数に入れる try: #ここで録音して変換している print("処理中") listen_loop( service.StreamingRecognize(request_stream(), args.deadline_seconds)) #print("service:",service.StreamingRecognize(request_stream(), args.deadline_seconds)) #最終的な結果はここ printr(" ".join((bold(recognition_result.transcription)))) #" ", #"confidence: ", str(int(recognition_result.confidence * 100)), "%", #"time: ",time.strftime("%H:%M:%S")))) list = { "ID": ID, "trans": recognition_result.transcription, "confidence": recognition_result.confidence * 100, "time": time.strftime("%H:%M:%S"), "finish": finish } json_data = json.dumps(list) response = requests.post( 'https://2dhdoj4j8c.execute-api.ap-northeast-1.amazonaws.com:443/dev/datadump', json_data) #pprint.pprint(response.json()) print() except Exception as e: print(str(e))
def main(): stop_audio = threading.Event() with cloud_speech.beta_create_Speech_stub(make_channel("speech.googleapis.com", 443)) as service: try: listen_print_loop(service.StreamingRecognize(request_stream(stop_audio), DEADLINE_SECS)) finally: # Stop the request stream once we're done with the loop - otherwise # it'll keep going in the thread that the grpc lib makes for it.. stop_audio.set()
def threaded_listen(): with cloud_speech.beta_create_Speech_stub(self.google_grpc_channel('speech.googleapis.com', 443)) as service: with source as s: while self._running_voice: try: # listen for 1 second, then check again if the stop function has been called init_buff = self.listen_trigger(s, 1) except WaitTimeoutError: # listening timed out, just try again pass else: self.g_stream_loop(service, source, init_buff, callback)
def main(): stop_audio = threading.Event() with cloud_speech.beta_create_Speech_stub( make_channel('speech.googleapis.com', 443)) as service: try: listen_print_loop( service.StreamingRecognize(request_stream(stop_audio), DEADLINE_SECS)) finally: # Stop the request stream once we're done with the loop - otherwise # it'll keep going in the thread that the grpc lib makes for it.. stop_audio.set()
def __init__(self): self.pub_transcript = rospy.Publisher('result_transcript', ResultTranscript, queue_size=5) self.pub_start_speech = rospy.Publisher('start_of_speech', Empty, queue_size=10) self.pub_end_speech = rospy.Publisher('end_of_speech', Empty, queue_size=10) self.is_start_audio = False self.is_start_speech = False self.is_stop_audio = True self.stop_audio = threading.Event() with cloud_speech.beta_create_Speech_stub(self.make_channel('speech.googleapis.com', 443)) as self.service: self.t1 = threading.Thread(target=self.listen_print_loop) self.t1.start()
def main(input_uri, encoding, sample_rate, language_code='en-US'): channel = make_channel('speech.googleapis.com', 443) service = cloud_speech_pb2.beta_create_Speech_stub(channel) # The method and parameters can be inferred from the proto from which the # grpc client lib was generated. See: # https://github.com/googleapis/googleapis/blob/master/google/cloud/speech/v1beta1/cloud_speech.proto operation = service.AsyncRecognize(cloud_speech_pb2.AsyncRecognizeRequest( config=cloud_speech_pb2.RecognitionConfig( # There are a bunch of config options you can specify. See # https://goo.gl/KPZn97 for the full list. encoding=encoding, # one of LINEAR16, FLAC, MULAW, AMR, AMR_WB sample_rate=sample_rate, # the rate in hertz # See https://g.co/cloud/speech/docs/languages for a list of # supported languages. language_code=language_code, # a BCP-47 language tag ), audio=cloud_speech_pb2.RecognitionAudio( uri=input_uri, ) ), DEADLINE_SECS) # Print the longrunning operation handle. print(operation) # Construct a long running operation endpoint. service = operations_grpc_pb2.beta_create_Operations_stub(channel) name = operation.name while True: # Give the server a few seconds to process. print('Waiting for server processing...') time.sleep(1) operation = service.GetOperation( operations_grpc_pb2.GetOperationRequest(name=name), DEADLINE_SECS) if operation.done: break response = cloud_speech_pb2.AsyncRecognizeResponse() operation.response.Unpack(response) # Print the recognition result alternatives and confidence scores. for result in response.results: print('Result:') for alternative in result.alternatives: print(u' ({}): {}'.format( alternative.confidence, alternative.transcript))
def main(input_uri, encoding, sample_rate): channel = make_channel('speech.googleapis.com', 443) service = cloud_speech_pb2.beta_create_Speech_stub(channel) # The method and parameters can be inferred from the proto from which the # grpc client lib was generated. See: # https://github.com/googleapis/googleapis/blob/master/google/cloud/speech/v1beta1/cloud_speech.proto response = service.AsyncRecognize(cloud_speech_pb2.AsyncRecognizeRequest( config=cloud_speech_pb2.RecognitionConfig( # There are a bunch of config options you can specify. See # https://goo.gl/KPZn97 for the full list. encoding=encoding, # one of LINEAR16, FLAC, MULAW, AMR, AMR_WB sample_rate=sample_rate, # the rate in hertz # See # https://g.co/cloud/speech/docs/best-practices#language_support # for a list of supported languages. language_code='fi-FI', # a BCP-47 language tag ), audio=cloud_speech_pb2.RecognitionAudio( uri=input_uri, ) ), DEADLINE_SECS) # Print the longrunning operation handle. print >> sys.stderr, response # Construct a long running operation endpoint. service = operations_grpc_pb2.beta_create_Operations_stub(channel) name = response.name while True: # Give the server a few seconds to process. print >> sys.stderr, 'Waiting for server processing...' time.sleep(1) # Get the long running operation with response. response = service.GetOperation( operations_grpc_pb2.GetOperationRequest(name=name), DEADLINE_SECS) if response.done: break # Print the recognition results. results = cloud_speech_pb2.AsyncRecognizeResponse() response.response.Unpack(results) for result in results.results: for alternative in result.alternatives: print(('"{}",{}').format(alternative.transcript.encode('utf-8'), alternative.confidence))
def main(input_uri, encoding, sample_rate): service = cloud_speech.beta_create_Speech_stub( make_channel('speech.googleapis.com', 443)) # The method and parameters can be inferred from the proto from which the # grpc client lib was generated. See: # https://github.com/googleapis/googleapis/blob/master/google/cloud/speech/v1beta1/cloud_speech.proto response = service.SyncRecognize( cloud_speech.SyncRecognizeRequest( config=cloud_speech.RecognitionConfig( encoding=encoding, sample_rate=sample_rate, ), audio=cloud_speech.RecognitionAudio(uri=input_uri, )), DEADLINE_SECS) # Print the recognition results. print(response.results)
def main(input_uri, encoding, sample_rate, language_code='en-US'): channel = make_channel('speech.googleapis.com', 443) service = cloud_speech_pb2.beta_create_Speech_stub(channel) # The method and parameters can be inferred from the proto from which the # grpc client lib was generated. See: # https://github.com/googleapis/googleapis/blob/master/google/cloud/speech/v1beta1/cloud_speech.proto operation = service.AsyncRecognize( cloud_speech_pb2.AsyncRecognizeRequest( config=cloud_speech_pb2.RecognitionConfig( # There are a bunch of config options you can specify. See # https://goo.gl/KPZn97 for the full list. encoding=encoding, # one of LINEAR16, FLAC, MULAW, AMR, AMR_WB sample_rate=sample_rate, # the rate in hertz # See https://g.co/cloud/speech/docs/languages for a list of # supported languages. language_code=language_code, # a BCP-47 language tag ), audio=cloud_speech_pb2.RecognitionAudio(uri=input_uri, )), DEADLINE_SECS) # Print the longrunning operation handle. print(operation) # Construct a long running operation endpoint. service = operations_grpc_pb2.beta_create_Operations_stub(channel) name = operation.name while True: # Give the server a few seconds to process. print('Waiting for server processing...') time.sleep(1) operation = service.GetOperation( operations_grpc_pb2.GetOperationRequest(name=name), DEADLINE_SECS) if operation.done: break response = cloud_speech_pb2.AsyncRecognizeResponse() operation.response.Unpack(response) # Print the recognition result alternatives and confidence scores. for result in response.results: print('Result:') for alternative in result.alternatives: print(u' ({}): {}'.format(alternative.confidence, alternative.transcript))
def main(input_uri, encoding, sample_rate): service = cloud_speech.beta_create_Speech_stub( make_channel('speech.googleapis.com', 443)) # The method and parameters can be inferred from the proto from which the # grpc client lib was generated. See: # https://github.com/googleapis/googleapis/blob/master/google/cloud/speech/v1beta1/cloud_speech.proto response = service.SyncRecognize(cloud_speech.SyncRecognizeRequest( config=cloud_speech.RecognitionConfig( encoding=encoding, sample_rate=sample_rate, ), audio=cloud_speech.RecognitionAudio( uri=input_uri, ) ), DEADLINE_SECS) # Print the recognition results. print(response.results)
def main(): #メイン部分 global queue #列 global should_finish_stream preloading_process = Process(target=reading_audio_loop, args=[queue]) preloading_process.start() while True: should_finish_stream = False with cloud_speech_pb2.beta_create_Speech_stub(make_channel(args.host, args.ssl_port)) as service: listen_loop(service.StreamingRecognize(request_stream(), args.deadline_seconds)) if recognition_result.success: printr(" ".join((bold(recognition_result.transcription), " ", "confidence: ", str(int(recognition_result.confidence * 100)), "%"))) print()
def main(input_uri, encoding, sample_rate): channel = make_channel('speech.googleapis.com', 443) service = cloud_speech_pb2.beta_create_Speech_stub(channel) # The method and parameters can be inferred from the proto from which the # grpc client lib was generated. See: # https://github.com/googleapis/googleapis/blob/master/google/cloud/speech/v1beta1/cloud_speech.proto response = service.AsyncRecognize(cloud_speech_pb2.AsyncRecognizeRequest( config=cloud_speech_pb2.RecognitionConfig( # There are a bunch of config options you can specify. See # https://goo.gl/A6xv5G for the full list. encoding=encoding, # one of LINEAR16, FLAC, MULAW, AMR, AMR_WB sample_rate=sample_rate, # the rate in hertz # See # https://g.co/cloud/speech/docs/best-practices#language_support # for a list of supported languages. language_code='en-US', # a BCP-47 language tag ), audio=cloud_speech_pb2.RecognitionAudio( uri=input_uri, ) ), DEADLINE_SECS) # Print the longrunning operation handle. print(response) # Construct a long running operation endpoint. service = operations_grpc_pb2.beta_create_Operations_stub(channel) name = response.name while True: # Give the server a few seconds to process. print('Waiting for server processing...') time.sleep(1) # Get the long running operation with response. response = service.GetOperation( operations_grpc_pb2.GetOperationRequest(name=name), DEADLINE_SECS) if response.done: break # Print the recognition results. results = cloud_speech_pb2.AsyncRecognizeResponse() response.response.Unpack(results) print(results)
def run_recognition_loop(self): print("===start recognition") #if len(self.silent_frames) > 4: # self.silent_frames = self.silent_frames[-4:] #変換を依頼している時には、 #while not self.is_recording: while not self.should_finish_stream : #print(sys._getframe().f_code.co_name) time.sleep(self.frame_seconds // 4) if len(self.frames) == 0 : continue #音が入ってくるまで、解析処理を行わないようにする。 data = self.frames[0] rms = audioop.rms(data, 2) decibel = 20 * math.log10(rms) if rms > 0 else 0 #print("decibel", decibel) #音が入ってくるまで、処理を行わない。 if decibel < self.silent_decibel: #print("pre frame len",len(self.frames)) if len(self.frames) > 0 : self.frames.pop(0) continue print(sys._getframe().f_code.co_name, "get sound. decibel=",decibel) self.is_recording = True self.frames = self.silent_frames + self.frames with cloud_speech_pb2.beta_create_Speech_stub(self.make_channel(self.host, self.ssl_port)) as service: try: self.listen_loop(service.StreamingRecognize(self.request_stream(), self.deadline_seconds)) #処理結果が終了した時だけ出力するようにした if self.recognition_result.is_final : printr(" ".join((bold(self.recognition_result.transcription), " ", "confidence: ", str(int(self.recognition_result.confidence * 100)), "%"))) self.recognition_result.is_final = False #with open('result.txt', 'w') as f: # f.write(recognition_result.transcription) except Exception as e: print(str(e))
def main(input_uri, encoding, sample_rate): channel = make_channel('speech.googleapis.com', 443) service = cloud_speech_pb2.beta_create_Speech_stub(channel) # The method and parameters can be inferred from the proto from which the # grpc client lib was generated. See: # https://github.com/googleapis/googleapis/blob/master/google/cloud/speech/v1beta1/cloud_speech.proto response = service.AsyncRecognize(cloud_speech_pb2.AsyncRecognizeRequest( config=cloud_speech_pb2.RecognitionConfig( encoding=encoding, sample_rate=sample_rate, ), audio=cloud_speech_pb2.RecognitionAudio( uri=input_uri, ) ), DEADLINE_SECS) # Print the longrunning operation handle. print(response) # Construct a long running operation endpoint. service = operations_grpc_pb2.beta_create_Operations_stub(channel) name = response.name while True: # Give the server a few seconds to process. print('Waiting for server processing...') time.sleep(1) # Get the long running operation with response. response = service.GetOperation( operations_grpc_pb2.GetOperationRequest(name=name), DEADLINE_SECS) if response.done: break # Print the recognition results. results = cloud_speech_pb2.AsyncRecognizeResponse() response.response.Unpack(results) print(results)
def main(input_uri, encoding, sample_rate): service = cloud_speech.beta_create_Speech_stub( make_channel('speech.googleapis.com', 443)) # The method and parameters can be inferred from the proto from which the # grpc client lib was generated. See: # https://github.com/googleapis/googleapis/blob/master/google/cloud/speech/v1beta1/cloud_speech.proto response = service.SyncRecognize( cloud_speech.SyncRecognizeRequest( config=cloud_speech.RecognitionConfig( # There are a bunch of config options you can specify. See # https://goo.gl/A6xv5G for the full list. encoding=encoding, # one of LINEAR16, FLAC, MULAW, AMR, AMR_WB sample_rate=sample_rate, # the rate in hertz # See # https://g.co/cloud/speech/docs/best-practices#language_support # for a list of supported languages. language_code='en-US', # a BCP-47 language tag ), audio=cloud_speech.RecognitionAudio(uri=input_uri, )), DEADLINE_SECS) # Print the recognition results. print(response.results)
def main(input_uri, encoding, sample_rate): channel = make_channel('speech.googleapis.com', 443) service = cloud_speech_pb2.beta_create_Speech_stub(channel) # The method and parameters can be inferred from the proto from which the # grpc client lib was generated. See: # https://github.com/googleapis/googleapis/blob/master/google/cloud/speech/v1beta1/cloud_speech.proto response = service.AsyncRecognize( cloud_speech_pb2.AsyncRecognizeRequest( config=cloud_speech_pb2.RecognitionConfig( encoding=encoding, sample_rate=sample_rate, ), audio=cloud_speech_pb2.RecognitionAudio(uri=input_uri, )), DEADLINE_SECS) # Print the longrunning operation handle. print(response) # Construct a long running operation endpoint. service = operations_grpc_pb2.beta_create_Operations_stub(channel) name = response.name while True: # Give the server a few seconds to process. print('Waiting for server processing...') time.sleep(1) # Get the long running operation with response. response = service.GetOperation( operations_grpc_pb2.GetOperationRequest(name=name), DEADLINE_SECS) if response.done: break # Print the recognition results. results = cloud_speech_pb2.AsyncRecognizeResponse() response.response.Unpack(results) print(results)
def main(): with cloud_speech.beta_create_Speech_stub( make_channel('speech.googleapis.com', 443)) as service: # For streaming audio from the microphone, there are three threads. # First, a thread that collects audio data as it comes in with record_audio(RATE, CHUNK) as buffered_audio_data: # Second, a thread that sends requests with that data requests = request_stream(buffered_audio_data, RATE) # Third, a thread that listens for transcription responses recognize_stream = service.StreamingRecognize( requests, DEADLINE_SECS) # Exit things cleanly on interrupt signal.signal(signal.SIGINT, lambda *_: recognize_stream.cancel()) # Now, put the transcription responses to use. try: listen_print_loop(recognize_stream) recognize_stream.cancel() except face.CancellationError: # This happens because of the interrupt handler pass
def main(input_uri, encoding, sample_rate): service = cloud_speech.beta_create_Speech_stub( make_channel('speech.googleapis.com', 443)) # The method and parameters can be inferred from the proto from which the # grpc client lib was generated. See: # https://github.com/googleapis/googleapis/blob/master/google/cloud/speech/v1beta1/cloud_speech.proto response = service.SyncRecognize(cloud_speech.SyncRecognizeRequest( config=cloud_speech.RecognitionConfig( # There are a bunch of config options you can specify. See # https://goo.gl/A6xv5G for the full list. encoding=encoding, # one of LINEAR16, FLAC, MULAW, AMR, AMR_WB sample_rate=sample_rate, # the rate in hertz # See # https://g.co/cloud/speech/docs/best-practices#language_support # for a list of supported languages. language_code='en-US', # a BCP-47 language tag ), audio=cloud_speech.RecognitionAudio( uri=input_uri, ) ), DEADLINE_SECS) # Print the recognition results. print(response.results)
def service(self): service = cloud_speech.beta_create_Speech_stub(self.channel) return service
# 音量が閾値より小さかったら,データを捨てループを抜ける ---- if decibel < DECIBEL_THRESHOLD: frames_startbuf.append(frames[0:i + 1]) del frames[0:i + 1] break # 全フレームの音量が閾値を超えていたら,録音開始!! ---- # 更に,framesの先頭に,先頭バッファをプラス # これをしないと「かっぱ」の「かっ」など,促音の前の音が消えてしまう if i == START_FRAME_LEN - 1: flag_RecordStart = True frames = frames_startbuf + frames # googleサーバに接続 ############################ print "\nconnecting ...." with cloud_speech.beta_create_Speech_stub( make_channel('speech.googleapis.com', 443)) as service: try: print "success to connect." except: print "connection error." # 録音開始後の処理 ############################### listen_print_loop( service.StreamingRecognize(request_stream(), DEADLINE_SECS)) # 音声認識 繰り返しの終了判定 ##################### if re.match(EXIT_WORD, recog_result): print('Exiting..') break # 音声認識繰り返ししない設定 ######################