Python语音识别ubuntu ALSA库



我正在尝试使用ubuntu 21.10 上的SpeechRecognition将语音转换为文本


这是代码
import speech_recognition as sr
def listen():
r = sr.Recognizer()
mic = sr.Microphone(device_index=1)
with mic as source:
audio = r.listen(source, timeout=10)
return r.recognize_google(audio)
print(listen())

这是抛出错误

ALSA lib pcm.c:2660:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.rear
ALSA lib pcm.c:2660:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.center_lfe
ALSA lib pcm.c:2660:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.side
ALSA lib pcm_route.c:877:(find_matching_chmap) Found no matching channel map
ALSA lib pcm_route.c:877:(find_matching_chmap) Found no matching channel map
ALSA lib pcm_route.c:877:(find_matching_chmap) Found no matching channel map
ALSA lib pcm_route.c:877:(find_matching_chmap) Found no matching channel map
ALSA lib pcm_oss.c:377:(_snd_pcm_oss_open) Unknown field port
ALSA lib pcm_oss.c:377:(_snd_pcm_oss_open) Unknown field port
ALSA lib pcm_usb_stream.c:486:(_snd_pcm_usb_stream_open) Invalid type for card
ALSA lib pcm_usb_stream.c:486:(_snd_pcm_usb_stream_open) Invalid type for card
Expression 'parameters->channelCount <= maxChans' failed in 'src/hostapi/alsa/pa_linux_alsa.c', line: 1514
Expression 'ValidateParameters( inputParameters, hostApi, StreamDirection_In )' failed in 'src/hostapi/alsa/pa_linux_alsa.c', line: 2818
Traceback (most recent call last):
File "/home/harsha/PycharmProjects/playground/audio.py", line 29, in <module>
assitant.listen()
File "/home/harsha/PycharmProjects/playground/audio.py", line 23, in listen
with mic as source:
File "/home/harsha/PycharmProjects/playground/sand/lib/python3.9/site-packages/speech_recognition/__init__.py", line 138, in __enter__
self.audio.open(
File "/home/harsha/PycharmProjects/playground/sand/lib/python3.9/site-packages/pyaudio.py", line 750, in open
stream = Stream(self, *args, **kwargs)
File "/home/harsha/PycharmProjects/playground/sand/lib/python3.9/site-packages/pyaudio.py", line 441, in __init__
self._stream = pa.open(**arguments)
OSError: [Errno -9998] Invalid number of channels

我在内置麦克风的笔记本电脑上做这件事,我在堆栈溢出上尝试了所有可能的答案,请不要认为这是重复的。

这可能是因为您的设备索引指向了错误的设备。

这是我的建议:

检查设备列表:

import speech_recognition as sr
for mic in sr.Microphone.list_microphone_names():
print(mic)

你会有一个这样的日志:

HDA Intel PCH: ALC257 Analog (hw:0,0) // my mic with index 0
HDA Intel PCH: HDMI 0 (hw:0,3)
HDA Intel PCH: HDMI 1 (hw:0,7)
HDA Intel PCH: HDMI 2 (hw:0,8)
HDA Intel PCH: HDMI 3 (hw:0,9)
HDA Intel PCH: HDMI 4 (hw:0,10)
HDA NVidia: HDMI 0 (hw:1,3)
HDA NVidia: HDMI 1 (hw:1,7)
HDA NVidia: HDMI 2 (hw:1,8)
HDA NVidia: HDMI 3 (hw:1,9)

但您可以通过不设置设备索引来使用默认麦克风

import speech_recognition as sr
def listen():
r = sr.Recognizer()
mic = sr.Microphone() // default device index
with mic as source:
// r.adjust_for_ambient_noise(source) case you ever have noise issues
audio = r.listen(source, timeout=10)
return r.recognize_google(audio)
print(listen())

最新更新