Python 你们能在PyQt5窗口上显示连续语音到文本吗?

Python 你们能在PyQt5窗口上显示连续语音到文本吗?,python,azure,pyqt5,speech-to-text,Python,Azure,Pyqt5,Speech To Text,我正在使用Azure语音识别,我想创建自己的语音到文本应用程序,我想在PyQt5窗口上显示文本 我仍然不知道如何才能将识别的文本作为输出显示 我从另一个来源获得了这段代码,它使用tinkter在窗口上显示文本,我想做同样的事情 原始代码的来源: 这就是我要做的事的代码! ` ` 现在输出!: 更新。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。

我正在使用Azure语音识别,我想创建自己的语音到文本应用程序,我想在PyQt5窗口上显示文本

我仍然不知道如何才能将识别的文本作为输出显示

我从另一个来源获得了这段代码,它使用tinkter在窗口上显示文本,我想做同样的事情

原始代码的来源:

这就是我要做的事的代码! `

`

现在输出!:

更新。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。 我正在尝试另一种方法。。。。。。我正在尝试实现一次性识别,但我不明白如何在我说话时更新标签中的文本

这是我的密码

import config
import azure.cognitiveservices.speech as speechsdk
import time
import wave

import sys

from PyQt5 import QtCore, QtWidgets
from PyQt5.QtWidgets import QApplication
from PyQt5.QtWidgets import QVBoxLayout
from PyQt5.QtWidgets import QPushButton
from PyQt5.QtWidgets import QWidget
from PyQt5.QtWidgets import QLabel
import math

from PyQt5.QtWidgets import *
from azure.cognitiveservices.speech import SpeechConfig

speech_key, service_region = "", ""  # speech key and region goes here
speech_config = speechsdk.SpeechConfig(subscription=speech_key, region=service_region)
speech_recognizer = speechsdk.SpeechRecognizer(speech_config=speech_config)


def speech_to_Print():
    

    # section of code for audio input
   
    result = speech_recognizer.recognize_once()
    sentence = result.text  # register the text from speech into sentence field
    print(sentence)
    return sentence


class QRecognizer(QtCore.QObject):
    textChanged = QtCore.pyqtSignal(str)

    def __init__(self, key, region, parent=None):
        super().__init__(parent)
        config = speechsdk.SpeechConfig(subscription=key, region=region)
        self._recognizer = speechsdk.SpeechRecognizer(speech_config=config)


def main():
    import sys

    app = QtWidgets.QApplication(sys.argv)

    speech_key, service_region = "", ""  # speech key and region goes here
    qrecognizer = QRecognizer(speech_key, service_region)

    w = QtWidgets.QWidget()
    while True:
        newSentence = speech_to_Print()
        label = QtWidgets.QLabel(newSentence)

        qrecognizer.textChanged.connect(label.setText)

        lay = QtWidgets.QVBoxLayout(w)
        lay.addWidget(label)
        w.show()
        sys.exit(app.exec_())


if __name__ == "__main__":
    main()

基本上,您需要创建一个新线程来执行语音到文本的转换,然后您将获得识别的文本作为信号

from PyQt5.QtCore import QRunnable, QThreadPool, QObject, pyqtSignal
import azure.cognitiveservices.speech as speechsdk
from playsound import playsound

API_KEY = ""
REGION = ""


class Stt(QRunnable):
    def __init__(self):
        super().__init__()
        self.signals = WorkerSignals()

    def run(self) -> None:
        speech_config = speechsdk.SpeechConfig(subscription=API_KEY, region=REGION)
        speech_recognizer = speechsdk.SpeechRecognizer(speech_config=speech_config)
        playsound('music/StartBeep.wav')
        result = speech_recognizer.recognize_once()
        playsound("music/EndBeep.wav")

        if result.reason == speechsdk.ResultReason.RecognizedSpeech:
            self.signals.finished.emit(result.text)
        elif result.reason == speechsdk.ResultReason.NoMatch:
            print("No speech could be recognized")
        elif result.reason == speechsdk.ResultReason.Canceled:
            cancellation_details = result.cancellation_details
            print("Speech Recognition canceled: {}".format(cancellation_details.reason))
            if cancellation_details.reason == speechsdk.CancellationReason.Error:
                print("Error details: {}".format(cancellation_details.error_details))

    def start(self):
        QThreadPool.globalInstance().start(self)




    def start(self):
        QThreadPool.globalInstance().start(self)


