Exemplo n.º 1
0
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))
Exemplo n.º 2
0
    def sync_recognize(self, config, audio, options=None):
        """
        Perform synchronous speech-recognition: receive results after all audio
        has been sent and processed.

        Example:
          >>> from google.cloud.gapic.speech.v1beta1 import speech_client
          >>> from google.cloud.grpc.speech.v1beta1 import cloud_speech_pb2
          >>> api = speech_client.SpeechClient()
          >>> config = cloud_speech_pb2.RecognitionConfig()
          >>> audio = cloud_speech_pb2.RecognitionAudio()
          >>> response = api.sync_recognize(config, audio)

        Args:
          config (:class:`google.cloud.grpc.speech.v1beta1.cloud_speech_pb2.RecognitionConfig`): [Required] The ``config`` message provides information to the recognizer
            that specifies how to process the request.
          audio (:class:`google.cloud.grpc.speech.v1beta1.cloud_speech_pb2.RecognitionAudio`): [Required] The audio data to be recognized.
          options (:class:`google.gax.CallOptions`): Overrides the default
            settings for this call, e.g, timeout, retries etc.

        Returns:
          A :class:`google.cloud.grpc.speech.v1beta1.cloud_speech_pb2.SyncRecognizeResponse` instance.

        Raises:
          :exc:`google.gax.errors.GaxError` if the RPC is aborted.
          :exc:`ValueError` if the parameters are invalid.
        """
        request = cloud_speech_pb2.SyncRecognizeRequest(
            config=config, audio=audio)
        return self._sync_recognize(request, options)