【问题标题】:Can I use curl to make a speech:recognize (Google Cloud Speech-to-text) request using a .json from Google Cloud Storage?我可以使用 curl 发出语音:使用来自 Google Cloud Storage 的 .json 识别(Google Cloud Speech-to-text)请求吗?
【发布时间】:2019-02-28 17:52:10
【问题描述】:

我希望能够发表演讲:在我自己的云托管资源上识别请求,因此我可以简单地登录 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-platform google-speech-api


    【解决方案1】:

    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 对象)读取这些参数。您必须将它们包含在请求中,因此构建请求的程序需要知道它们。

    【讨论】:

      【解决方案2】:

      您可以使用以下代码找到您的 API 密钥,但您必须使用此链接 https://cloud.google.com/sdk/docs/install#linux 安装 gcloud

      gcloud auth application-default print-access-token
      
      curl -H "Authorization: Bearer YOUR_API_KEY" -H "Content-Type: application/json; charset=utf-8" --data "{
        'input':{
          'text':'I\'ve added the event to your calendar.'
        },
        'voice':{
          'languageCode':'en-gb',
          'name':'en-GB-Standard-A',
          'ssmlGender':'FEMALE'
        },
        'audioConfig':{
          'audioEncoding':'MP3'
        }
      }" "https://texttospeech.googleapis.com/v1/text:synthesize"
      

      【讨论】:

        猜你喜欢
        • 2019-09-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-05-26
        • 2020-03-21
        • 2019-04-16
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多