def _process(self, input_file, language_code): if not input_file.endswith('.flac'): raise RuntimeError('Only flac encoding file is supported.') audio_content = cloud_speech.RecognitionAudio(content=open( input_file, 'rb').read(), ) sample_rate = self._sample_rate(input_file) operation = self.service.AsyncRecognize( cloud_speech.AsyncRecognizeRequest( config=cloud_speech.RecognitionConfig( encoding=AUDIO_ENCODING, sample_rate=sample_rate, language_code=language_code, ), audio=audio_content), DEADLINE_SECS) return operation
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, 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( 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)