我正在编写一个基本的会说话的汤姆之类的代码,它听这个人并沿边重复它的音频,我希望它显示说话者所说的文本。我面临的问题是我无法同时使用 print 和 listen 命令。我需要说这句话两次,即一次用于在屏幕上打印,另一次用于重复。我想弄清楚这两件事只能同时发生,即音频应该同时重复和显示,而不需要我重复这句话两次。
import speech_recognition
import pyttsx
speech_engine = pyttsx.init()
speech_engine.setProperty('rate', 150)
def speak(text):
speech_engine.say(text)
speech_engine.runAndWait()
recognizer = speech_recognition.Recognizer()
def listen():
with speech_recognition.Microphone() as source:
recognizer.adjust_for_ambient_noise(source)
audio = recognizer.listen(source)
try:
return recognizer.recognize_google(audio)
except speech_recognition.UnknownValueError:
print("Could not understand audio")
except speech_recognition.RequestError as e:
print("Recog Error; {0}".format(e))
return ""
speak("Say something!")
print (listen())
speak("I heard you say " + listen())