Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
286 views
in Technique[技术] by (71.8m points)

python - When I try to run speech recognition in pyqt5 program is crashed

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")



See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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()

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...