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



我正在开发一个使用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()

语音到文本转录代码是:

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异步推送到ur代码以处理文本。

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

最新更新