Help needed with SpeechRecognition module

Hey I tried making a personal assistant for myself. When you say PyBot then it says “Good morning sir, how may I help you”. Then it should still be listening. So after telling PyBot it should keep listening and if I tell open edge it should open edge(open edge feature yet to be coded). But looks like it is giving an error

import playsound
import pyaudio
import speech_recognition as sr 
import time
import subprocess
import pyttsx3

Repeater_Variable = 5

Recognition = sr.Recognizer()

Announcer = pyttsx3.init()

while (Repeater_Variable == 5):
    with sr.Microphone() as source:
        print("Speak anything")
        Listener = Recognition.listen(source)

    try:
        #Listens to the audio and converts it into text using google service
        Spoken_Text = Recognition.recognize_google(Listener)


        if Spoken_Text == "PyBot":
            Announcer.say("Good morning sir, how may I help you")
            Announcer.runAndWait()

            if Spoken_Text == "Open edge" or Spoken_Text == "open edge":
                Announcer.say("Still in development")
                Announcer.runAndWait()


        else:
            Announcer.say("Failed to recognize audio please try again")
            Announcer.runAndWait()

        
    except:
        print("Sorry could not recognize the test")

Thanks for reading/ answering to the post :pray:

Hello, @PythonBoy . It is good to see that you are still working on your project.
I think, your main problem is that your code and if statements are being executed too fast and even if you say something, your program may not be executing the suitible if statement. So, while loops can not help you properly in this kind of cases. Most programmers use the keyword “with” to run self-looped methods. And, I can see this usage in your code, which is a good thing but in this case, I am a bit worried about it. -It is possible for interpreter to just read thewith block which is nested in a while block. But, I am not sure about it at all.- So,I think, you can use a with block instead of a while loop. And, you can put all stuff about with in this one block. I tried re-writing the code as below:

import playsound
import pyaudio
import speech_recognition as sr 
import time
import subprocess
import pyttsx3

Repeater_Variable = 5

Recognition = sr.Recognizer()
Announcer = pyttsx3.init()
print("Speak anything")
said_PyBot = False
with sr.Microphone() as source: 
    Recognition.adjust_for_ambient_noise(source, duration=0.2)#let recognizer to adjust the energy threshold based on the surrounding noise level.
    Listener = Recognition.listen(source)
    Spoken_Text = Recognition.recognize_google(Listener)
    try:
        #Listens to the audio and converts it into text using google service
        if Spoken_Text.lower() == "py bot" or Spoken_Text.lower() == "pybot":
            Announcer.say("Good morning sir, how may I help you")
            said_PyBot = True
            Announcer.runAndWait()
        elif (Spoken_Text == "Open edge" or Spoken_Text == "open edge") and said_PyBot ==True:
            Announcer.say("Still in development")
            Announcer.runAndWait()
        else:
            #Announcer.say("Failed to recognize audio please try again") #I removed this line because this would be repeated thousands of times.
            Announcer.runAndWait()
    except:
        print("Sorry could not recognize the test")         

I hope this code will help you. It may crash, or don’t work; its out of warranty. (Of course, that doesn’t mean that you can’t feedback me🙃.)
[EDIT:I am sorry for my deleted post, I clicked reply by mistake.]

1 Like

Hey thanks for the help. Yea I’d never give up in life. Looks like I will have to remove PyBot line as it will be too complex to hear PyBot first then Hear Open edge again after that. So I am going to just use Open Edge directly instead of saying PyBot first then Open edge since I am still a beginner. Thanks for the help. Good to see you here. I am really thankful to you for helping me many times

You are welcome and thank you for your nice words. If removing the line you mentioned about-or any other changes- makes the code working properly, please let me know about it.(or vice versa).
EDIT&NOTE: I have found some useful links about speech recognition:

  1. Speech Recognition in Python - A Complete Beginner's Guide
  2. The Ultimate Guide To Speech Recognition With Python – Real Python
    . I have also edited the code I sent. You may try it again. Hope will help you.
1 Like

Really thanks for the links. I will defenitely look forward to it. To make it functional yet simple, I am planning to make it so that when you press the letter “Q” then it starts listening to the commands. This will be possible using pynput module. Pynput is used to listen or do actions on the keyboard or the mouse. Sure I will keep you update in the DM’s and I will also show how the program works since you have helped me in the past. Thanks for your help!

You are welcome -and thank you. I will be proud of that.:slight_smile:

1 Like