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

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 import Chat, reflections
from PIL import ImageTk,Image
from tkinter import *

engine = pyttsx3.init("sapi5")
voices = engine.getProperty("voices")

def speak(audio):

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

def takecommand():
    r = sr.Recognizer()
    with sr.Microphone() as source:
        r.pause_threshold = 1
        audio = r.listen(source)
        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")
        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"
    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)
        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("opening interface")
        x = pygame.init()
        blue = (0,0,225,225)
        white = (225,225,225,225)
        gameWindow = pygame.display.set_mode((1920,1020))
        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)
        while not exit_game:
            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:
        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,880 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,035 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
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
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
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,047 views
0 votes
1 answer
0 votes
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
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,880 points
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP