2023年4月29日 星期六

[Python] 語音技術與應用:語音轉文字

在這篇文章中,我們將講解一些常見的語音技術以及如何在Python中使用這些技術。


安裝套件

pip install pyaudio
pip install SpeechRecognition

匯入套件

import speech_recognition as sr
import pyttsx3
import os

語音辨識:

# 設置語音引擎
engine = pyttsx3.init()

# 設置語音識別器
r = sr.Recognizer()

# 設置麥克風作為音訊來源
with sr.Microphone() as source:
    print("說些什麼吧!")
    audio = r.listen(source)

# 將語音轉換為文字
try:
    text = r.recognize_google(audio, language='zh-TW')
    print("您說的是:" + text)
except sr.UnknownValueError:
    print("語音無法識別!")
    text = "語音無法識別"
except sr.RequestError as e:
    print("無法連接到語音識別服務: {0}".format(e))
    text = "無法連接到語音識別服務"

沒有留言:

張貼留言