Python 语音识别:仅在程序第一次运行时检测到麦克风一次

问题描述 投票:0回答:1

我正在使用语音识别库来检测语音。我正在使用streamlit作为界面。按钮触发语音识别过程。第一次运行正常。音频被识别并转录。

编辑:当我第二次按下按钮时,没有任何反应。未检测到麦克风

目前我不知道问题是出在streamlit还是出在speech_recognition部分。

这是执行 SR 的函数:

import speech_recognition as sr

r= sr.Recognizer()
def get_audio():
    with sr.Microphone() as source:
        r.pause_threshold=0.5
        audio=r.listen(source)
        said=""


    try:
        print("inside Try")
        said=r.recognize_google(audio, language="fr-FR")
        print(said)
    except sr.UnknownValueError:
        print("Exception: Google speech recogniton could not understand audio")
    except sr.RequestError as e:
        print(f"Could not request results from Google Speech Recognition service: {e}")
    return said

第二次甚至没有进入Try块

这是对 get_audio() 调用的摘录:

if st.button("Click-me to speak"):
        text= get_audio()
        query = st.text_input("Input your prompt here: ", text, key="transcription")
        if query:
            submit = True
speech-recognition streamlit
1个回答
0
投票

这可能是由于内置的 Streamlit Caching 机制所致。由于 r 对象(识别器)是在函数外部创建的,因此它可能无法在函数调用之间正确重置。因此,让我们尝试在每次调用 get_audio() 函数时在 get_audio() 函数中创建一个新的 Recognizer 对象,以确保如下所示的新状态:

import speech_recognition as sr
import streamlit as st

def get_audio():
    r = sr.Recognizer()  # Create a new Recognizer object each time
    with sr.Microphone() as source:
        r.pause_threshold = 0.5
        audio = r.listen(source)
        said = ""
    try:
        print("inside Try")
        said = r.recognize_google(audio, language="fr-FR")
        print(said)
    except sr.UnknownValueError:
        print("Exception: Google speech recognition could not understand audio")
    except sr.RequestError as e:
        print(f"Could not request results from Google Speech Recognition service: {e}")
    return said

if st.button("Click-me to speak"):
    text = get_audio()
    query = st.text_input("Input your prompt here: ", text, key="transcription")
    if query:
        submit = True
© www.soinside.com 2019 - 2024. All rights reserved.