问题描述
我正在尝试使用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