Python语音识别,Pyaudio不是在听,而是在给出这个



我正在尝试使用python中的语音识别和pyaudio将语音转换为文本。它在转换预先录制的音频文件时工作良好,但当我使用麦克风(外部(录制时,它没有在听,而是显示以下内容:

ALSA lib pcm.c:2495:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.rear
ALSA lib pcm.c:2495:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.center_lfe
ALSA lib pcm.c:2495:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.side
ALSA lib pcm_route.c:867:(find_matching_chmap) Found no matching channel map
ALSA lib pcm_route.c:867:(find_matching_chmap) Found no matching channel map
ALSA lib pcm_route.c:867:(find_matching_chmap) Found no matching channel map
ALSA lib pcm_route.c:867:(find_matching_chmap) Found no matching channel map

这是我的代码:

import speech_recognition as sr
r = sr.Recognizer()
mic = sr.Microphone()
with mic as source:
print('say somthing')
audio = r.listen(source)

print(r.recognize_google(audio))

试试这个代码块。这在我的系统中运行得非常好,即使在实时输入中也是如此。

import speech_recognition as sr
def myCommand():
r = sr.Recognizer()
with sr.Microphone() as source:
audio = r.listen(source, phrase_time_limit = 5)  
try:
command = r.recognize_google(audio).lower()
print("you said: " + command)

except sr.UnknownValueError:
print("Sorry, Cant understand, Please say again")
command = myCommand()
return command

最新更新