我正在尝试使用Python创建一个简单的语音识别工具,并尝试了以下代码:
import speech_recognition as sr
r = sr.Recognizer()
with sr.Microphone as source:
print ("Speak into the microphone")
audio = r.listen(source)
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-4-10651df1693e> in <module>
----> 1 with sr.Microphone as source:
2 print ("Speak into the microphone")
3 audio = r.listen(source)
AttributeError: __enter__
我希望有人可以在这种情况下阐明如何使用__enter__
属性?
非常感谢!
答案 0 :(得分:-1)
以下是使用Google识别器的代码,该代码会一直监听直到您终止:
import speech_recognition as sr
def speech_recog():
r = sr.Recognizer()
mic = sr.Microphone()
while True:
with mic as source:
print("Speak...")
audio = r.listen(source)
try:
text = r.recognize_google(audio)
print(f"You said {text}")
except sr.UnknownValueError:
print("Didnt hear that try again")
speech_recog()
如果只想听一个实例,请删除:
while True:
Here is the link to the speech recignition documentation
您可以在其中选择要阅读的实施更多信息,该文档对该软件包非常有用。