python - Google Cloud - 收到无效的 JSON 负载。未知名称 "encoding"at 'config' : Proto field is not repeating, 无法启动列表

标签 python google-cloud-platform google-cloud-speech

当我尝试使用以下配置调用 Google Cloud Speech to Text Api 进行长时间运行的识别时:

config = dict(
            languageCode='de',
            maxAlternatives=1,
            enableWordTimeOffsets=True,
            enableAutomaticPunctuation=True,
            model='default',
            encoding='ENCODING_UNSPECIFIED'
          )

我收到这个错误

Invalid JSON payload received. Unknown name "encoding" at 'config': Proto field is not repeating, cannot start list

如何解决?

最佳答案

能否请您提供更多信息...例如您在项目的这一部分使用的是哪种语言和库版本?

假设您使用的是 Python,您可以在此处找到另一种连接到 Google Cloud Speech to Text Api 的官方方法:https://cloud.google.com/speech-to-text/docs/basics

我习惯的做法是使用 googleapiclient phyton 包和 JSON 数据结构而不是字典数据。

import base64
import googleapiclient.discovery

with open(speech_file, 'rb') as speech:
    # Base64 encode the binary audio file for inclusion in the JSON
    # request.
    speech_content = base64.b64encode(speech.read())

# Construct the request
service = googleapiclient.discovery.build('speech', 'v1')
service_request = service.speech().recognize(
    body={
        "config": {
            "encoding": "LINEAR16",  # raw 16-bit signed LE samples
            "sampleRateHertz": 16000,  # 16 khz
            "languageCode": "en-US",  # a BCP-47 language tag
        },
        "audio": {
            "content": speech_content
            }
        })

不懂安装python包的可以引用这篇官方文章:https://packaging.python.org/tutorials/installing-packages/#id13

LongRunning请求请引用: https://cloud.google.com/speech-to-text/docs/reference/rest/v1/speech/longrunningrecognize

本例中的配置 JSON 结构为:

{
  "config": {
    object(RecognitionConfig)
  },
  "audio": {
    object(RecognitionAudio)
  }
}

其中 RecognitionConfig 是一种 JSON 对象:

{
  "encoding": enum(AudioEncoding),
  "sampleRateHertz": number,
  "languageCode": string,
  "maxAlternatives": number,
  "profanityFilter": boolean,
  "speechContexts": [
    {
      object(SpeechContext)
    }
  ],
  "enableWordTimeOffsets": boolean
}

RecognitionAudio 是这样的:

{
  // Union field audio_source can be only one of the following:
  "content": string,
  "uri": string
  // End of list of possible types for union field audio_source.
}

关于LongRunning的识别,您也可以引用这个链接: https://developers.google.com/resources/api-libraries/documentation/speech/v1/java/latest/com/google/api/services/speech/v1/Speech.SpeechOperations.html

它展示了如何使用 Phyton 包 googleapiclient.discovery 来处理长时间运行的请求,只需在您的 Phyton 类中使用以下方法即可:

...
service_request = service.speech().longrunningrecognize(
        body= {
            "config": {
                "encoding": "FLAC",
                "languageCode": "en-US",
                "enableWordTimeOffsets": True
            },
            "audio": {
                "uri": str('gs://speech-clips/'+self.audio_fqid)
            }
        }
    )
...

关于python - Google Cloud - 收到无效的 JSON 负载。未知名称 "encoding"at 'config' : Proto field is not repeating, 无法启动列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54654238/

相关文章:

python - TensorFlow 2 中可以用什么来替代 tf.train.GradientDescentOptimizer

python - 检查 pandas df.iterrows() 中的最后一行是否

python - 如何检查列表中的单词然后返回计数?

python - 谷歌云语音到文本语法将结果缩小到数字?

python - Tensorflow,预测值概率 (ROI)

ios - 将数据从 iOS 推送到 Google Cloud BigQuery

python - 如何避免 gcloud compute 警报将 key 存储在缓存中

google-cloud-platform - 错误 4003 : can't ssh login into the instance that I created in google cloud platform

google-chrome - Google Chrome浏览器可以在不更改媒体类型或编码的情况下为Google Cloud语音转文本录制音频文件吗?

google-cloud-platform - Google Speech to text 可以离线使用吗?