我通过导入speech_recognition运行语音到python 3上的文本代码,我的程序卡在“说些什么”并显示
sudo jack_control start //终端命令
--- start
sudo python speech.py //终端命令
终端输出:
ALSA lib pcm_dsnoop.c:606:(snd_pcm_dsnoop_open) unable to open slave
ALSA lib pcm_dmix.c:1029:(snd_pcm_dmix_open) unable to open slave
ALSA lib pcm.c:2266:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.rear
ALSA lib pcm.c:2266:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.center_lfe
ALSA lib pcm.c:2266:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.side
ALSA lib pcm_dmix.c:1029:(snd_pcm_dmix_open) unable to open slave
speak say anything
python3代码:
import speech_recognition as sr
r = sr.Recognizer()
with sr.Microphone() as source:
print('speak say anything')
audio = r.listen(source)
text = r.recognize_google(audio)
print("you said:{}".format(text))
无论是插孔还是代码,我都无法找到问题。
根据official documentation,listen()
方法等待直到音频能量超过一定水平(表明有人说话),并记录直到检测到静音。如果你的麦克风吸收过多的环境噪音,那么listen()
永远不会回来,因为它一直在等待沉默。
要解决这个问题,你可以使用r.adjust_for_ambient_noise(source)
:
with sr.Microphone() as source:
r.adjust_for_ambient_noise(source)
print('speak say anything')
audio = r.listen(source)
print("done listening")
您还可以在timeout
中指定phrase_time_limit
和listen()
参数,使其在一定秒数后停止并返回,即使未检测到语音或静音。
with sr.Microphone() as source:
r.adjust_for_ambient_noise(source)
print('speak say anything')
try:
# wait for speech for a maximum of 3 seconds
# listen to speech for a maximum of 3 seconds
audio = r.listen(source, timeout=3, phrase_time_limit=3)
except Exception as e:
# a timeouterror exception will be thrown if the timeout is reached
print(e)
print("done listening")
在初始化中,检查工作麦克风并明确设置device_index也是一个好主意:
for device_index in Microphone.list_working_microphones():
m = Microphone(device_index=device_index)
break
else:
print("No working microphones found!")