繁体   English   中英

Python - 语音识别:属性错误:__enter__

[英]Python - Speech Recognition: Attribute error : __enter__

我正在尝试使用语音识别和 pyttsx3 模块在 python 中编写 jarvis 代码。 由于出现Attribute error:__enter__我不明白为什么会这样显示的错误,我被卡住了。 我是 python 的新手。 目前我正在使用 python 3.8。

def takeCommmand():
    r = sr.Recognizer()
    with sr.Microphone as source:
        print("listening...")
        r.pause_threshold = 1  
        audio = r.listen(source)
        return

错误:

File "c:/Users/Dell/Desktop/jarvis voice assistant/jarvis.py", line 28, in takeCommmand
with sr.Microphone as source:
AttributeError: __enter__

请告诉我代码有什么问题。

尝试这个:

def takeCommmand():
    r = sr.Recognizer()
    with sr.Microphone() as source: # <--- look here
        print("listening...")
        r.pause_threshold = 1  
        audio = r.listen(source)
        return

您需要添加()才能使其工作。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM