对文本代码的语音停留在“说些什么”上:

时间:2019-04-04 08:12:44

标签: python python-3.x speech-recognition pyaudio

我正在通过导入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))

无论是杰克还是代码,我都找不到问题。

1 个答案:

答案 0 :(得分:1)

根据official documentationlisten()方法将一直等待,直到音频能量超过一定水平(表明有人在讲话)为止,然后进行记录,直到检测到无声为止。如果您的麦克风拾取到过多的环境噪声,则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_limitlisten()参数,以使其在一定的秒数后停止并返回,即使未检测到语音或静音。

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!")