機器學習

GCP 筆記: Google Speech API 快速入門

Eric Chuang
2021-06-17

Speech API 主要的功能,是將語音轉為文字。

首先,先在主控台中產生 API 金鑰。

打開執行個體,將 API 金鑰複製並輸入至環境變數中,並建立 request.json 檔案,將要求的參數存在該檔案中。

export API_KEY=<YOUR_API_KEY>
vim request.json
{
  "config": {
      "encoding":"FLAC",
      "languageCode": "en-US"
  },
  "audio": {
      "uri":"gs://cloud-samples-tests/speech/brooklyn.flac"
  }
}

送出 POST 要求。

curl -s -X POST -H "Content-Type: application/json" --data-binary @reques
t.json \
> "https://speech.googleapis.com/v1/speech:recognize?key=${API_KEY}"

接著會回傳如下的結果:

{
  "results": [
    {
      "alternatives": [
        {
          "transcript": "how old is the Brooklyn Bridge",
          "confidence": 0.98287845
        }
      ]
    }
  ]
}

也可以調整前一個指令,將檔案存到 result.json 中。

curl -s -X POST -H "Content-Type: application/json" --data-binary @reques
t.json \
> "https://speech.googleapis.com/v1/speech:recognize?key=${API_KEY}" > result.json