https://pypi.org/project/SpeechRecognition/
pip3 install SpeechRecognition
Collecting SpeechRecognition
Downloading https://files.pythonhosted.org/packages/26/e1/7f5678cd94ec1234269d23756dbdaa4c8cfaed973412f88ae8adf7893a50/SpeechRecognition-3.8.1-py2.py3-none-any.whl (32.8MB)
|████████████████████████████████| 32.8MB 6.4MB/s
Installing collected packages: SpeechRecognition
Successfully installed SpeechRecognition-3.8.1
Audio來源建議使用WAV檔案,無法使用mp3:
其支援檔案類型:
WAV: must be in PCM/LPCM format
AIFF
AIFF-C
FLAC: must be native FLAC format; OGG-FLAC is not supported
import speech_recognition as sr
r = sr.Recognizer()
WAV = sr.AudioFile('OSR_us_000_0061_8k.wav')
with WAV as source:
audio = r.record(source)
print(r.recognize_google(audio, show_all=True))
pip3 install pydub
Collecting pydub
Downloading https://files.pythonhosted.org/packages/7b/d1/fbfa79371a8cd9bb15c2e3c480d7e6e340ed5cc55005174e16f48418333a/pydub-0.24.1-py2.py3-none-any.whl
Installing collected packages: pydub
Successfully installed pydub-0.24.1
進入https://ffmpeg.org/download.html#build-windows
進入https://www.gyan.dev/ffmpeg/builds/
下載ffmpeg-4.3.1-2020-10-01-full_build.7z
解壓縮後將"ffmpeg-4.3.1-2020-10-01-full_build"改成"ffmpeg"放在C:\下,並修改環境變數
使用者
系統
重新打開cmd
轉檔mp3->wav
pydubTest.py
from os import path
from pydub import AudioSegment
# convert wav to mp3
sound = AudioSegment.from_mp3("good.mp3")
sound.export("test.wav", format="wav")
辨識
test2.py
import speech_recognition as sr
r = sr.Recognizer()
WAV = sr.AudioFile('test.wav')
with WAV as source:
audio = r.record(source)
print(r.recognize_google(audio, show_all=True))