Esempio n. 1
0
def do_translation_loop():
    print("Begin speaking...")

    client = media.SpeechTranslationServiceClient()

    speech_config = media.TranslateSpeechConfig(
        audio_encoding="linear16",
        source_language_code="en-US",
        target_language_code="es-ES",
    )

    config = media.StreamingTranslateSpeechConfig(audio_config=speech_config,
                                                  single_utterance=True)

    # The first request contains the configuration.
    # Note that audio_content is explicitly set to None.
    first_request = media.StreamingTranslateSpeechRequest(
        streaming_config=config)

    with MicrophoneStream(RATE, CHUNK) as stream:
        audio_generator = stream.generator()
        mic_requests = (media.StreamingTranslateSpeechRequest(
            audio_content=content) for content in audio_generator)

        requests = itertools.chain(iter([first_request]), mic_requests)

        responses = client.streaming_translate_speech(requests)

        # Print the translation responses as they arrive
        result = listen_print_loop(responses)
        if result == 0:
            stream.exit()
def do_translation_loop(configs, token, seq_count, session):

    client = media.SpeechTranslationServiceClient()
    speech_config = media.TranslateSpeechConfig(
        audio_encoding='linear16',
        source_language_code=configs["source_lang"],
        target_language_code=configs["target_lang"])

    config = media.StreamingTranslateSpeechConfig(audio_config=speech_config,
                                                  single_utterance=True)

    # The first request contains the configuration.
    # Note that audio_content is explicitly set to None.
    first_request = media.StreamingTranslateSpeechRequest(
        streaming_config=config, audio_content=None)

    with MicrophoneStream(RATE, CHUNK, configs, token, seq_count,
                          session) as stream:

        audio_generator = stream.generator()
        mic_requests = (media.StreamingTranslateSpeechRequest(
            audio_content=content, streaming_config=config)
                        for content in audio_generator)

        requests = itertools.chain(iter([first_request]), mic_requests)

        responses = client.streaming_translate_speech(requests)

        # Print the translation responses as they arrive
        result = listen_print_loop(responses, stream)

        if result == 0:
            stream.exit()
Esempio n. 3
0
def main():
    client = mediatranslation.SpeechTranslationServiceClient()

    speech_config = mediatranslation.TranslateSpeechConfig(
        audio_encoding='linear16',
        source_language_code='en-US',
        target_language_code='es-ES')

    config = mediatranslation.StreamingTranslateSpeechConfig(
        audio_config=speech_config)

    # The first request contains the configuration.
    # Note that audio_content is explicitly set to None.
    first_request = mediatranslation.StreamingTranslateSpeechRequest(
        streaming_config=config, audio_content=None)

    with MicrophoneStream(RATE, CHUNK) as stream:
        audio_generator = stream.generator()
        mic_requests = (mediatranslation.StreamingTranslateSpeechRequest(
            audio_content=content, streaming_config=config)
                        for content in audio_generator)

        requests = itertools.chain(iter([first_request]), mic_requests)

        responses = client.streaming_translate_speech(requests)

        # Print the translation responses as they arrive
        listen_print_loop(responses)
Esempio n. 4
0
def do_translation_loop(args, projector, font, fontsize, log, interim):
    dev_index = args.dev
    lang = args.lang

    for event in pygame.event.get():
        if event.type == pygame.KEYDOWN and event.key == pygame.K_q:
            log.write("-- CLEAN EXIT ---\n")
            log.close()
            exit(0)
        elif event.type == pygame.KEYDOWN and event.key == pygame.K_c:
            log.write("c pressed - clear screen\n")
            log.flush()
            projector.clear()
        elif event.type == pygame.KEYDOWN and event.key == pygame.K_i:
            interim = not interim
            log.write("i pressed. Interim results: {}\n".format(interim))
            log.flush()
        else:
            continue

    client = media.SpeechTranslationServiceClient()

    speech_config = media.TranslateSpeechConfig(audio_encoding='linear16',
                                                source_language_code='en-US',
                                                target_language_code=lang)

    config = media.StreamingTranslateSpeechConfig(audio_config=speech_config,
                                                  single_utterance=True)

    # The first request contains the configuration.
    # Note that audio_content is explicitly set to None.
    first_request = media.StreamingTranslateSpeechRequest(
        streaming_config=config, audio_content=None)

    with MicrophoneStream(RATE, CHUNK, dev_index) as stream:
        audio_generator = stream.generator()
        mic_requests = (media.StreamingTranslateSpeechRequest(
            audio_content=content, streaming_config=config)
                        for content in audio_generator)

        requests = itertools.chain(iter([first_request]), mic_requests)

        responses = client.streaming_translate_speech(requests)

        # Print the translation responses as they arrive
        result = listen_print_loop(responses, args, projector, font, fontsize,
                                   log, interim)
        if result == 0:
            stream.exit()
    return interim
Esempio n. 5
0
def translate_from_file(file_path='path/to/your/file'):

    client = mediatranslation.SpeechTranslationServiceClient()

    # The `sample_rate_hertz` field is not required for FLAC and WAV (Linear16)
    # encoded data. Other audio encodings must provide the sampling rate.
    audio_config = mediatranslation.TranslateSpeechConfig(
        audio_encoding='linear16',
        source_language_code='en-US',
        target_language_code='fr-FR')

    streaming_config = mediatranslation.StreamingTranslateSpeechConfig(
        audio_config=audio_config)

    def request_generator(config, audio_file_path):

        # The first request contains the configuration.
        # Note that audio_content is explicitly set to None.
        yield mediatranslation.StreamingTranslateSpeechRequest(
            streaming_config=config, audio_content=None)

        with open(audio_file_path, 'rb') as audio:
            while True:
                chunk = audio.read(4096)
                if not chunk:
                    break
                yield mediatranslation.StreamingTranslateSpeechRequest(
                    audio_content=chunk,
                    streaming_config=config)

    requests = request_generator(streaming_config, file_path)
    responses = client.streaming_translate_speech(requests)

    for response in responses:
        # Once the transcription settles, the response contains the
        # is_final result. The other results will be for subsequent portions of
        # the audio.
        result = response.result
        translation = result.text_translation_result.translation
        source = result.recognition_result

        if result.text_translation_result.is_final:
            print(u'\nFinal translation: {0}'.format(translation))
            print(u'Final recognition result: {0}'.format(source))
            break

        print(u'\nPartial translation: {0}'.format(translation))
        print(u'Partial recognition result: {0}'.format(source))