Azure语音服务start_continuous_recognition()无法让streamlit st.write()识别文本
我正在尝试使用Azure语音服务构建一个Streamlit应用程序。我想在用户界面上显示实时识别内容。
如果我使用下面提到的recognize_once()逻辑,我可以在应用程序的界面上看到输出;但是当我尝试使用start_continuous_recognition()时,似乎st.write()的逻辑被跳过了。
一次性识别逻辑:
def speech_recognize_once_from_mic():
# Set up the speech config and audio config
speech_config = speechsdk.SpeechConfig(subscription=subscription_key, region=service_region)
audio_config = speechsdk.AudioConfig(use_default_microphone=True)
# Create a speech recognizer with the given settings
speech_recognizer = speechsdk.SpeechRecognizer(speech_config=speech_config, audio_config=audio_config)
st.write("Speak into your microphone.")
result = speech_recognizer.recognize_once_async().get()
# Check the result
if result.reason == speechsdk.ResultReason.RecognizedSpeech:
return f"Recognized: {result.text}"
elif result.reason == speechsdk.ResultReason.NoMatch:
return "No speech could be recognized"
elif result.reason == speechsdk.ResultReason.Canceled:
cancellation_details = result.cancellation_details
return f"Speech Recognition canceled: {cancellation_details.reason}"
else:
return "Unknown error"
# Simple UI for processing the audio input
st.title("Azure Speech Service with Streamlit")
if st.button('Start speech recognition'):
recognition_result = speech_recognize_once_from_mic()
st.write(recognition_result)
连续识别逻辑:
def recognized_callback(evt):
# Callback function that appends recognized speech to chunks
global chunks
if evt.result.reason == speechsdk.ResultReason.RecognizedSpeech:
chunks.append(evt.result.text)
print(f"Recognized: {evt.result.text}")
st.write(chunks)
print('Done')
def process_audio():
# Set up the recognizer
recognizer = speechsdk.SpeechRecognizer(speech_config=speech_config, audio_config=audio_config)
recognizer.recognized.connect(recognized_callback)
recognizer.session_stopped.connect(session_stopped_callback)
# Store the recognizer in the session state to access it later
st.session_state.recognizer = recognizer
# Start continuous recognition
recognizer.start_continuous_recognition()
我该如何解决这个问题呢?
提前谢谢你!
1 个回答
0
我在一个使用Azure语音服务的Streamlit应用中尝试了以下代码,并在浏览器界面上收到了实时识别的文本。
代码:
import streamlit as st
import azure.cognitiveservices.speech as speechsdk
chunks = []
subscription_key = "<speech_key>"
service_region = "<speech_region>"
speech_config = speechsdk.SpeechConfig(subscription=subscription_key, region=service_region)
audio_config = speechsdk.AudioConfig(use_default_microphone=True)
def recognized_callback(evt):
global chunks
if evt.result.reason == speechsdk.ResultReason.RecognizedSpeech:
chunks.append(evt.result.text)
print(f"Recognized: {evt.result.text}")
def main():
st.title("Azure Speech Service with Streamlit")
recognizer = speechsdk.SpeechRecognizer(speech_config=speech_config, audio_config=audio_config)
recognizer.recognized.connect(recognized_callback)
recognizer.start_continuous_recognition()
st.write("Speak into your microphone.")
while True:
if chunks:
st.write(chunks)
chunks.clear()
st.write("Done")
if __name__ == "__main__":
main()
浏览器输出:
我在Streamlit中得到了以下输出:
我说了以下句子,它成功地将语音转换成了文本,如下所示:
输出:
程序成功运行,我在VS Code终端中收到了识别的文本输出,如下所示。
C:\Users\xxxxxxxxx\Documents\xxxxxxxx>streamlit run app.py
You can now view your Streamlit app in your browser.
Local URL: http://localhost:8501
Network URL: http://192.168.0.126:8501
Recognized: Hi, Kamali.
Recognized: Welcome.
Recognized: Welcome to my world, Kamali.
Recognized: How are you?
Recognized: Very good to see you.
Recognized: I am very happy for you.
Recognized: Let's continue the recognition.
Recognized: Hi, Priya.
Recognized: Welcome.
Recognized: I am very happy to see you.