【问题标题】:Microsoft Azure Speech-to-Text MVC applicationMicrosoft Azure Speech-to-Text MVC 应用程序
【发布时间】:2021-01-31 05:52:39
【问题描述】:

我正在开发一个使用 Microsoft 认知服务 Speech-to-Text 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()

Speech-to-Text Transcribe 代码为:

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。我希望有人可以帮助我。

【问题讨论】:

    标签: model-view-controller speech-to-text azure-cognitive-services


    【解决方案1】:

    你需要更换这部分

    print(all_results)
    

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

    如果没有,请在 UI 中公开一个按钮,以将 Speech_recognizer.start_continuous_recognition() 作为单独的函数调用并选择要处理的结果。这样可以避免冻结 UI

    【讨论】:

      猜你喜欢
      • 2019-11-14
      • 2019-05-03
      • 1970-01-01
      • 1970-01-01
      • 2016-07-31
      • 2020-03-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多