code:
"""
資料來源:
https://realpython.com/python-speech-recognition/
pip install sounddevice
pip install scipy
pip install pyaudio # 注意 要安裝喔!
pip install soundfile
pip install SpeechRecognition
"""
import soundfile
import speech_recognition as sr
import numpy as np
#typing
data: np.array = np.array([])
samplerate: int = 44100
data, samplerate = soundfile.read('out.wav') # 讀取out.wav 檔案
r = sr.Recognizer() # 預設辨識英文 初始化
#Recognizer
# wave 檔案 轉檔 PCM_16
soundfile.write('new.wav', data, samplerate, subtype='PCM_16')
with sr.WavFile("new.wav") as source: # 讀取wav檔
#type(source) speech_recognition.AudioFile
audio = r.record(source)
#type(audio) speech_recognition.audio.AudioData
# 辨聲音檔案 辨識成中文輸出
try:
str1= r.recognize_google(audio,language="zh-TW") # 使用Google的服務
print("辨識後的文字: " +str1)
except LookupError:
print("錯誤:Could not understand audio")
輸出結果:
推薦hahow線上學習python: https://igrape.net/30afN