class WorkerSignals(QObject):
    finished = pyqtSignal(str)
开始识别

stt=Stt()
stt.signals.finished.connect(callback_function)
stt.start()
不要将同一个对象用于多次识别,而是通过将上述代码包装到函数中并调用该函数,每次都创建一个新的对象


我已经写了一篇关于如何在PyQt5应用程序中使用Azure Speech services的详细教程,请访问阅读该教程,您还可以获得完整的代码。

基本上,您需要创建一个新线程来执行Speech to text,然后将识别的文本作为信号

from PyQt5.QtCore import QRunnable, QThreadPool, QObject, pyqtSignal
import azure.cognitiveservices.speech as speechsdk
from playsound import playsound

API_KEY = ""
REGION = ""


class Stt(QRunnable):
    def __init__(self):
        super().__init__()
        self.signals = WorkerSignals()

    def run(self) -> None:
        speech_config = speechsdk.SpeechConfig(subscription=API_KEY, region=REGION)
        speech_recognizer = speechsdk.SpeechRecognizer(speech_config=speech_config)
        playsound('music/StartBeep.wav')
        result = speech_recognizer.recognize_once()
        playsound("music/EndBeep.wav")

        if result.reason == speechsdk.ResultReason.RecognizedSpeech:
            self.signals.finished.emit(result.text)
        elif result.reason == speechsdk.ResultReason.NoMatch:
            print("No speech could be recognized")
        elif result.reason == speechsdk.ResultReason.Canceled:
            cancellation_details = result.cancellation_details
            print("Speech Recognition canceled: {}".format(cancellation_details.reason))
            if cancellation_details.reason == speechsdk.CancellationReason.Error:
                print("Error details: {}".format(cancellation_details.error_details))

    def start(self):
        QThreadPool.globalInstance().start(self)




    def start(self):
        QThreadPool.globalInstance().start(self)


class WorkerSignals(QObject):
    finished = pyqtSignal(str)
开始识别

stt=Stt()
stt.signals.finished.connect(callback_function)
stt.start()
不要将同一个对象用于多次识别,而是通过将上述代码包装到函数中并调用该函数,每次都创建一个新的对象


我已经写了一篇关于如何在PyQt5应用程序中使用Azure Speech services的详细教程,请访问阅读该教程,您也可以获得完整的代码。

评论不用于扩展讨论;这段对话已经结束了。好的。。。。。我添加此评论,以便其他人知道我在哪里,如果可能,可以帮助。。。我尝试着用同样的方法,但是用一句话来代替文本,并使用while循环来不断地生成文本。。。我已经测试了这个方法,它在控制台中给了我输出。。。。我只是想让标签更新,但我不明白怎么做!这是文本显示方式的代码。。。。我只希望PyQt5窗口更新与控制台相同的文本。如果您需要任何澄清,请告诉我…评论不用于扩展讨论;这段对话已经结束了。好的。。。。。我添加此评论,以便其他人知道我在哪里,如果可能,可以帮助。。。我尝试着用同样的方法,但是用一句话来代替文本,并使用while循环来不断地生成文本。。。我已经测试了这个方法,它在控制台中给了我输出。。。。我只是想让标签更新,但我不明白怎么做!这是文本显示方式的代码。。。。我只希望PyQt5窗口更新与控制台相同的文本。如果您需要任何澄清,请告诉我。。。