1

如果有人知道这里发生了什么,我将不胜感激。所以我试图识别通过麦克风输入的语音(设备索引为 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.

为什么会这样? 干杯! 感谢所有帮助:)

4

1 回答 1

1

问题是操作系统不允许您的 IDE 访问麦克风。如果您在操作系统本机“终端”中运行您的 python 代码,则会提示您是否允许终端访问麦克风,然后允许它,代码将起作用。

于 2020-03-28T15:30:30.137 回答