im trying to build a small project with speech to text and text to speech functionality. The speech to text function has an attribute error, this works fine when you run that block separately though, any help would be highly appreciated.
Please check the test_numbers function where the speech_to_text function is called.
Sample Error:- output_number=tk.Label(self.window, text=self.MyText, fg='Black', font=("Helvetica", 16)) AttributeError: 'kid_learning' object has no attribute 'MyText'
Code:-
import tkinter as tk
import pyttsx3
import time
import random
import speech_recognition as sr
# creating basic window
# window = tk.Tk()
# window.geometry("624x650") # window size
# window.resizable(0, 0) # prevents window resizable
# window.title("Kids Learning app")
class kid_learning ():
def __init__(self):
self.window = tk.Tk()
self.window.geometry("624x650") # window size
# self.window.resizable(0, 0) # prevents window resizable
self.window.title("Kids Learning app")
# self.MyText=''
## clear frame
def clearFrame(self):
# destroy all widgets from frame
for widget in self.window.winfo_children():
widget.destroy()
# this will clear frame and frame will be empty
# if you want to hide the empty panel then
## this is the welcome page
def welcome_page(self):
self.clearFrame()
welcome_text=tk.Label(self.window, text="Welcome to Kid's Learning", fg='Black', font=("Helvetica", 16)) # welcome label wiget
welcome_text.place(x=60, y=50)
modules= tk.StringVar()
modules.set("Select Any Module")
module_menu= tk.OptionMenu(self.window, modules,"Numbers")
module_menu.pack()
module_menu.place(x=60,y=90)
select=tk.Button(self.window,text='Select',command=self.number_module_page)
select.place(x=60,y=120)
self.window.mainloop()
## this is the number module page
def number_module_page(self):
self.clearFrame()
welcome_text=tk.Label(self.window, text="Welcome to the number's module", fg='Black', font=("Helvetica", 16)) # welcome label wiget
welcome_text.place(x=60, y=50)
play_ivr_bt=tk.Button(self.window,text='Play IVR',command=self.ivr_numbers)
play_ivr_bt.place(x=60,y=120)
test_bt=tk.Button(self.window,text='Take Test',command=self.test_numbers)
test_bt.place(x=60,y=150)
self.window.mainloop()
## this is the ivr for text to speech function
def text_to_speech(self,Text):
self.engine = pyttsx3.init()
voices = self.engine.getProperty('voices')
volume = self.engine.getProperty('volume')
rate = self.engine.getProperty('rate')
self.engine.setProperty('volume',1)
self.engine.setProperty('voice', voices[0].id)
self.engine.setProperty('rate', 140)
self.engine.say(Text)
self.engine.runAndWait()
## this is the ivr function for speech to text
def speech_to_text(self):
r = sr.Recognizer()
# Reading Microphone as source
# listening the speech and store in audio_text variable
with sr.Microphone() as source:
print("Talk")
audio_text = r.listen(source)
print("Time over, thanks")
# recoginize_() method will throw a request error if the API is unreachable, hence using exception handling
try:
# using google speech recognition
print("Text: "+r.recognize_google(audio_text))
except:
print("Sorry, I did not get that")
self.MyText=r.recognize_google(audio_text)
def ivr_numbers(self):
self.clearFrame()
welcome_text=tk.Label(self.window, text="Please Repeat after me", fg='Black', font=("Helvetica", 16)) # welcome label wiget
welcome_text.place(x=60, y=50)
## display text
Text= '''
welcome to module 1 please repeat after me
1 2 3 4 5 6 7 8 9 10
'''
display_text=tk.Label(self.window, text=Text, fg='Black', font=("Helvetica", 16)) # welcome label wiget
display_text.place(x=60, y=50)
## repeat button
repeat_bt=tk.Button(self.window,text='Replay',command=self.ivr_numbers)
repeat_bt.place(x=90,y=120)
## exit button
repeat_bt=tk.Button(self.window,text='Exit',command=self.welcome_page)
repeat_bt.place(x=90,y=150)
self.text_to_speech(Text)
self.window.mainloop()
def test_numbers(self):
self.clearFrame()
welcome_text=tk.Label(self.window, text="Repeat the number that pops up", fg='Black', font=("Helvetica", 16))
welcome_text.place(x=60, y=50)
number=random.randint(1,10)
## show number
number=tk.Label(self.window, text=number, fg='Black', font=("Helvetica", 16))
number.place(x=90, y=80)
start_bt=tk.Button(self.window,text="Click to Speak",command=self.speech_to_text)
start_bt.place(x=90,y=120)
# time.sleep(10)
output_number=tk.Label(self.window, text=self.MyText, fg='Black', font=("Helvetica", 16))
output_number.place(x=90, y=120)
# time.sleep(2)
if self.MyText == str(number):
print('correct',self.MyText)
else:
print('false',self.MyText,number)
self.window.mainloop()
s=kid_learning()
s.welcome_page()
I tried multiple things like declaring the function globally, indentation etc. Didnt find any luck
what i want is, once the "click to speak" button is clicked the speech to text should activate and whatever is conveyed by the user should be captured, (then if correct print correct else incorrect)