def listen(self): service = cloud_speech_pb2.SpeechStub(self.make_channel('speech.googleapis.com', 443)) # For streaming audio from the microphone, there are three threads. # First, a thread that collects audio data as it comes in with self.record_audio(self.RATE, self.CHUNK) as buff: # Second, a thread that sends requests with that data overlap_buffer = collections.deque(maxlen=self.SECS_OVERLAP * self.RATE / self.CHUNK) requests = self.request_stream(self._audio_data_generator(buff, overlap_buffer), self.RATE) # Third, a thread that listens for transcription responses recognize_stream = service.StreamingRecognize( requests, self.DEADLINE_SECS) # Exit things cleanly on interrupt signal.signal(signal.SIGINT, lambda *_: recognize_stream.cancel()) # Now, put the transcription responses to use. try: while True: self.listen_print_loop(recognize_stream, buff) # Discard this stream and create a new one. # Note: calling .cancel() doesn't immediately raise an RpcError # - it only raises when the iterator's next() is requested recognize_stream.cancel() requests = self.request_stream(self._audio_data_generator( buff, overlap_buffer), self.RATE) # Third, a thread that listens for transcription responses recognize_stream = service.StreamingRecognize( requests, self.DEADLINE_SECS) except grpc.RpcError: # This happens because of the interrupt handler pass
def main(input_uri, encoding, sample_rate, language_code='en-US'): service = cloud_speech_pb2.SpeechStub( 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_pb2.SyncRecognizeRequest( 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 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(): service = cloud_speech_pb2.SpeechStub( make_channel('speech.googleapis.com', 443)) print("Speech2Text started") # 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 grpc.RpcError as e: code = e.code() # CANCELLED is caused by the interrupt handler, which is expected. if code is not code.CANCELLED: raise
def handle_request_sound_transcript(req): requestSoundTranscriptResponse = RequestSoundTranscriptResponse() try: resp = getAudioText() requestSoundTranscriptResponse.utterance = resp.responses[0] requestSoundTranscriptResponse.isGood = resp.isGood except RuntimeError: global googleSpeech googleSpeech = cloud_speech_pb2.SpeechStub( make_channel('speech.googleapis.com', 443)) requestSoundTranscriptResponse.isGood = False return requestSoundTranscriptResponse
def main(input_uri, encoding, sample_rate, language_code='en-US'): channel = make_channel('speech.googleapis.com', 443) service = cloud_speech_pb2.SpeechStub(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_pb2.OperationsStub(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_pb2.GetOperationRequest(name=name), DEADLINE_SECS) if operation.error.message: print('\nOperation error:\n{}'.format(operation.error)) 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 sound_transcript_server(): rospy.init_node(NAME) while (True): try: global googleSpeech googleSpeech = cloud_speech_pb2.SpeechStub( make_channel('speech.googleapis.com', 443)) serv = rospy.Service('sound_transcript_server', RequestSoundTranscript, handle_request_sound_transcript) rospy.spin() break except RuntimeError: pass
def main(): service = cloud_speech_pb2.SpeechStub( make_channel('speech.googleapis.com', 443)) # 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 _make_service(self, channel): return cloud_speech.SpeechStub(channel)