【问题标题】:AttributeError: __Enter__ - How to use?AttributeError: __Enter__ - 如何使用?
【发布时间】:2019-12-22 00:21:57
【问题描述】:

我正在尝试用Python制作一个简单的语音识别工具,并尝试了以下代码:

import speech_recognition as sr

r = sr.Recognizer()

with sr.Microphone as source:
print ("Speak into the microphone")
audio = r.listen(source) 

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-4-10651df1693e> in <module>
----> 1 with sr.Microphone as source:
      2     print ("Speak into the microphone")
      3     audio = r.listen(source)

AttributeError: __enter__ 

我希望有人能够说明在这种情况下如何使用__enter__ 属性?

非常感谢!

【问题讨论】:

  • 这意味着sr.Microphone 不支持上下文管理器。我不知道图书馆,所以你必须自己调查一下

标签: python python-3.x attributes speech-recognition microphone


【解决方案1】:

这是使用谷歌识别器的代码,它会不断地监听直到你终止:

import speech_recognition as sr

def speech_recog():
    r = sr.Recognizer()

    mic = sr.Microphone()
    while True:
        with mic as source:
            print("Speak...")

            audio = r.listen(source)

            try:
                text = r.recognize_google(audio)
                print(f"You said {text}")

            except sr.UnknownValueError:
                print("Didnt hear that try again")
speech_recog()

如果您只想监听一个实例,请移除:

while True:

Here is the link to the speech recignition documentation

在那里你可以选择要阅读更多关于哪个实现,这个包的文档非常好。

【讨论】:

    猜你喜欢
    • 2018-10-02
    • 1970-01-01
    • 2022-07-27
    • 2019-05-03
    • 2022-08-16
    • 2017-09-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多