コード例 #1
0
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()
コード例 #2
0
ファイル: translate_from_mic.py プロジェクト: othercamb/GCP
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)
コード例 #3
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()
コード例 #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
コード例 #5
0
    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)

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