首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Microsoft Azure语音到文本MVC应用程序

Microsoft Azure语音到文本MVC应用程序
EN

Stack Overflow用户
提问于 2020-10-17 01:23:26
回答 1查看 37关注 0票数 0

我正在开发一个使用Microsoft Cognitive services语音到文本API的应用程序。我正在尝试创建一个GUI,一旦按下start按钮,转录的文本就会显示在文本框中,一旦按下stop按钮,转录就会停止。我是创建图形用户界面的新手,并且一直在使用PyQt5。我按照MVC (Model-View-Controller,模型-视图-控制器)来划分应用程序。GUI的代码如下所示:

代码语言:javascript
复制
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()

语音到文本转写代码是:

代码语言:javascript
复制
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。我希望有人能帮我解决这个问题。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-10-17 07:13:00

您需要更换此部件

代码语言:javascript
复制
print(all_results)

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

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64393716

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档