使用指令列將語音轉錄為文字

此頁面說明如何使用 REST 介面curl 指令,將語音辨識要求傳送至語音轉文字。

Speech-to-Text 可讓您將 Google 語音辨識技術輕鬆整合至開發人員應用程式。您可將音訊資料傳送至 Speech-to-Text API,然後 API 會傳回該音訊檔案的文字轉錄結果。如要進一步瞭解這項服務,請參閱語音轉文字基本概念

事前準備

  1. Sign in to your Google Cloud account. If you're new to Google Cloud, create an account to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.
  2. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

  3. Make sure that billing is enabled for your Google Cloud project.

  4. Enable the Speech-to-Text APIs.

    Enable the APIs

  5. Make sure that you have the following role or roles on the project: Cloud Speech Administrator

    Check for the roles

    1. In the Google Cloud console, go to the IAM page.

      Go to IAM
    2. Select the project.
    3. In the Principal column, find all rows that identify you or a group that you're included in. To learn which groups you're included in, contact your administrator.

    4. For all rows that specify or include you, check the Role column to see whether the list of roles includes the required roles.

    Grant the roles

    1. In the Google Cloud console, go to the IAM page.

      前往「IAM」頁面
    2. 選取專案。
    3. 按一下「授予存取權」
    4. 在「New principals」(新增主體) 欄位中,輸入您的使用者 ID。 這通常是 Google 帳戶的電子郵件地址。

    5. 在「Select a role」(選取角色) 清單中,選取角色。
    6. 如要授予其他角色,請按一下 「新增其他角色」,然後新增每個其他角色。
    7. 按一下 [Save]
  6. Install the Google Cloud CLI.

  7. If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.

  8. To initialize the gcloud CLI, run the following command:

    gcloud init
  9. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

  10. Make sure that billing is enabled for your Google Cloud project.

  11. Enable the Speech-to-Text APIs.

    Enable the APIs

  12. Make sure that you have the following role or roles on the project: Cloud Speech Administrator

    Check for the roles

    1. In the Google Cloud console, go to the IAM page.

      Go to IAM
    2. Select the project.
    3. In the Principal column, find all rows that identify you or a group that you're included in. To learn which groups you're included in, contact your administrator.

    4. For all rows that specify or include you, check the Role column to see whether the list of roles includes the required roles.

    Grant the roles

    1. In the Google Cloud console, go to the IAM page.

      前往「IAM」頁面
    2. 選取專案。
    3. 按一下「授予存取權」
    4. 在「New principals」(新增主體) 欄位中,輸入您的使用者 ID。 這通常是 Google 帳戶的電子郵件地址。

    5. 在「Select a role」(選取角色) 清單中,選取角色。
    6. 如要授予其他角色,請按一下 「新增其他角色」,然後新增每個其他角色。
    7. 按一下 [Save]
  13. Install the Google Cloud CLI.

  14. If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.

  15. To initialize the gcloud CLI, run the following command:

    gcloud init
  16. 提出音訊轉錄要求

    請使用下列程式碼範例,將 recognize REST 要求傳送至 Speech-to-Text API。

    1. 執行這項指令,建立做為要求輸入內容的 JSON 檔案。將 /full/path/to/audio/file.wav 替換為要轉錄的音訊檔案路徑:

      echo "{
        \"config\": {
          \"auto_decoding_config\": {},
          \"language_codes\": [\"en-US\"],
          \"model\": \"long\"
        },
        \"content\": \"$(base64 -w 0 /full/path/to/audio/file.wav | sed 's/+/-/g; s/\//_/g')\"
      }" > /tmp/data.txt
    2. 使用 curl 發出 recognize 要求:

      curl -X POST -H "Content-Type: application/json; charset=utf-8" \
          -H "Authorization: Bearer $(gcloud auth print-access-token)" \
          -d @/tmp/data.txt \
          https://speech.googleapis.com/v2/projects/PROJECT_ID/locations/global/recognizers/_:recognize

      畫面會顯示類似以下的回應:

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

    您已將第一個要求傳送至 Speech-to-Text。

    清除所用資源

    如要避免系統向您的 Google Cloud 帳戶收取本頁所用資源的費用,請按照下列步驟操作。

    1. Optional: Revoke the authentication credentials that you created, and delete the local credential file.

      gcloud auth application-default revoke
    2. Optional: Revoke credentials from the gcloud CLI.

      gcloud auth revoke

    控制台

  17. In the Google Cloud console, go to the Manage resources page.

    Go to Manage resources

  18. In the project list, select the project that you want to delete, and then click Delete.
  19. In the dialog, type the project ID, and then click Shut down to delete the project.
  20. gcloud

    Delete a Google Cloud project:

    gcloud projects delete PROJECT_ID

    後續步驟