如何解决TypeError: ‘type’对象不支持上下文管理器协议?

huangapple go评论66阅读模式
英文:

How to solve TypeError: 'type' object does not support context manager protocol?

问题

I was creating a voice assistant project but I am having a problem with the line with with command in it.

The code I've written is this

import speech_recognition as sr
import win32com.client

speaker = win32com.client.Dispatch("SAPI.SpVoice")

def say(text):
    speaker.Speak(f"{text}")

def takeCommand():
    r = sr.Recognizer()
    with sr.Microphone() as source:
        r.pause_threshold = 1
        audio = r.listen(source)
        query = r.recognize_google(audio, language="en-in")
        print(f"User said: {query}")
        return query

if __name__ == "__main__":
    print("VS Code")
    say("Hello, I am Jarvis A.I.")
    while 1:
        print("listening...")
        text = takeCommand()
        say(text)

And the error it always gets is this

VS Code
listening...
Traceback (most recent call last):
  File "f:\Jarvis AI\main.py", line 23, in <module>
    text = takeCommand()
           ^^^^^^^^^^^^^
  File "f:\Jarvis AI\main.py", line 11, in takeCommand
    with sr.Microphone() as source:
TypeError: 'type' object does not support the context manager protocol

I've installed packages in my system like pywin32, pyaudio, and speechrecognition, but now I don't know what to do and how to proceed.

英文:

I was creating a voice assistant project but I am having problem with the line with with command in it.

The code I've written is this

import speech_recognition as sr
import win32com.client

speaker = win32com.client.Dispatch(&quot;SAPI.SpVoice&quot;)

def say(text):
    speaker.Speak(f&quot;{text}&quot;)

def takeCommand():
    r = sr.Recognizer()
    with sr.Microphone as source:
        r.pause_threshold = 1
        audio = r.listen(source)
        query = r.recognize_google(audio, language=&quot;en-in&quot;)
        print(f&quot;User said: {query}&quot;)
        return query

if __name__ == &quot;__main__&quot;:
    print(&quot;VS Code&quot;)
    say(&quot;Hello I am Jarvis A.I.&quot;)
    while 1:
        print(&quot;listening...&quot;)
        text = takeCommand()
        say(text)

And the error it always gets is this

VS Code
listening...
Traceback (most recent call last):
  File &quot;f:\Jarvis AI\main.py&quot;, line 23, in &lt;module&gt;
    text = takeCommand()
           ^^^^^^^^^^^^^
  File &quot;f:\Jarvis AI\main.py&quot;, line 11, in takeCommand
    with sr.Microphone as source:
TypeError: &#39;type&#39; object does not support the context manager protocol

I've installed packages in my system like pywin32, pyaudio and speechrecognition but now I don't know what to do and how to proceed.

答案1

得分: 7

尝试将 with sr.Microphone as source: 更改为 with sr.Microphone() as source:

英文:

try changing with sr.Microphone as source: to with sr.Microphone() as source:

huangapple
  • 本文由 发表于 2023年5月17日 22:05:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/76273001.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定