Microsoft Azure 语音转文本 MVC 应用程序

Microsoft Azure Speech-to-Text MVC application

我正在开发一个使用 Microsoft Cognitive 服务 Speech-to-Text API 的应用程序。我正在尝试创建一个 GUI,一旦按下开始按钮,转录的文本应该显示在文本框中,一旦按下停止按钮,转录就会停止。我对创建 GUI 还很陌生,一直在使用 PyQt5。我按照MVC(Model-View-Controller)划分了应用程序。 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()

Speech-to-Text 转录代码是:

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)

并异步推送 all_results 到您用于处理文本的代码。

如果不是,则在 UI 中公开一个按钮以调用 speech_recognizer.start_continuous_recognition() 作为单独的函数并选择要处理的结果。这样你就可以避免冻结 UI