0

I'm newbie in Python and PysimpleGUI. I want to create a simple GUI for speech to text. Can Pysimple GUI make it? Or is there another GUI framework that can work other than PysimpleGUI? Now I have done creating the GUI but have some problem, I want to create a code for speak button to always get the voice and stop button when I want to stop get voice, but I don't know how to stop. When I test the program will freeze and I get the error:

>Traceback (most recent call last):
  File "C:/Users/sumet3412/PycharmProjects/Speech2Txt/testPysimpleGUI.py", line 123, in <module>
    value = r.recognize_google(audio, language="en-US")
  File "C:\Users\sumet3412\PycharmProjects\Speech2Txt\venv\lib\site-packages\speech_recognition\__init__.py", line 672, in recognize_google
    if "alternative" not in actual_result: raise UnknownValueError()
speech_recognition.UnknownValueError
import speech_recognition as sr
import  PySimpleGUI as sg

r = sr.Recognizer()
m = sr.Microphone()

layout = [[sg.Text('Converter', font='Helvetica 15')],
          [sg.ReadButton('Speak'), sg.ReadButton('Stop')],
          [sg.Output(size=(80, 10))],
          [sg.Exit()]]

window = sg.Window('Speech Recognition').Layout(layout)

while True:
    event,values = window.Read()
    if event is None or event == 'Exit':
        break
    elif event == 'Speak':
        with m as source:
            r.adjust_for_ambient_noise(source)
            audio = r.listen(source)
            value = r.recognize_google(audio, language="en-US")
            print(value)

window.Close()
Anurag A S
  • 725
  • 10
  • 23
LINKED
  • 1
  • 1
  • Your problem is that in order to get a button press back, you must call Read. After getting the Speak button, you go off into a routine that never returns. This is why your GUI locks up. You must break up your program into a thread to do something like this, or make your calls to record be non-blocking calls. – Mike from PSG May 14 '19 at 14:32
  • I looked at the function you're calling r.listen, and I see no mechanism to "cancel" to "terminate" recording once it starts. You can set a time limit, but with this function at least, I see no way to interrupt a recording. It's not a GUI problem as much as a problem with the speech recognition API. I would try to record using another package that DOES allow the speech recording to be cut off and then pass that recording to the recognizer. – Mike from PSG May 14 '19 at 14:40

1 Answers1

0

A late response but I've created something similar and you can set the duration on this line:

audio= r.listen(source,5)

This will work as it means the audio will be captured for 5 seconds and then transcribed. Hope this helps.