如果有人知道这里发生了什么,我将不胜感激。所以我正在尝试识别通过麦克风输入的语音(设备索引为 1(,我的程序在收听上挂断了。我针对环境噪音进行了调整,但我仍然没有任何进展。
这是我的代码:
import pyaudio
import speech_recognition as sr
r = sr.Recognizer()
mic = sr.Microphone(1)
with mic as source:
r.adjust_for_ambient_noise(source)
print("Please Speak :")
audio = r.listen(source)
print("Stop Talking")
try:
text = r.recognize_google(audio)
print("You said : " + r. recognize_google(text))
except:
print("Sorry, I could not recognize what you said")
这就是程序运行时我得到的全部内容:
Please Speak :
然后没有别的了。我在 python 控制台中运行了这个,但从未返回控制台提示。
r = sr.Recognizer()
with mic as source:
r.adjust_for_ambient_noise(source)
print("Please Speak :")
audio = r.listen(source)
Please Speak :
我不确定我能在这里做什么或出了什么问题。 我正在运行:
Mac OS Mojave 10.14.6
Python 3.8
Pycharm IDLE
现在我做了一些更改:
import speech_recognition as sr
r = sr.Recognizer()
print(sr.Microphone.list_microphone_names())
mic = sr.Microphone(device_index=1)
with mic as source:
r.adjust_for_ambient_noise(source, duration=5)
print("Please Speak :")
audio = r.listen(source, timeout=5)
print("Stop Talking")
try:
text = r.recognize_google(audio)
print("You said : " + text)
except:
print("Sorry, I could not recognize what you said.")
这是我的错误:
eTraceback (most recent call last):
File "/Users/cameronclarke/PycharmProjects/SpeechRecog/Speech
Recog.py", line 13, in <module>
audio = r.listen(source, timeout=5)
File
Users/cameronclarke/opt/anaconda3/envs/SpeechRecog/lib/python3.7/site-
packages/speech_recognition/__init__.py", line 544, in listen
raise WaitTimeoutError("listening timed out while waiting for phrase
to start")
speech_recognition.WaitTimeoutError: listening timed out while waiting
for phrase to start.
为什么会这样?干杯!感谢所有帮助:)
问题是操作系统不允许IDE访问麦克风。 如果您在操作系统本机"终端"中运行python代码,则会提示询问您是否允许终端访问麦克风,然后允许它,代码将起作用。