首页 > 解决方案 > 我可以使用 curl 发出语音:使用来自 Google Cloud Storage 的 .json 识别(Google Cloud Speech-to-text)请求吗?

问题描述

我希望能够发表演讲:使用我自己的云托管资源识别请求,因此我可以简单地登录 Google Cloud Platform 控制台,在 Cloud Shell 中运行命令,然后查看结果。很像https://cloud.google.com/speech-to-text/docs/quickstart-protocol,除了不使用本地任何东西。

不确定要分享哪些其他重要信息,但我的云中的 .json 和 .flac 文件具有公共读取权限。

我怎样才能做到这一点?

我的请求:

curl -H "Content-Type: application/json" https://speech.googleapis.com/v1/speech:recognize?key=[my-api-key] -d @https://storage.googleapis.com/[bucket]/[json-request-filename].json

响应:

Warning: Couldn't read data from file
Warning: "https://storage.googleapis.com/[bucket]/[json-request-filename].json",
Warning: this makes an empty POST.
{
  "error": {
    "code": 400,
    "message": "RecognitionAudio not set.",
    "status": "INVALID_ARGUMENT"
  }
}

这是托管在谷歌云存储中的 .json:

{
  "config": {
      "encoding":"FLAC",
      "sampleRateHertz": 16000,
      "languageCode": "en-US",
      "enableWordTimeOffsets": false
  },
  "audio": {
      "uri":"gs://[bucket]/[audio-filename].flac"
  }
}

没有新信息,但 Google Cloud Platform Shell 的外观如下:

[my-account]@cloudshell:~ ([my-project])$ curl -H "Content-Type: application/json" https://speech.googleapis.com/v1/speech:recognize?key=[my-api-key] -d @https://storage.googleapis.com/[bucket]/[json-request-filename].json
Warning: Couldn't read data from file
Warning: "https://storage.googleapis.com/[bucket]/[json-request-filename].json",
Warning: this makes an empty POST.
{
  "error": {
    "code": 400,
    "message": "RecognitionAudio not set.",
    "status": "INVALID_ARGUMENT"
  }
}

标签: google-cloud-platformgoogle-speech-api

解决方案


curl 命令中的 -d 标志告诉 curl 从紧随其后的文件名中读取数据,并将该数据用作请求的主体。curl 不会将 Web URL 识别为有效文件。curl 无法读取该 JSON 文件,因此它就像一个空文件一样,并构建一个带有空正文的请求。发送到 API 的请求没有关于该 JSON 文件的任何信息。

语音 API 接收到带有空正文的请求,并且无法对其执行任何操作。API 甚至不知道您在 curl 命令中指定了 Google Cloud 对象。

Speech:recognize 方法记录在https://cloud.google.com/speech-to-text/docs/reference/rest/v1p1beta1/speech/recognize中。除了从请求的正文中之外,它没有任何方法可以获取所需的参数。您不能告诉它从其他地方(例如 URL 或 Google Cloud 对象)读取这些参数。您必须将它们包含在请求中,因此构建请求的程序需要知道它们。


推荐阅读