0

I'm doing a project on speech recognition and am trying to use Tkinter to create a GUI for my project...the SR part works well but when I integrate it with Tkinter it doesn't work...please help. (am new to programming so please don't mind my code:) )

#MY CODE
import speech_recognition as sr
import tkinter as tk

obj = tk.Tk()
obj.title("SpeechToText")
obj.geometry('400x200')
obj.resizable(0,0)

def rec():
    r = sr.Recognizer()
    msg.configure(text="Say something")
    while True:
        with sr.Microphone() as source: 
            r.adjust_for_ambient_noise(source)
            audio = r.listen(source)
        try:
            txt = "".format(r.recognize_google(audio).get())
            msg.configure(text=txt)
        except Exception as e:
            print(e)
            break

msg = tk.Label()
msg.grid(row=0,column=0)
btn = tk.Button(text="Start",command=rec)
btn.grid(row=2,column=0)
obj.mainloop()

I would like it to display the translated text in the label but it doesn't. It only shows "say something" even after speaking.

Conrad Max
  • 11
  • 6

1 Answers1

3

Try this, I blocked out the msg.configure(text='Say Somethin'). This line makes the recorded text being reformatted to 'Say Something' and not the text that was recorded. Hope this helps.

import speech_recognition as sr
import tkinter as tk

obj = tk.Tk()
obj.title("SpeechToText")
obj.geometry('400x200')
obj.resizable(0,0)

def rec():
    r = sr.Recognizer()
    #msg.configure(text="Say something")
    while True:
        with sr.Microphone() as source: 
            r.adjust_for_ambient_noise(source)
            audio = r.listen(source)
        try:
            txt = r.recognize_google(audio)
            msg.configure(text=txt)
            print(txt)
        except Exception as e:
            print(e)
            break

msg = tk.Label()
msg.grid(row=0,column=0)
btn = tk.Button(text="Start",command=rec)
btn.grid(row=2,column=0)
obj.mainloop()
Rutvik
  • 31
  • 2