Exemplo n.º 1
0
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 grpc.RpcError as e:
            code = e.code()
            # CANCELLED is caused by the interrupt handler, which is expected.
            if code is not code.CANCELLED:
                raise
Exemplo n.º 2
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.º 3
0
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))
Exemplo n.º 4
0
def main():
    service = cloud_speech_pb2.SpeechStub(
        make_channel('speech.googleapis.com'))

    # 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 buff:
        # Second, a thread that sends requests with that data
        overlap_buffer = collections.deque(maxlen=int(SECS_OVERLAP * RATE /
                                                      CHUNK))
        requests = request_stream(_audio_data_generator(buff, overlap_buffer),
                                  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:
            while True:
                testUtterance = listen_print_loop(recognize_stream,
                                                  WRAP_IT_UP_SECS, buff)
                if testUtterance != "":
                    recognize_stream.cancel()
                    return testUtterance
                    break

                # 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()

                logging.debug('Starting new stream')
                requests = request_stream(
                    _audio_data_generator(buff, overlap_buffer), RATE)
                recognize_stream = service.StreamingRecognize(
                    requests, DEADLINE_SECS)

        except grpc.RpcError:
            # This happens because of the interrupt handler
            pass
Exemplo n.º 5
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