我正在开发一个文本到语音程序,你说了什么,它会在字典中找到它,并在字典中用它的匹配项进行回复。输入一切正常并且工作正常,但是这不会产生任何声音。没有错误。这是我的代码:
import speech_recognition as sr
import os
import sys
import pyttsx3
devnull = os.open(os.devnull, os.O_WRONLY)
old_stderr = os.dup(2)
sys.stderr.flush()
os.dup2(devnull, 2)
os.close(devnull)
def say(sp):
engine = pyttsx3.init()
engine.say(sp)
engine.runandwait()
def get_audio_and_reply():
r = sr.Recognizer()
with sr.Microphone() as source:
audio = r.listen(source)
said = ""
try:
said = r.recognize_google(audio)
print("You: " + said)
what_to_say={"hi":"Hello",
"hello":"Hi" ,
"hi neuron":"Hello",
"hello neuron":"Hi",
"what's your name":"Neuron",
"what is your name":"Neuron",
"how are you":"I do not know because I do not have feelings!"
}
try:
say(what_to_say[said])
except:
say("I do not, as of yet, understand this")
except:
pass
while True:
get_audio_and_reply()
有人有解决办法吗?