Model view controller Microsoft Azure语音到文本MVC应用程序

Model view controller Microsoft Azure语音到文本MVC应用程序,model-view-controller,speech-to-text,azure-cognitive-services,Model View Controller,Speech To Text,Azure Cognitive Services,我正在开发一个使用Microsoft认知服务语音到文本API的应用程序。我试图创建一个GUI,一旦按下开始按钮,转录文本就会显示在文本框中,一旦按下停止按钮,转录就会停止。我对创建GUI非常陌生,并且一直在使用PyQt5。我已经根据MVC(模型-视图-控制器)划分了应用程序。GUI的代码如下所示: import sys import time from functools import partial import azure.cognitiveservices.speech as speec

我正在开发一个使用Microsoft认知服务语音到文本API的应用程序。我试图创建一个GUI,一旦按下开始按钮,转录文本就会显示在文本框中,一旦按下停止按钮,转录就会停止。我对创建GUI非常陌生,并且一直在使用PyQt5。我已经根据MVC(模型-视图-控制器)划分了应用程序。GUI的代码如下所示:

import sys
import time
from functools import partial

import azure.cognitiveservices.speech as speechsdk

from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *


class test_view(QMainWindow):
    def __init__(self):
         super().__init__()
         self.generalLayout = QVBoxLayout()
         self._centralWidget = QWidget(self)
         self.setCentralWidget(self._centralWidget)
         self._centralWidget.setLayout(self.generalLayout)

         self._createApp()

    def _createApp(self):
        self.startButton = QPushButton('Start')
        self.stopButton = QPushButton('Stop')
        buttonLayout = QHBoxLayout()
        self.startButton.setFixedWidth(220)
        self.stopButton.setFixedWidth(220)
        buttonLayout.addWidget(self.startButton)
        buttonLayout.addWidget(self.stopButton)

        self.text_box = QTextEdit()
        self.text_box.setReadOnly(True)
        self.text_box.setFixedSize(1500, 400)
        layout_text = QHBoxLayout()
        layout_text.addWidget(self.text_box)
        layout_text.setAlignment(Qt.AlignCenter)

        self.generalLayout.addLayout(buttonLayout)
        self.generalLayout.addLayout(layout_text)

    def appendText(self, text):
        self.text_box.append(text)
        self.text_box.setFocus()

    def clearText(self):
         return self.text_box.setText('')


class test_ctrl:
    def __init__(self, view):
        self._view = view


def main():
    application = QApplication(sys.argv)
    view = test_view()
    view.showMaximized()
    test_ctrl(view=view)
    sys.exit(application.exec_())


if __name__ == "__main__":
   main()
语音到文本转录代码为:

import azure.cognitiveservices.speech as speechsdk
import time


def setupSpeech():
    speech_key, service_region = "speech_key", "service_region"
    speech_config = speechsdk.SpeechConfig(subscription=speech_key, region=service_region)
    speech_recognizer = speechsdk.SpeechRecognizer(speech_config=speech_config)

    return speech_recognizer


def main():
    speech_recognizer = setupSpeech()

    done = False

    def stop_cb(evt):
        print('CLOSING on {}'.format(evt))
        speech_recognizer.stop_continuous_recognition()
        nonlocal done
        done = True

    all_results = []

    def handle_final_result(evt):
        all_results.append(evt.result.text)

    speech_recognizer.recognizing.connect(lambda evt: print(evt))
    speech_recognizer.recognized.connect(handle_final_result)
    speech_recognizer.session_stopped.connect(stop_cb)
    speech_recognizer.canceled.connect(stop_cb)

    speech_recognizer.start_continuous_recognition()
    while not done:
        time.sleep(.5)

    print(all_results)


if __name__ == "__main__":
    main()

我确信这两段代码都可以工作,但我不确定如何将语音到文本代码构建到MVC代码中。我认为它应该与模型一起工作,并且应该通过控制器连接到视图。我尝试了多种方法,但我就是想不出来。我还认为我需要某种线程来防止代码冻结GUI。我希望有人能帮我解决这个问题。

你需要更换这个零件

print(all_results)
并将所有_结果异步推送到ur代码以处理文本

如果没有,请在UI中显示一个按钮以调用语音识别器。作为单独的函数启动连续识别()并选择要处理的结果。这样可以避免冻结UI