how to use speech recognition modules as r listen in background

0 votes
i am making a GUI chat bot which takes voice input from user but whenever i open the window it does not take the command rather it takes command after the window is closed. i am making the window using pygame. please help i have to make this project before my competition gets amended.
Jun 25, 2020 in Python by Shaurya Thapliyal
• 340 points
4,675 views

Hello @ Shaurya Thapliyal,

Can you please provide you snippet or provide more information about the error you're getting?

yes here have it,

this is the gameloop() 

this is the full code-

import pyttsx3
import os
import datetime
import speech_recognition as sr
import wikipedia
import webbrowser
import random
import wolframalpha
import mysql.connector as con
import ezgmail
import pyautogui
import pygame
from nltk.chat.util import Chat, reflections
from PIL import ImageTk,Image
from tkinter import *

engine = pyttsx3.init("sapi5")
voices = engine.getProperty("voices")
engine.setProperty('voice',voices[1].id)

def speak(audio):
    engine.say(audio)
    engine.runAndWait()

def wishme():
    hr = int(datetime.datetime.now().hour)
    if hr >= 0 and hr < 12:
        speak("good morning")
    elif hr >= 12 and hr < 18:
        speak("good afternoon")
    else:
        speak("good evening")
    speak("hi my name is pandora i am your digital assistant")

def takecommand():
    r = sr.Recognizer()
    with sr.Microphone() as source:
        print("listening...")
        r.pause_threshold = 1
        r.adjust_for_ambient_noise(source)
        audio = r.listen(source)
    try:
        print("recognizing...")
        query = r.recognize_google(audio,language = "en-in")
        print(f"user said:{query}\n")
    except Exception as e:
        speak("sorry sir i did not got that")
        print(e)
        query = pyautogui.prompt("commmand:")
    return query

def database():
    mycon = con.connect(host="localhost",user="root",passwd="123",database="test")
    if mycon.is_connected():
        speak("body connected to brain")
        print("BODY connected to BRAIN")
    cur = mycon.cursor()
    query = "select name from student"
    cur.execute(query)
    y = tuple()
    for x in cur:
        y = y + (x,)
    a = list(y)

def gameloop():
    speak("sir please give your command:")
    command = takecommand().lower()
    if "introduce" in command:
        txt = font.render("python automated newly developed and organised requisite assistant",True, blue)
        txt.get_rect()
        gameWindow.blit(txt,(0,0))
        pygame.display.update()
        speak("python automated newly developed and organised requisite assistant")

if __name__ == "__main__":
    speak("passwd please:")
    print("passwd please:")
    query = takecommand().lower()
    if "shaurya" in query:
        speak("initialising")
        speak("opening interface")
        x = pygame.init()
        blue = (0,0,225,225)
        white = (225,225,225,225)
        gameWindow = pygame.display.set_mode((1920,1020))
        pygame.display.set_caption("PANDORA")
        exit_game = False
        game_over = False
        arc = pygame.image.load(r"C:\Users\SHAURYA\Downloads\os personalize\IMG-20200622-WA0004.JPG")
        font = pygame.font.Font("freesansbold.ttf", 115)
        text = font.render('P.A.N.D.O.R.A', True, blue)
        text.get_rect()
        gameWindow.blit(text,(0,0))
        pygame.display.update()
        while not exit_game:
            pyautogui.press("space")
            gameWindow.blit(arc,(600,100))
            pygame.display.update()
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    exit_game = True
                if event.type == pygame.KEYDOWN:
                    if event.key == pygame.K_SPACE:
                        gameloop()
    else:
        speak("wrong passwd")
        print("wrong passwd")

1 answer to this question.

+1 vote

Hello Shaurya Thapliyal,

Following might be the reason for your error:​

The possible reason could be that the recognizer_instance.energy_threshold property is probably set to a value that is too high to start off with. You should decrease this threshold, or call recognizer_instance.adjust_for_ambient_noise(source, duration = 1).


If it's a continious loop of audio, the stream might never leave audio = r.listen(source) or your identation is wrong. Consider using a non-blocking object such as the select library. If you're on Windows I'd might thread the output some how.


Use this ambient noise suppression/adjustment;

r.adjust_for_ambient_noise(source, duration=1)

Ensure pyaudio.h is installed by running the below command

        sudo apt-get install portaudio19-dev python-pyaudio python3-pyaudio


Hope it helps!!

Thank You!!

answered Jun 29, 2020 by Niroj
• 82,840 points
thank you sir i will be in great debt.

Related Questions In Python

0 votes
1 answer

How to use Pandas HDF5 as a Database in Python?

HDF5 works fine for concurrent read only ...READ MORE

answered Nov 30, 2018 in Python by Nymeria
• 3,560 points

edited Dec 10, 2018 by Nymeria 1,307 views
+2 votes
2 answers

How to use BeatifulSoup for webscraping?

your programme is fine until you start ...READ MORE

answered Apr 4, 2018 in Python by charlie_brown
• 7,720 points
1,069 views
0 votes
1 answer

Raw_input method is not working in python3. How to use it?

raw_input is not supported anymore in python3. ...READ MORE

answered May 5, 2018 in Python by aayushi
• 750 points
3,361 views
0 votes
3 answers

how to use print statement in python3?

Brackets are required to print the output. >>> ...READ MORE

answered Nov 25, 2021 in Python by anonymous
1,622 views
0 votes
2 answers
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 7, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 4,434 views
0 votes
1 answer
+1 vote
7 answers

How to use not equal operator in python

To check if operands are not equal ...READ MORE

answered Nov 30, 2021 in Python by Charry
371,015 views
0 votes
1 answer

How to keep keys/values in same order as declared?

You can't really do what you want ...READ MORE

answered Apr 13, 2020 in Python by Niroj
• 82,840 points
689 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP