def test_sync_recognize(self, mock_create_stub):
        # Mock gRPC layer
        grpc_stub = mock.Mock()
        mock_create_stub.return_value = grpc_stub

        client = speech_client.SpeechClient()

        # Mock request
        encoding = enums.RecognitionConfig.AudioEncoding.FLAC
        sample_rate = 44100
        config = cloud_speech_pb2.RecognitionConfig(encoding=encoding,
                                                    sample_rate=sample_rate)
        uri = 'gs://bucket_name/file_name.flac'
        audio = cloud_speech_pb2.RecognitionAudio(uri=uri)

        # Mock response
        expected_response = cloud_speech_pb2.SyncRecognizeResponse()
        grpc_stub.SyncRecognize.return_value = expected_response

        response = client.sync_recognize(config, audio)
        self.assertEqual(expected_response, response)

        grpc_stub.SyncRecognize.assert_called_once()
        args, kwargs = grpc_stub.SyncRecognize.call_args
        self.assertEqual(len(args), 2)
        self.assertEqual(len(kwargs), 1)
        self.assertIn('metadata', kwargs)
        actual_request = args[0]

        expected_request = cloud_speech_pb2.SyncRecognizeRequest(config=config,
                                                                 audio=audio)
        self.assertEqual(expected_request, actual_request)
示例#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.gapic.speech.v1beta1 import enums
          >>> from google.cloud.proto.speech.v1beta1 import cloud_speech_pb2
          >>> api = speech_client.SpeechClient()
          >>> encoding = enums.RecognitionConfig.AudioEncoding.ENCODING_UNSPECIFIED
          >>> sample_rate = 0
          >>> config = cloud_speech_pb2.RecognitionConfig(encoding, sample_rate)
          >>> uri = ''
          >>> audio = cloud_speech_pb2.RecognitionAudio(uri)
          >>> response = api.sync_recognize(config, audio)

        Args:
          config (:class:`google.cloud.proto.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.proto.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.proto.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.
        """
        # Create the request object.
        request = cloud_speech_pb2.SyncRecognizeRequest(config=config,
                                                        audio=audio)
        return self._sync_recognize(request, options)
示例#3
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))
示例#4
0
    def request_transcribe_sync(self, content):
        """
        TODO: Description.
        :param content:
        :return:
        """
        service = cloud_speech_pb2.SpeechStub(
            self.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=self.
                    encoding,  # one of LINEAR16, FLAC, MULAW, AMR, AMR_WB
                    sample_rate=self.sample_rate,  # the rate in hertz
                    # See https://g.co/cloud/speech/docs/languages for a list of
                    # supported languages.
                    language_code=self.language_code,  # a BCP-47 language tag
                ),
                audio=cloud_speech_pb2.RecognitionAudio(content=content, )),
            DEADLINE_SECS)

        # Print the recognition result alternatives and confidence scores.
        alternatives = []
        for result in response.results:
            for alternative in result.alternatives:
                alternatives.append({
                    'service_name': self.config['service_name'],
                    'confidence': alternative.confidence,
                    'transcript': alternative.transcript
                })

        return alternatives