Google Speech API Python没有响应

时间:2019-07-18 10:05:32

标签: python google-cloud-platform speech-to-text google-speech-api

我正在尝试实现Google的语音API,但是每次尝试运行该程序时,终端都将无响应。似乎该程序一直运行到“ response = client.recognize(config,audio)”行,并在此时卡住。这是我的代码,我直接从Google的云平台文档中提取了大部分代码。

def transcribe_file(speech_file):

    import os
    import io
    os.environ["GOOGLE_APPLICATION_CREDENTIALS"]="api-key.json"

    """Transcribe the given audio file."""
    from google.cloud import speech
    from google.cloud.speech import enums
    from google.cloud.speech import types
    import io
    client = speech.SpeechClient()

    with io.open(speech_file, 'rb') as audio_file:
        content = audio_file.read()

    audio = types.RecognitionAudio(content=content)
    config = types.RecognitionConfig(
        encoding='FLAC',
        sample_rate_hertz=16000,
        language_code='en-US')

    print(config)

    response = client.recognize(config, audio)

    # Each result is for a consecutive portion of the audio. Iterate through
    # them to get the transcripts for the entire audio file.
    for result in response.results:
        # The first alternative is the most likely one for this portion.
        print(u'Transcript: {}'.format(result.alternatives[0].transcript))

transcribe_file(audio/file/name.wav)

0 个答案:

没有答案