不能同时打印和收听

时间:2016-05-30 19:56:54

标签: python printing speech-recognition listen pyttsx

我正在写一个基本的会说话的汤姆,就像代码一样,听取这个人并重复它的音频,我想让它显示说话者所说的文字。 我面临的问题是我无法同时使用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())

2 个答案:

答案 0 :(得分:2)

speak("Say something!")
text = listen()
speak("I heard you say " + text)

答案 1 :(得分:0)

写:

print(audio)

下:

return recognizer.recognize_google(audio)