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")
輸出結果:

腳本將數據以 PCM_16 的特定子類型寫回一個新的 WAV 文件 new.wav。然後使用 speech_recognition 打開並讀取這個 WAV 文件。但如果 out.wav 已經是speech_recognition可以讀取的格式,這個步驟可能是多余的,除非你需要確保文件格式或子類型的特定要求。
推薦hahow線上學習python: https://igrape.net/30afN
![Python 讀取 DOCX 圖片關聯:qn+find/findall 與 XPath 的實戰對照 from lxml import etree ; from docx.oxml.ns import qn; lxml.etree._Element.findall( f”.//{ qn(‘a:blip’) }” ) ; .get( qn(“r:embed”) ) #獲取 屬性名 ‘r:embed’ 的 屬性值(如: ‘rId4’) ; lxml.etree._Element.xpath( “//a:blip/@r:embed”, namespaces = NS) #/@r:embed = 獲取 屬性名 ‘r:embed’ 的 屬性值(如: ‘rId4’),使用.findall() 要先.findall()獲取List[_Element],再迴圈_Element.get()獲取屬性值, .xpath() 第一個參數path 使用”//a:blip/@r:embed” ,可直接獲取屬性值(List[str]如: [‘rId4’, ‘rId5’]) ; 如何對docx真實移除圖片瘦身? Python 讀取 DOCX 圖片關聯:qn+find/findall 與 XPath 的實戰對照 from lxml import etree ; from docx.oxml.ns import qn; lxml.etree._Element.findall( f”.//{ qn(‘a:blip’) }” ) ; .get( qn(“r:embed”) ) #獲取 屬性名 ‘r:embed’ 的 屬性值(如: ‘rId4’) ; lxml.etree._Element.xpath( “//a:blip/@r:embed”, namespaces = NS) #/@r:embed = 獲取 屬性名 ‘r:embed’ 的 屬性值(如: ‘rId4’),使用.findall() 要先.findall()獲取List[_Element],再迴圈_Element.get()獲取屬性值, .xpath() 第一個參數path 使用”//a:blip/@r:embed” ,可直接獲取屬性值(List[str]如: [‘rId4’, ‘rId5’]) ; 如何對docx真實移除圖片瘦身?](https://i0.wp.com/savingking.com.tw/wp-content/uploads/2025/11/20251119130848_0_3fbf6b.png?quality=90&zoom=2&ssl=1&resize=350%2C233)

![Python: pandas.DataFrame (df) 的取值: df [單一字串] 或df [list_of_strings] 選取一個或多個columns; df [切片] 或 df [bool_Series] 選取多個rows #bool_Series長度同rows, index也需要同df.index ,可以使用.equals() 確認: df.index.equals(mask.index) Python: pandas.DataFrame (df) 的取值: df [單一字串] 或df [list_of_strings] 選取一個或多個columns; df [切片] 或 df [bool_Series] 選取多個rows #bool_Series長度同rows, index也需要同df.index ,可以使用.equals() 確認: df.index.equals(mask.index)](https://i0.wp.com/savingking.com.tw/wp-content/uploads/2025/04/20250420212553_0_6fb2c3.png?quality=90&zoom=2&ssl=1&resize=350%2C233)
![Python: 如何使用 os.environ[“PATH”] 設定環境變數?與 sys.path.append() 差別為何? Python: 如何使用 os.environ[“PATH”] 設定環境變數?與 sys.path.append() 差別為何?](https://i0.wp.com/savingking.com.tw/wp-content/uploads/2024/09/20240905135312_0_890fa1.png?quality=90&zoom=2&ssl=1&resize=350%2C233)






近期留言