【问题标题】:Google Cloud Speech-To-Text API response doesn't return wordsGoogle Cloud Speech-To-Text API 响应不返回字词
【发布时间】:2020-10-27 10:33:06
【问题描述】:

我正在尝试使用带有 Python 的 Google Cloud Speech-To-Text API 在我的应用程序中实现 Speech-To-Text。我正确地得到了转录,但是响应只包含转录和信心,而不是单独的单词。如果我尝试访问这些单词,我会得到一个空列表。

为了访问结果,我使用以下代码:

best_alternative = result.alternatives[0]
word = best_alternative
transcript = best_alternative.transcript
confidence = best_alternative.confidence
print(f'Transcript: {transcript}')
print(f'Confidence: {confidence:.0%}')

打印出best_alternative.__dict__ 给了我成绩单和信心,但不是文字。有什么特殊的方法可以访问成绩单中的单词还是我遗漏了什么?

更新: 最初,我是这样初始化识别配置的:

config = speech.RecognitionConfig(
    encoding=speech.RecognitionConfig.AudioEncoding.LINEAR16,
    sample_rate_hertz=RATE,
    language_code=lan_code)
streaming_config = speech.StreamingRecognitionConfig(
        config=config,
        interim_results=True,
        enable_speaker_diarization=True)

使用此配置,返回的响应不包含文字,仅包含文字记录和置信度。然后我将配置更改为:

config = speech.RecognitionConfig()
config.sample_rate_hertz = 16000
config.language_code = 'en-US'
config.encoding = speech.RecognitionConfig.AudioEncoding.LINEAR16
config.enable_speaker_diarization = True

这最终给了我文字以及成绩单和信心。可以使用以下方式访问这些单词:

response.results[0].alternatives[0].words[i].word


    

【问题讨论】:

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


    【解决方案1】:

    根据 Cloud Speech-to-Text API REST documentationspeech.recognize 方法为每个转录结果 results[] 对象返回语音识别响应以及 SpeechRecognitionResult,而 SpeechRecognitionAlternative 检索 transcriptconfidence , words[] 在特定假设内。

    查看 Python Google google-cloud-speech 库实现,我承认对于真正的 SpeechRecognitionAlternative() class,我们可以发现每个已识别单词的特定单词信息列表 WordInfo

    print("Words: {}".format(result.alternatives[0].words[0].word))
    

    【讨论】:

    • 在发布之前,我实际上已经尝试过一段类似的代码,但我收到了“List out of index”错误。正如我在上面已经写过的,打印 result.alternatives.__dict__ 只会打印出成绩单和信心,而不会打印出文字 [ ]。
    • 我找到了一个解决方案,并在原帖中添加了它。非常感谢您的帮助!
    • 感谢您的更新,很高兴在这里为您提供帮助。特别是,results[0] 只会为您提供音频帧的第一个结果,假设您打算遍历我写下答案的整个结果。如果有助于实现预期目标,请考虑 accept/up vote 我的回答。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-16
    相关资源
    最近更新 更多