在 Python 中无法将语音转为文本

Having trouble with speech to text in Python

import speech_recognition as sr

rec = sr.Recognizer()
with sr.Microphone as source:
    print('say something')
    audio = rec.listen(source)
    voice_data = rec.recognize_google(audio)
    print(voice_data)

这是我的代码,我在网上看了很多教程,我安装了 SpeechRecognizer 和 pyAudio(使用 whl 文件,而不是 pip,我不知道它是否有所不同)。

当我尝试 运行 时,它给了我关于 pydevd.py 中某些内容的错误,最后给了我:

with sr.Microphone as source:

AttributeError: enter

我该如何解决这个问题?

我通过将与捕获音频无关的语句从 with 语句中移出来让它工作,这应该适合你:

import speech_recognition as sr

rec = sr.Recognizer()

print('say something')
with sr.Microphone() as source:
    audio = rec.listen(source)
    voice_data = rec.recognize_google(audio)

print(voice_data)