AttributeError:尝试从麦克风获取输入时__enter__

时间:2019-11-13 17:07:58

标签: python speech-recognition text-to-speech attributeerror microphone

我一直在尝试使用Speech_recognition从我的python程序中的麦克风获取输入

我尝试运行以下代码:-

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)

但是它没有运行。

它说:-

"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__

以退出代码1完成的过程

我该如何解决这个问题???请一些帮助我摆脱这种情况!!!

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!!!


2 个答案:

答案 0 :(得分:0)

我遇到了同样的问题,我通过像这样编码来解决了这个问题:

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

我希望这有助于解决您的问题。如果您仍然遇到问题,请告诉我。

答案 1 :(得分:0)

我实际上认为Windows 7不能轻易与speech_recognition模块配合使用...所以我只是切换到Windows 10,现在它像黄油般平滑地工作!谢谢