我正在用 Windows 上的 python 编写个人助理,它工作得非常糟糕,有时我有错误 13 - 对存储语音的文件的权限被拒绝。有时它直接无法识别我的声音,有时它会花费一分钟或更长时间来识别我的声音。查看代码,我应该改进哪些内容以使其更好地工作?
import os
import time
import playsound
import speech_recognition as sr
from gtts import gTTS
def speak(text):
tts = gTTS(text=text, lang="es-ES")
filename = "voice.mp3"
tts.save(filename)
playsound.playsound(filename)
def get_audio():
r = sr.Recognizer()
with sr.Microphone() as source:
audio = r.listen(source)
said = ""
try:
said = r.recognize_google(audio, language="es-ES")
print(said)
except Exception as e:
print("Exception: " + str(e))
return said
speak("Di algo")
get_audio()