I have seen similar issues with sr used with python especially when having a running gui. But there is not one question or answer that really solves my problem. I want to run the speech recognition code in a QThread while the main thread takes care of the gui. The sr code works fine when it's executed in the main thread but NOT when I move it to QThread. After debugging, it seems that the code crashes on the the line:
with sr.Microphone() as source:
Process finished with exit code -1073740791 (0xC0000409)
I have read that it's because I have a while True loop in my Qthread, which seems to be wrong because even when I remove it, it still crashes. Any idea why I'm getting this? Here's the code below:
# Create worker
class VoiceWorker(QThread):
sentChat = pyqtSignal(str)
def __init__(self, parent=None):
QtCore.QThread.__init__(self, parent)
def run(self):
r = sr.Recognizer()
while True:
print("Say somethig!")
with sr.Microphone() as source:
audio = r.listen(source)
said = ""
print("Got it! Now to recognize it...")
try:
said = r.recognize_google(audio)
# self.textChanged.emit(value)
# self.sentChat.emit(said)
print(said)
except sr.UnknownValueError:
print("Oops")
class Ui_OutputDialog(QDialog):
def __init__(self):
# Create window UI
super(Ui_OutputDialog, self).__init__()
loadUi("./outputwindow.ui", self)
# Create Qthread for always listening
self.listening_worker = VoiceWorker()
self.listening_worker.start()
if __name__ == "__main__":
app = QApplication(sys.argv)
ui = Ui_OutputDialog()
ui.show()
sys.exit(app.exec_())