使用 Recognizer 類別
現在你已經建立了 Recognizer 類別的實例,接下來要在它上面呼叫 recognize_google() 方法,以存取 Google 的網頁語音 API,將語音轉成文字。
recognize_google() 需要一個 audio_data 引數,否則會回傳錯誤。
預設語言是美式英語(US English)。如果你的音訊檔不是美式英語,可以用 language 引數來更改語言。語言代碼清單可在此查看:here。
一個包含英語語音的音訊檔已匯入為 clean_support_call_audio。你可以在這裡收聽該音訊檔:listen to the audio file here。SpeechRecognition 也已以 sr 匯入。
為了避免觸發 Google 網頁 API 的請求上限,我們已模擬(mock)Recognizer 類別以搭配本課的音訊檔使用。這表示有些功能會受限。
本練習屬於課程
Python 的口語語言處理
練習說明
- 在
recognizer上呼叫recognize_google()方法,並傳入clean_support_call_audio。 - 將 language 參數設為
"en-US"。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# Create a recognizer class
recognizer = sr.Recognizer()
# Transcribe the support call audio
text = ____.____(
audio_data=____,
language=____)
print(text)