4

When I try to run speech recognition in pyqt5 program is crashed. Sr's code is at the another script file. I import it to pyqt5 script. I connected button with sr function. When I press the button sr work but crashs both.

PyQt5 Code:

import sys
from PyQt5 import QtWidgets,QtGui
from Speech_Recognition import Voice



def Gui():

    app = QtWidgets.QApplication(sys.argv)
    window = QtWidgets.QWidget()
    window.setGeometry(200,200,150,150)


    button1 = QtWidgets.QPushButton(window)
    button1.setText("Start")
    button1.clicked.connect(Voice)


    window.show()
    sys.exit(app.exec())


Gui()

Speech Recognition

import speech_recognition as sr

text = ""


def Voice():

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



    while True:
        print("Say somethig!")
        with m as source:
            audio = r.listen(source)
            print("Got it! Now to recognize it...")

            try:

                value = r.recognize_google(audio)
                text = value
                print("You said: {}".format(text))

            except sr.UnknownValueError:
                print("Oops")



eyllanesc
  • 235,170
  • 19
  • 170
  • 241
Laccazet
  • 143
  • 1
  • 7

1 Answers1

2

Your code has a while True that will block the eventloop of the GUI, in those cases it is advisable to execute that task in another thread:

import sys
import threading
from PyQt5 import QtWidgets

from Speech_Recognition import Voice


def on_clicked():
    th = threading.Thread(target=Voice)
    th.daemon = True
    th.start()


def Gui():

    app = QtWidgets.QApplication(sys.argv)
    window = QtWidgets.QWidget()
    window.setGeometry(200, 200, 150, 150)

    button1 = QtWidgets.QPushButton(window)
    button1.setText("Start")
    button1.clicked.connect(on_clicked)

    window.show()
    sys.exit(app.exec_())


Gui()

Plus:

from PyQt5 import QtCore, QtGui, QtWidgets
import speech_recognition as sr

class VoiceWorker(QtCore.QObject):
    textChanged = QtCore.pyqtSignal(str)

    @QtCore.pyqtSlot()
    def task(self):
        r = sr.Recognizer()
        m = sr.Microphone()

        while True:
            print("Say somethig!")
            with m as source:
                audio = r.listen(source)
                print("Got it! Now to recognize it...")
                try:
                    value = r.recognize_google(audio)
                    self.textChanged.emit(value)
                    print("You said: {}".format(text))
                except sr.UnknownValueError:
                    print("Oops")

def Gui():
    app = QtWidgets.QApplication(sys.argv)

    worker = VoiceWorker()
    thread = QtCore.QThread()
    thread.start()
    worker.moveToThread(thread)

    window = QtWidgets.QWidget()
    window.setGeometry(200, 200, 350, 400)
    window.setWindowTitle("Assistant") 

    title_label = QtWidgets.QLabel(window)
    title_label.setText("Assistant")
    title_label.move(135,10)
    title_label.setFont(QtGui.QFont("SansSerif", 15))

    programs_says = QtWidgets.QLabel(window)
    programs_says.setText("Programs Says")
    programs_says.move(240,100)

    you_says = QtWidgets.QLabel(window)
    you_says.move(25,100)


    you_text = QtWidgets.QLabel(window)
    worker.textChanged.connect(you_text.setText)
    you_text.move(25,150) 


    start_button = QtWidgets.QPushButton("Start")
    close_button = QtWidgets.QPushButton("Close")


    v_box = QtWidgets.QVBoxLayout()
    v_box.addStretch()
    v_box.addWidget(start_button)
    v_box.addWidget(close_button)
    window.setLayout(v_box)

    start_button.clicked.connect(worker.task)
    close_button.clicked.connect(QCoreApplication.instance().quit)
    window.show()
    sys.exit(app.exec())


Gui()
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
  • Speech Recognition is converting my voice to text. I want to equal Sr text and pyqt label. I changed the code. The text variable is coming from speech recognition script. – Laccazet May 18 '19 at 18:25
  • @Laccazet It seems that you do not know anything about Qt, just for this opportunity I will show you how to do it. If you have another doubt then create another question – eyllanesc May 18 '19 at 18:42