I have this code, i want it to start recording what the user is saying and convert it to text and log the time it took and the text that was recognized.
my code looks like this.
root = Tk()
root.title('Amplio Home Task')
root.iconbitmap('images/AmplioLogo.ico')
root.geometry("1000x600")
def rec():
r = sr.Recognizer()
#msg.configure(text="Say something")
while True:
with sr.Microphone() as source:
r.adjust_for_ambient_noise(source)
audio_text = r.listen(source)
try:
txt = (r.recognize_google(audio_text))
print(txt)
except Exception as e:
print(e)
break
def fnc1to2():
welcome_frame.pack_forget()
target_text_frame.pack()
label.grid(row=0,column=0)
button3.grid(row=5,column=0)
button2.grid(row=6,column=0)
def fnc2to3():
target_text_frame.pack_forget()
word_meaning_frame.pack()
labe2.pack()
button4.pack()
# crate the frame
welcome_frame = Frame(root)
button1 = Button(welcome_frame, text='Start', command = fnc1to2)
button1.pack()
target_text_frame = Frame(root)
label = Label(target_text_frame, text='this will be some text for the user to read...",font="helvetica 28", wraplength=800, justify="center")
button2 = Button(target_text_frame, text='Done', command = fnc2to3)
button3 = Button(target_text_frame, text='Start', command = rec)
word_meaning_frame = Frame(root)
labe2 = Label(word_meaning_frame, text='word meaning frame',font="helvetica 28", wraplength=800, justify="center")
button4 = Button(word_meaning_frame, text='Done', command = fnc2to3)
welcome_frame.pack()
root.mainloop()
I can't get it to record the text, and i am not sure how to get the timing to work. I have it done in a function when i am not using tkinter (below), but something in the fitting to tkinter doesn't work so smoothly for me.
def intro():
start = time.time()
print ("Some long text for the user to read...")
# Reading Microphone as source
# listening the speech and store in audio_text variable
with sr.Microphone() as source:
print("Talk\n"
'Press ENTER when you are done ')
start = time.time()
audio_text = r.listen(source)
if not input(' '):
print("Great job, thanks")
end = time.time()
readtime = (end - start)
try:
# using google speech recognition
text = (r.recognize_google(audio_text))
return text, readtime
except:
print("Sorry, I did not get that")
So what i would like to do is:
when I click the stat button-
- start recording
- start taking time
when i click the Done button-
- finish recording
- run the speech recognition
- return the recognized the text and the time it took to record to global variables for later logging
- move to the next frame
Thank you