Python speech_recognition 如果无法识别语音

Python speech_recognition IF Speech not recognised

我是一名葡萄牙高中生,如果我没有说清楚,我很抱歉,但我会尽力解释我的问题。 因此,对于这个项目,我需要制作一个程序来识别语音并按照要求去做。但是如果无法识别语音,我希望程序尝试直到它最终捕捉到正在说的内容。但是我真的不知道该怎么做,因为这个“错误”不断出现:Error

如果有人想帮忙的话,这里是代码:

import speech_recognition as sr
import subprocess as sp
import os

def ouvir_microfone():

   audio_Input = sr.Recognizer()

   with sr.Microphone() as source:
           
               Ray = "ON"
               Open = "Open"

               print("Hello my name is Ray pleased to help you")

               audio_Input.adjust_for_ambient_noise(source)
               print("Say something: ")
               audio = audio_Input.listen(source)
               
               frase = audio_Input.recognize_google(audio,language='en-US')

               if ValueError: print("Speech not recognised")
               else: print("You said: " + frase)

               if Open in frase:
                   print('sucess') 
ouvir_microfone()
import speech_recognition as sr
import subprocess as sp
import os

def ouvir_microfone():

    audio_Input = sr.Recognizer()

    with sr.Microphone() as source:
        
        Ray = "ON"
        Open = "Open"

        print("Hello my name is Ray pleased to help you")

        audio_Input.adjust_for_ambient_noise(source)
        print("Say something: ")
        audio = audio_Input.listen(source)
        try:
            # valid
            frase = audio_Input.recognize_google(audio,language='en-US')
            print("You said: " + frase)
        except: 
            # google could not validate
            print("Speech not recognised")
            return False

        if Open in frase:
            print('sucess') 
        return True
while True:
    if ouvir_microfone():
        # got a valid response
        break

作为实体模板使用。一些导入也未用作旁注。如果无法识别语音,则捕获异常和 return False,因此它会重试。 google 语音识别 python 库在无法读取可区分语音的情况下抛出异常。

使用try和exception来处理错误

   try:
        frase = audio_Input.recognize_google(audio,language='en-US')
        print("You said: " + frase)
    except ValueError: 
        print("Speech not recognised")
    if Open in frase:
        print('sucess')