第一次尝试时输入被忽略

时间:2020-09-16 09:44:19

标签: python speech-recognition

我在语音识别方面遇到此问题。当用户提供输入时,它会忽略它并再次询问。 这是代码

def getVoice():
    r = sr.Recognizer()
    with sr.Microphone() as source:
        print("Listening: ")
        r.pause_threshold = 1
        aud = r.listen(source)
    try:
        query = r.recognize_google(aud, language= "en-US")
        print(f"You said: {query}\n")

         
    except sr.UnknownValueError:
        print(f"Please {name}, try again.\n")
        query = None
    return query

1 个答案:

答案 0 :(得分:0)

尝试这个

import speech_recognition as sr

r = sr.Recognizer()

with sr.Microphone() as source:
    print("Listening: ")
    text = r.listen(source)
    try:
        recognised_text = r.recognize_google(text)
        print(recognised_text)
        
    except sr.UnknownValueError as uve:
        print(f"unknownvalueerror occurred: {uve}")
    except sr.RequestError as e:
        print(f"Requesterror occured: {e}")