当我尝试在pyqt5程序中运行语音识别时崩溃

时间:2019-05-18 15:46:08

标签: python pyqt speech-recognition

当我尝试在pyqt5程序中运行语音识别时崩溃了。 Sr的代码在另一个脚本文件中。我将其导入pyqt5脚本。我用sr功能连接了按钮。当我按下按钮sr时,它却无法正常工作。

PyQt5代码:

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

语音识别

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



1 个答案:

答案 0 :(得分:1)

您的代码中有一段时间True会阻塞GUI的事件循环,在这种情况下,建议在另一个线程中执行该任务:

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

加号:

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