-2

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?

Cory Kramer
  • 114,268
  • 16
  • 167
  • 218
Roshan r
  • 522
  • 2
  • 11
  • 30
  • 4
    Why are you using sleep ? That won't help, You must set a threshold for `audio` as returned by `r.listen(source)` and if the `audio` is greater than threshold then you are speaking something otherwise it is a long silence. – ZdaR Jun 19 '15 at 12:57
  • Cory.. Can you let me know how can i set the threshold? – Roshan r Jun 19 '15 at 12:59
  • There is no direct assumption as such it highly depends on the values returned by `r.listen(source)`, you need to experiment a bit and analyse the output for voice sample of various altitudes, and select appropriately. – ZdaR Jun 19 '15 at 13:02
  • But here i need to wait till engine completes saying "Hi How can i help you ?" – Roshan r Jun 19 '15 at 13:06

2 Answers2

2

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

Nikolay Shmyrev
  • 24,897
  • 5
  • 43
  • 87
0

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