Google语音APIRecognitionConfig没有"speechContexts"字段错误

Google Speech API RecognitionConfig has no "speechContexts" field error

我正在使用最新的 Google-Cloud-Speech API (0.36.0)。我能够成功执行我的脚本,但是,当我添加 speechContexts 参数时,我一直得到 "ValueError: Protocol message RecognitionConfig has no "speechContexts" 字段。"错误。

我已经按照 Google 文档页面上的示例进行操作,但到目前为止我没有取得任何进展。

源代码:

config = types.RecognitionConfig(
            encoding = enums.RecognitionConfig.AudioEncoding.LINEAR16,
            sample_rate_hertz = 22050,
            language_code = 'en-US',
            speechContexts = [{'phrases':['installer']}]
            )

输出

Traceback (most recent call last):
  File "<stdin>", line 5, in <module>
ValueError: Protocol message RecognitionConfig has no "speechContexts" field.

问题是您是字段 speechContexts,而根据 the documentation for the RecognitionConfig class,此字段的正确名称是 speech_contexts

您只需将上面的代码更改为:

config = types.RecognitionConfig(
            encoding = enums.RecognitionConfig.AudioEncoding.LINEAR16,
            sample_rate_hertz = 22050,
            language_code = 'en-US',
            speech_contexts = [{'phrases':['installer']}] #Note the change in the field
            )

您可以参考 Python Reference for the Cloud Speech API 以获得客户端库的完整文档和使用示例。