【问题标题】:Python speech recognition ubuntu ALSA libPython语音识别ubuntu ALSA lib
【发布时间】:2022-01-25 10:15:22
【问题描述】:

我正在尝试在 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

我在带有内置麦克风的笔记本电脑上执行此操作,我在堆栈溢出时尝试了所有可能的答案,请不要认为这是重复的。

【问题讨论】:

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


    【解决方案1】:

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

    这是我的建议:

    查看设备列表:

    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())
    

    【讨论】:

    • 我尝试检查设备列表并得到了这个先生ALSA lib pcm_route.c:877:(find_matching_chmap) Found no matching channel map
    • 如果您只收到ALSA lib 而没有任何错误消息,则意味着您对其进行了编码工作。那些是 ALSA 的唯一输出。尝试通过不设置 device_index 来使用默认麦克风。如果您在使用ALSA 时遇到问题,请查看ALSA FILE: nano /usr/share/alsa/alsa.conf。我建议您在r.listen(source) 之前使用print(say something)。如果您的代码确实有错误,请告诉我
    • 错误以ASLA lib pcm_route.c:877: 开头,程序立即崩溃,它没有监听,最终错误为OSError: [Errno -9998] Invalid number of channels
    • 你好,雷霆。您为 device_index 使用哪个值?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-03-12
    • 2021-07-15
    • 2021-07-04
    • 2021-11-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多