這個頁面說明如何取得 Cloud Speech-to-Text 所轉錄的音訊的時間偏移值。
Cloud STT 可將時間偏移 (時間戳記) 值加入辨識要求的回應文字中。時間偏移值會顯示在提供的音訊中辨識出的每個所說字詞的開始與結束。時間偏移值代表從音訊開始起算經過的時間量,以 100 毫秒為遞增量。
時間偏移在分析較長的音訊檔案時特別實用,因為在此情況下,您可能需要在經過辨識的文字中搜尋特定字詞,並在原始音訊中找出該字詞 (搜尋)。Cloud STT 針對下列所有語音辨識方法提供時間偏移的支援:speech:recognize、speech:longrunningrecognize 和串流。
系統僅會在辨識回應中提供的第一個替代轉錄加上時間偏移值。
如要在要求結果中納入時間偏移,請在要求設定中將 enableWordTimeOffsets 參數設為 true。
通訊協定
如需完整資訊,請參閱 speech:longrunningrecognize API 端點。
如要執行同步語音辨識,請提出 POST 要求並提供適當的要求內容。以下為使用 curl 的 POST 要求範例,這個範例使用 Google Cloud CLI 產生存取權杖。如需 gcloud CLI 的安裝操作說明,請參閱快速入門導覽課程。
curl -X POST \ -H "Authorization: Bearer "$(gcloud auth application-default print-access-token) \ -H "Content-Type: application/json; charset=utf-8" \ --data "{ 'config': { 'language_code': 'en-US', 'enableWordTimeOffsets': true }, 'audio':{ 'uri':'gs://gcs-test-data/vr.flac' } }" "https://speech.googleapis.com/v1/speech:longrunningrecognize"
如要進一步瞭解如何設定要求內容,請參閱 RecognitionConfig 和 RecognitionAudio 參考說明文件。
如果要求成功,伺服器會傳回 200 OK HTTP 狀態碼與 JSON 格式的回應。如果作業尚未完成 (仍在處理中),
回應會類似以下內容:
{ "name": "2885768779530032514", "metadata": { "@type": "type.googleapis.com/google.cloud.speech.v1.LongRunningRecognizeMetadata", "progressPercent": 97, "startTime": "2020-12-14T03:11:54.492593Z", "lastUpdateTime": "2020-12-14T03:15:57.484509Z", "uri": "gs://{BUCKET_NAME}/{FILE_NAME}" } }
程序完成後,輸出內容會類似於下列內容:
{
"name": "7612202767953098924"
}其中 name 是為要求建立的長時間執行作業名稱。
處理 vr.flac 檔案大約需要 30 秒。如要擷取作業的結果,請向 https://speech.googleapis.com/v1/operations/ 端點發出 GET 要求。將 your-operation-name 替換為從 longrunningrecognize 要求收到的 name。
curl -H "Authorization: Bearer "$(gcloud auth application-default print-access-token) \ -H "Content-Type: application/json; charset=utf-8" \ "https://speech.googleapis.com/v1/operations/your-operation-name"
如果要求成功,伺服器會傳回 200 OK HTTP 狀態碼與 JSON 格式的回應:
{ "name": "7612202767953098924", "metadata": { "@type": "type.googleapis.com/google.cloud.speech.v1.LongRunningRecognizeMetadata", "progressPercent": 100, "startTime": "2017-07-20T16:36:55.033650Z", "lastUpdateTime": "2017-07-20T16:37:17.158630Z" }, "done": true, "response": { "@type": "type.googleapis.com/google.cloud.speech.v1.LongRunningRecognizeResponse", "results": [ { "alternatives": [ { "transcript": "okay so what am I doing here...(etc)...", "confidence": 0.96596134, "words": [ { "startTime": "1.400s", "endTime": "1.800s", "word": "okay" }, { "startTime": "1.800s", "endTime": "2.300s", "word": "so" }, { "startTime": "2.300s", "endTime": "2.400s", "word": "what" }, { "startTime": "2.400s", "endTime": "2.600s", "word": "am" }, { "startTime": "2.600s", "endTime": "2.600s", "word": "I" }, { "startTime": "2.600s", "endTime": "2.700s", "word": "doing" }, { "startTime": "2.700s", "endTime": "3s", "word": "here" }, { "startTime": "3s", "endTime": "3.300s", "word": "why" }, { "startTime": "3.300s", "endTime": "3.400s", "word": "am" }, { "startTime": "3.400s", "endTime": "3.500s", "word": "I" }, { "startTime": "3.500s", "endTime": "3.500s", "word": "here" }, ... ] } ] }, { "alternatives": [ { "transcript": "so so what am I doing here...(etc)...", "confidence": 0.9642093, } ] } ] } }
如果作業尚未完成,您可以重複發出 GET 要求來輪詢端點,直到回應的 done 屬性為 true 為止。
gcloud
如要瞭解完整的詳細資訊,請參閱
recognize-long-running 指令。
如要執行非同步語音辨識,請使用 Google Cloud CLI,提供本機檔案路徑或 Google Cloud Storage 網址。加上 --include-word-time-offsets 旗標。
gcloud ml speech recognize-long-running \ 'gs://cloud-samples-tests/speech/brooklyn.flac' \ --language-code='en-US' --include-word-time-offsets --async
如果要求成功,伺服器會傳回 JSON 格式長時間執行作業的 ID。
{
"name": OPERATION_ID
}然後您可以執行下列指令,取得關於作業的資訊。
gcloud ml speech operations describe OPERATION_ID
您也可以執行下列指令來對作業進行輪詢,直到作業完成為止。
gcloud ml speech operations wait OPERATION_ID
作業完成後,作業會以 JSON 格式傳回音訊的轉錄稿。
{ "@type": "type.googleapis.com/google.cloud.speech.v1.LongRunningRecognizeResponse", "results": [ { "alternatives": [ { "confidence": 0.9840146, "transcript": "how old is the Brooklyn Bridge", "words": [ { "endTime": "0.300s", "startTime": "0s", "word": "how" }, { "endTime": "0.600s", "startTime": "0.300s", "word": "old" }, { "endTime": "0.800s", "startTime": "0.600s", "word": "is" }, { "endTime": "0.900s", "startTime": "0.800s", "word": "the" }, { "endTime": "1.100s", "startTime": "0.900s", "word": "Brooklyn" }, { "endTime": "1.500s", "startTime": "1.100s", "word": "Bridge" } ] } ] } ] }
Go
如要瞭解如何安裝及使用 Cloud STT 的用戶端程式庫,請參閱「Cloud STT 用戶端程式庫」。詳情請參閱「Cloud STT Go API 參考文件」。
如要向 Cloud STT 進行驗證,請設定應用程式預設憑證。詳情請參閱「為本機開發環境設定驗證機制」。
Java
如要瞭解如何安裝及使用 Cloud STT 的用戶端程式庫,請參閱「Cloud STT 用戶端程式庫」。詳情請參閱「Cloud STT Java API 參考文件」。
如要向 Cloud STT 進行驗證,請設定應用程式預設憑證。詳情請參閱「為本機開發環境設定驗證機制」。
Node.js
如要瞭解如何安裝及使用 Cloud STT 的用戶端程式庫,請參閱「Cloud STT 用戶端程式庫」。詳情請參閱「Cloud STT Node.js API 參考文件」。
如要向 Cloud STT 進行驗證,請設定應用程式預設憑證。詳情請參閱「為本機開發環境設定驗證機制」。
Python
如要瞭解如何安裝及使用 Cloud STT 的用戶端程式庫,請參閱「Cloud STT 用戶端程式庫」。詳情請參閱「Cloud STT Python API 參考文件」。
如要向 Cloud STT 進行驗證,請設定應用程式預設憑證。詳情請參閱「為本機開發環境設定驗證機制」。
其他語言
C#:請按照用戶端程式庫頁面上的 C# 設定操作說明完成相關步驟,然後參閱「.NET 適用的 Cloud STT 參考文件」。
PHP:請按照用戶端程式庫頁面上的 PHP 設定操作說明完成相關步驟,然後參閱「PHP 適用的 Cloud STT 參考文件」。
Ruby:請按照用戶端程式庫頁面上的 Ruby 設定操作說明完成相關步驟,然後參閱「Ruby 適用的 Cloud STT 參考文件」。