我的问题其实很奇怪。我的程序有时运行良好,但有时它说出了我没有要求。例如:我说“你好”,它说的是我根本没问时间的时候。 当它无法识别用户的任何语音时,它也会说出时间。 当我问什么是日期,它说的是日期,但它显示了一个错误信息
我试过切换if语句,但我对语音识别了解不多,所以没有尝试过太多的更改
import os
import time
from datetime import date
import playsound
import speech_recognition as sr
from gtts import gTTS
def speak(text):
tts = gTTS(text=text, lang="en")
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)
print(said)
except Exception as e:
print("Exception: " + str(e))
return said
text = get_audio()
today = date.today()
t = time.localtime()
current_time = time.strftime("%H:%M", t)
d2 = today.strftime("%B %d, %Y")
if "hello" in text:
speak("hello sir")
if "date" in text:
speak("the date is: " + d2)
if "what's the time" or "what is the time" in text:
speak("The time is: " + current_time)
当我说ask The date时显示的错误消息:
Traceback (most recent call last):
File "d:/python programs/hello.py", line 42, in <module>
speak("The time is: " + current_time)
File "d:/python programs/hello.py", line 11, in speak
tts.save(filename)
File "E:\python\lib\site-packages\gtts\tts.py", line 248, in save
with open(str(savefile), 'wb') as f:PermissionError: [Errno 13] Permission denied: 'voice.mp3'
当它无法识别用户的声音并返回异常时: 上面写着时间
这些都是程序的问题。你知道吗
生成unique文件名而不是“语音.mp3“每次:
相关问题 更多 >
编程相关推荐