0

我正在使用 google 的 API 在 python 中制作语音识别器项目。代码如下:

from gtts import gTTS
import speech_recognition as sr

def get_audio():

    r = sr.Recognizer()

    with sr.Microphone as source:

        l = r.listen(source)

        said = ""

        try:

            said = recognize_google(l)

            print(said)

        except Exception as e:

            print("Exception: " + str(e))

        return said
get_audio()

错误是:

<ipython-input-5-73a42a1f9208> in get_audio()
      1 def get_audio():
      2     r = sr.Recognizer()
----> 3     with sr.Microphone as source:
      4         l = r.listen(source)
      5         said = ""

AttributeError: __enter__

有人可以解释为什么吗?一个人有相同的代码,并且在他的电脑上运行。

4

1 回答 1

0

利用

with sr.Microphone() as source:
   ...

注意括号:这是一个函数调用。

于 2020-01-20T17:11:40.263 回答