2

I'm writing a program were you can talk to it and it responds like siri. I'm using Googles speech recognition and espeak to listen and talk. the conversation is then printed to a text box.

When ever the program is asked to listen using speech recognition it freezes and clicking on the GUI again then it says 'not responding', am I running this wrong or is it just impossible to run speech recognition in tkinter?

Why is it freezing?

Here's the whole code that I've written so far:

import tkinter as tk
from subprocess import call as say
import winsound
import speech_recognition as sr

def cbc(tex):

    return lambda : callback(tex)

def callback(tex):
    button = "Listen" 

    tex.insert(tk.END, button)
    tex.see(tk.END)# Scroll if necessary

def listen(tex):

        say('espeak '+"Say,,your,,command,,after,,the,,beep", shell=True)
        winsound.Beep(1000,500)

        ltext = 'listening...'
        tex.insert(tk.END, ltext)
        tex.see(tk.END)

        r = sr.Recognizer()

        with sr.Microphone() as source:
            damand = r.listen(source)

        damandtxt = (recognizer_google(damand))
        tex.insert(tk.END, damandtxt)
        tex.see(tk.END)



top = tk.Tk()
tex = tk.Text(master=top)
tex.pack(side=tk.RIGHT)
bop = tk.Frame()
bop.pack(side=tk.LEFT)


tk.Button(bop, text='Listen', command=lambda: listen(tex)).pack()
tk.Button(bop, text='Exit', command=top.destroy).pack()

top.mainloop()
linusg
  • 6,289
  • 4
  • 28
  • 78
  • 1
    It is because your computer can only do one thing at a time. When it is listening, it can't handle any GUI manipulations. In order to prevent this from happening try using multiple threads to work around this problem. – Dzhao Mar 31 '16 at 17:57
  • @Dzhao How do I add multiple threads? sorry I'm pretty new to python. – joe woodger Mar 31 '16 at 18:17
  • Just updated my answer. The line `a_thread = threading.Thread(target = callback(tex))` needed an input for the `callback` function. – Dzhao Mar 31 '16 at 18:29
  • Possible duplicate of [Tkinter window says (not responding) but code is running](http://stackoverflow.com/questions/18522171/tkinter-window-says-not-responding-but-code-is-running) – Dzhao Mar 31 '16 at 18:49
  • https://stackoverflow.com/questions/56343553/python-tkinter-hangs-while-running-cmu-sphinx Can Mr Dzhao Help me with this problem? – Muhammad Saim Hashmi May 28 '19 at 13:46

1 Answers1

3

When running a script your computer can only do one thing at a time. So if you want it to listen, for example, the computer won't be able to run any other commands while it executes the current command. The way to work around this to use multiple threads so you're computer can do two things at once. Look into Python's threading module. To be honest, I don't know much about this aspect either but this is what I've implemented in my own GUI.

import tkinter as tk
from subprocess import call as say
import winsound
import speech_recognition as sr

import threading

def cbc(tex):

    return lambda : callback(tex)

def callback(tex):
    button = "Listen" 

    tex.insert(tk.END, button)
    tex.see(tk.END)# Scroll if necessary

def listen(tex):
    def callback(tex):
        say('espeak '+"Say,,your,,command,,after,,the,,beep", shell=True)
        winsound.Beep(1000,500)

        ltext = 'listening...'
        tex.insert(tk.END, ltext)
        tex.see(tk.END)

        r = sr.Recognizer()

        with sr.Microphone() as source:
            damand = r.listen(source)

        damandtxt = (recognizer_google(damand))
        tex.insert(tk.END, damandtxt)
        tex.see(tk.END)

    a_thread = threading.Thread(target = callback(tex))
    a_thread.start()

top = tk.Tk()
tex = tk.Text(master=top)
tex.pack(side=tk.RIGHT)
bop = tk.Frame()
bop.pack(side=tk.LEFT)


tk.Button(bop, text='Listen', command=lambda: listen(tex)).pack()
tk.Button(bop, text='Exit', command=top.destroy).pack()

top.mainloop()

The basic idea is to create a thread object and then give it a function to run. Then when you want to run this thread call the thread's start() method. Definitely read up on this since things can get hairy when running more than one thread at once.

Dzhao
  • 683
  • 1
  • 9
  • 22