gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 04:11:35 24 4
gpt4 key购买 nike

当我尝试在 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")



最佳答案

你的代码有一个 while 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()

关于python - 当我尝试在 pyqt5 程序中运行语音识别时崩溃了,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56200533/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com