pythonspeech-recognitiontext-to-speechattributeerrormicrophone

AttributeError: __enter__ while trying to take input from microphone


I had been trying to take input from microphone in my python program using speech_recognition

I try to run this code: -

import speech_recognition as sr
import pyaudio
r = sr.Recognizer()
with sr.Microphone as source:
    audio = r.listen(source)
    text = r.recognize_google(audio)
    print(text)

But it does not run.

It says: -

"C:\Users\Bravo Six\AppData\Local\Programs\Python\Python37-32\python.exe" "D:/BROTEEN/Works/Python/VALINI/speech testing.py"
Traceback (most recent call last):
  File "D:/BROTEEN/Works/Python/VALINI/speech testing.py", line 9, in <module>
    with sr.Microphone as source:
AttributeError: __enter__

Process finished with exit code 1

How can I solve this problem??? Please some help me get out of this situation!!!

I Ctrl + clicked on the sr.**Microphone**, wherein I saw that it said it throws an attribute error when pyaudio 0.2.11 or later is not installed. So I installed it using cmd, but the problem still sustains... HELP!!!



Solution

  • the same kind of problem happened to me, and I got it solved by coding it up like this:

    with sr.Microphone() as source:
       #your code here
    

    I hope this helps solve your problem. If you still are having some problems, let me know.