How can i make the python to wait till i complete speaking?

3.9k views Asked by At

I am writing a program to recognise the speech from a microphone and the code will process accordingly. The code I wrote for this purpose is below.

import speech_recognition as sr
import webbrowser
import pyttsx
from time import sleep

engine = pyttsx.init()
engine.setProperty('rate', 70)
r = sr.Recognizer()

def recognize(audio):
    try:
        return r.recognize(audio)
    except LookupError, e:
        print e
    return ''
with sr.Microphone() as source:
    while True:
        engine.say("Hi How can i help you ?")
        sleep(0.15)
        print "Start Speaking"
        audio = r.listen(source)
        words = recognize(audio)
        print("You said " + words)
        if words == "Facebook":
            engine.say("Shall i open the Facebook page for you ?")
            engine.runAndWait()
            audio = r.listen(source)
            words = recognize(audio)
            if words == "Yes":
                webbrowser.open('https://www.facebook.com')
        elif words == "stop":
            break

Here I tried sleep also but before the engine speaks I can see the text Start Speaking getting printed. Instead of Sleep, is there any nice way to capture the speech in microphone and wait till say something or for a long silence?

2

There are 2 answers

0
Nikolay Shmyrev On

This method:

        engine.runAndWait()

waits for speech to complete. You need to use it not just after engine.say("Shall i open the Facebook page for you ?"), but also after engine.say("Hi How can i help you ?") instead of sleep

0
David Edwards On

I normally use global variables which are frowned upon but the following is correct I think? The following two def's should help...

# contains reusable print and speech
def output_modes(output):
    engine = pyttsx3.init()
    print(f"Output: {output}")
    engine.say(output)
    engine.runAndWait()

# contains reusable grabbing audio
def input_modes():

    r1 = sr.Recognizer()
    mic1 = sr.Microphone()

    with mic1:
        try:
            output = r1.recognize_google(r1.listen(mic1))
            output_modes()
        except sr.UnknownValueError:
            output = "Unknown Error M1"
            output_modes()
        except sr.RequestError as e:
            output = "Error M2; {0}".format(e)
            output_modes()

You should be able to write a While loop that can call on input_modes() to listen or output_modes to speak for example

def interact():
    if input == 'Hello':
        output = 'Hi there'
        output_modes