IBM Cloud Docs
開始使用 Speech to Text

開始使用 Speech to Text

IBM Watson® Speech to Text 服務會將音訊轉錄為文字,以便啟用應用程式的語音轉錄功能。 此 curl-based 教學可協助您快速開始使用此服務。 範例會顯示如何呼叫服務的 POST /v1/recognize 方法來要求文字記錄。

指導教學使用 curl 指令行公用程式來示範 REST API 呼叫。 如需 curl 的相關資訊,請參閱 搭配使用 curl 與 Watson 範例

IBM Cloud 觀看下列視訊,以取得開始使用 Speech to Text 服務的視覺化摘要。

開始之前

IBM Cloud

IBM Cloud

  • 建立服務的實例:

    1. 移至 IBM Cloud 型錄中的 Speech to Text 頁面。
    2. 註冊免費的 IBM Cloud 帳戶,或者登入。
    3. 閱讀並同意授權合約的條款。
    4. 按一下建立
  • 複製認證以便向服務實例進行鑑別:

    1. 檢視服務實例的 管理 頁面:

      • 如果您位於服務實例的 入門 頁面上,請按一下主題清單中的 管理 項目。
      • 如果您位於 資源清單 頁面上,請展開 名稱 直欄中的 AI/ Machine Learning 分組,然後按一下服務實例的名稱。
    2. 在「管理」頁面上,按一下 認證 方框中的 顯示認證

    3. 複製服務實例的 API KeyURL 值。

本指導教學使用 API 金鑰來進行鑑別。 在正式作業中,使用 IAM 記號。 如需詳細資訊,請參閱 驗證 IBM Cloud

IBM Cloud Pak for Data

IBM Cloud Pak for Data

在開始本教學之前,必須先安裝和設定 Speech to Text 服務。 如需詳細資訊,請參閱 Watson Cloud Pak for Data 上的語音服務

  1. 使用 Web 用戶端、API 或指令行介面來建立服務實例。 有關在 IBM Cloud Pak for Data 上建立服務實體的詳細資訊,請參閱 為 Watson Speech 服務建立服務實體
  2. 遵循 建立 Watson 語音服務實例 中的指示,以取得實例的 Bearer 記號。 本指導教學使用 Bearer 記號向服務進行鑑別。

轉錄音訊且不使用任何選項

呼叫 POST /v1/recognize 方法,以要求 FLAC 音訊檔的基本文字記錄,且不使用其他要求參數。

  1. 下載音訊檔範例 audio-file.flac

  2. 發出下列指令,以呼叫服務的 /v1/recognize 方法進行基本轉錄,且不使用任何參數。 範例會使用 Content-Type 標頭來指出音訊的類型:audio/flac。 此範例使用預設語言模型 en-US_BroadbandModel 進行轉錄。

    IBM Cloud

    • {apikey}{url} 取代為您的 API 金鑰及 URL。
    • 修改 {path_to_file} 以指定 audio-file.flac 檔的位置。
    curl -X POST -u "apikey:{apikey}" \
    --header "Content-Type: audio/flac" \
    --data-binary @{path_to_file}audio-file.flac \
    "{url}/v1/recognize"
    

    IBM Cloud Pak for Data IBM Software Hub

    • {token}{url} 改為您服務實例的存取權標和 URL。
    • 修改 {path_to_file} 以指定 audio-file.flac 檔的位置。
    curl -X POST \
    --header "Authorization: Bearer {token}" \
    --header "Content-Type: audio/flac" \
    --data-binary @{path_to_file}audio-file.flac \
    "{url}/v1/recognize"
    

服務會傳回下列轉錄結果:

{
  "result_index": 0,
  "results": [
    {
      "alternatives": [
        {
          "confidence": 0.96
          "transcript": "several tornadoes touch down as a line of severe thunderstorms swept through Colorado on Sunday "
        }
      ],
      "final": true
    }
  ]
}

使用選項轉錄音訊

呼叫 POST /v1/recognize 方法來轉錄相同的 FLAC 音訊檔,但指定兩個轉錄參數。

  1. 如有必要,請下載示例音訊檔案 audio-file.flac.

  2. 發出下列指令,以呼叫服務的 /v1/recognize 方法,並使用兩個額外的參數。 將 timestamps 參數設定為 true,以指出音訊串流中每個字組的開頭和結尾。 將 max_alternatives 參數設定為 3,以接收轉錄的三個最可能替代項目。 範例使用 Content-Type 標頭來指出音訊的類型 audio/flac,而要求使用預設模型 en-US_BroadbandModel

    IBM Cloud

    • {apikey}{url} 取代為您的 API 金鑰及 URL。
    • 修改 {path_to_file} 以指定 audio-file.flac 檔的位置。
    curl -X POST -u "apikey:{apikey}" \
    --header "Content-Type: audio/flac" \
    --data-binary @{path_to_file}audio-file.flac \
    "{url}/v1/recognize?timestamps=true&max_alternatives=3"
    

    IBM Cloud Pak for Data IBM Software Hub

    • {token}{url} 改為您服務實例的存取權標和 URL。
    • 修改 {path_to_file} 以指定 audio-file.flac 檔的位置。
    curl -X POST \
    --header "Authorization: Bearer {token}" \
    --header "Content-Type: audio/flac" \
    --data-binary @{path_to_file}audio-file.flac \
    "{url}/v1/recognize?timestamps=true&max_alternatives=3"
    

服務傳回下列結果,其中包括時間戳記和三個替代的轉錄:

{
  "result_index": 0,
  "results": [
    {
      "alternatives": [
        {
          "timestamps": [
            ["several":, 1.0, 1.51],
            ["tornadoes":, 1.51, 2.15],
            ["touch":, 2.15, 2.5],
            . . .
          ]
        },
        {
          "confidence": 0.96
          "transcript": "several tornadoes touch down as a line of severe thunderstorms swept through Colorado on Sunday "
        },
        {
          "transcript": "several tornadoes touched down as a line of severe thunderstorms swept through Colorado on Sunday "
        },
        {
          "transcript": "several tornadoes touch down as a line of severe thunderstorms swept through Colorado and Sunday "
        }
      ],
      "final": true
    }
  ]
}

下一步

  • 若要嘗試從串流音訊輸入或從您上傳的檔案轉錄文字的範例應用程式,請參閱 Speech to Text 展示
  • 如需服務介面及功能的相關資訊,請參閱服務功能
  • 有關服務介面所有方法的詳細資訊,請參閱 API & SDK 參考資料