【问题标题】:Why am I getting raise UnknownValueError() speech_recognition.UnknownValueError?为什么我会收到 raise UnknownValueError() speech_recognition.UnknownValueError?
【发布时间】:2022-01-10 08:02:49
【问题描述】:

我正在尝试编写一个脚本,该脚本使用 SpeechRecognition 来运行一个函数,一旦说出一个唤醒词“运行”就可以正常运行,直到我进行正则表达式搜索以在我的 alpha 变量中找到唤醒词这是分配给我所说的。运行时出现错误。我已经用一个字符串在 SpeechRecognition 之外测试了我的正则表达式算法,它运行良好。

错误:

raise UnknownValueError()
speech_recognition.UnknownValueError

我能得到一些帮助吗?

代码:

import speech_recognition as sr
import re

def stt():
    recognizer = sr.Recognizer()
    microphone = sr.Microphone()

    # check that recognizer and microphone arguments are appropriate type
    if not isinstance(recognizer, sr.Recognizer):
        raise TypeError("`recognizer` must be `Recognizer` instance")

    if not isinstance(microphone, sr.Microphone):
        raise TypeError("`microphone` must be `Microphone` instance")

    # listen and assign
    with microphone as source:
        recognizer.adjust_for_ambient_noise(source)
        audio = recognizer.listen(source)
    # return output
    return recognizer.recognize_google(audio)


while True:
    alpha = stt()
    print(alpha)
    if re.search('.+Run.+', alpha):
        print("1")
        beta = alpha.split()
        query = beta.pop()
        print("working")

【问题讨论】:

    标签: python regex speech-recognition


    【解决方案1】:

    尝试将您的整个代码置于 try 和 except 之下。

    像这样:

    import speech_recognition as sr
    import re
    
    try:
    
        def stt():
            recognizer = sr.Recognizer()
            microphone = sr.Microphone()
    
            # check that recognizer and microphone arguments are appropriate type
            if not isinstance(recognizer, sr.Recognizer):
                raise TypeError("`recognizer` must be `Recognizer` instance")
    
            if not isinstance(microphone, sr.Microphone):
                raise TypeError("`microphone` must be `Microphone` instance")
    
            # listen and assign
            with microphone as source:
                recognizer.adjust_for_ambient_noise(source)
                audio = recognizer.listen(source)
            # return output
            return recognizer.recognize_google(audio)
    
    
        while True:
            alpha = stt()
            print(alpha)
            if re.search('.+Run.+', alpha):
                print("1")
                beta = alpha.split()
                query = beta.pop()
                print("working")
        
    except sr.UnknownValueError() as e:
        pass
    

    如果这不起作用。看看这个github issue

    【讨论】:

      猜你喜欢
      • 2023-04-05
      • 2016-08-21
      • 2014-07-04
      • 2020-06-06
      • 2018-12-26
      • 2021-03-05
      • 2013-07-13
      • 2019-05-28
      相关资源
      最近更新 更多