使用 SpeechRecognition 讀取音訊檔案

Python 的口語語言處理

Daniel Bourke

Machine Learning Engineer/YouTube Creator

AudioFile 類別

import speech_recognition as sr

# Setup recognizer instance recognizer = sr.Recognizer()
# Read in audio file clean_support_call = sr.AudioFile("clean-support-call.wav")
# Check type of clean_support_call type(clean_support_call)
<class 'speech_recognition.AudioFile'>
Python 的口語語言處理

從 AudioFile 到 AudioData

recognizer.recognize_google(audio_data=clean_support_call)
AssertionError: ``audio_data`` must be audio data
# Convert from AudioFile to AudioData
with clean_support_call as source:

# Record the audio clean_support_call_audio = recognizer.record(source)
# Check the type type(clean_support_call_audio)
<class 'speech_recognition.AudioData'>
Python 的口語語言處理

轉錄 AudioData

# Transcribe clean support call
recognizer.recognize_google(audio_data=clean_support_call_audio)
hello I'd like to get some help setting up my account please
Python 的口語語言處理

duration 與 offset

  • durationoffset 預設皆為 None
# 使用預設的 duration 與 offset
with clean_support_call as source:
    clean_support_call_audio = recognizer.record(source,
                                                 duration=None,
                                                 offset=None)
# 擷取前 2 秒音訊
with clean_support_call as source:
    clean_support_call_audio = recognizer.record(source,
                                                 duration=2.0)
hello I'd like to get
Python 的口語語言處理

一起來練習吧!

Python 的口語語言處理

Preparing Video For Download...