0

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-

  1. start recording
  2. start taking time

when i click the Done button-

  1. finish recording
  2. run the speech recognition
  3. return the recognized the text and the time it took to record to global variables for later logging
  4. move to the next frame

Thank you

Navot Naor
  • 59
  • 5
  • You may need to put that speech recording part in a separate thread so that it doesn't interfere with tkitner's mainloop. – Matiiss May 15 '21 at 15:16
  • When your `rec()` function is called, your tkinter GUI will hang until it returns because it interferes with the the tkinter `mainloop()`. There are a number of questions here about using tkinter in conjunction with background asynchronous tasks. You may have to use multiple threads to do what you want, but only one of them can make tkinter calls. See [Freezing/Hanging tkinter Gui in waiting for the thread to complete](https://stackoverflow.com/questions/53696888/freezing-hanging-tkinter-gui-in-waiting-for-the-thread-to-complete) for an example. – martineau May 15 '21 at 15:16
  • For processes that take a long time to compute you have to put it in a separate thread – Omid Ketabollahi May 15 '21 at 17:53
  • You can use threading as the commentators above mentioned. Another issue: to consistently use quotation marks. Parts of your code are intepreted as string : `text='this will be some text for the user to read...",font="helvetica 28",` – Julian Fock May 15 '21 at 22:25

0 Answers0