简体   繁体   English

谷歌语音 API Python 无响应

[英]Google Speech API Python not responding

I'm trying to implement google's speech api but every time I try to run the program, the terminal goes unresponsive.我正在尝试实施谷歌的语音 api 但每次我尝试运行该程序时,终端都没有响应。 It seems that the program runs until the line "response = client.recognize(config, audio)" and just gets stuck at that point.程序似乎一直运行到“response = client.recognize(config, audio)”这一行,然后就卡在了这一点上。 Here's a picture of my code, I pulled most of it straight from google's cloud platform documentation.这是我的代码图片,我直接从谷歌的云平台文档中提取了大部分代码。

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)

I had the same problem when building my app with PyInstaller.使用 PyInstaller 构建我的应用程序时,我遇到了同样的问题。 Make sure you have the certifi folder with the file cacert.pem in it.确保您有certifi文件夹,其中包含文件cacert.pem Those should come with the library when you run pip install google-cloud-speech .当您运行pip install google-cloud-speech时,这些应该随库一起提供。 If not reinstall it.如果没有重新安装它。

I was facing the same issue, tried changing我遇到了同样的问题,尝试改变

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

to

os.environ["GOOGLE_APPLICATION_CREDENTIALS"]= r"full/path/api-key.json"

on window .window上。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM