简体   繁体   中英

cannot print and listen at the same time

I am writing a basic talking tom like code which listens to the person and repeats its audio along side i want it to display the text which the speaker has spoken. the problem I'm facing is i am not able to use the print and listen command simultaneously. I need to speak the phrase twice ie, once for printing it on screen and other time for repeating it. I want to figure it these both things to happen only at once ie, the audio should be repeated and displayed at the same time without me repeating the phrase twice.

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())
speak("Say something!")
text = listen()
speak("I heard you say " + text)

Write:

print(audio)

Under:

return recognizer.recognize_google(audio)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM