在Raspberry pi上使用python speech_recognition进行语音识别:无效的通道数

时间:2019-04-11 11:13:08

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

我一直在pyaudio的帮助下尝试通过树莓派使用声卡,但是我收到一个错误消息,指出无效的通道号。我无法弄清楚如何解决此错误。

    import speech_recognition as sr

    # Record Audio
    r = sr.Recognizer()
    mic = sr.Microphone(device_index=2)
    with mic as source:
        print("Say something!")
        audio = r.listen(source)

    print("processing")
    # Speech recognition using Google Speech Recognition
    try:
    # for testing purposes, we're just using the default API key
    # to use another API key, use `r.recognize_google(audio, key="GOOGLE_SPEECH_RECOGNITION_API_KEY")`
    # instead of `r.recognize_google(audio)`
        print("You said: " + r.recognize_google(audio))
    except sr.UnknownValueError:
        print("Google Speech Recognition could not understand audio")
    except sr.RequestError as e:
        print("Could not request results from Google Speech Recognition service; {0}".format(e))

我得到的错误是-

Traceback (most recent call last):
  File "test1.py", line 7, in <module>
    with mic as source:
  File "/home/pi/.local/lib/python3.5/site-packages/speech_recognition/__init__.py", line 141, in __enter__
    input=True,  # stream is an input stream
  File "/usr/local/lib/python3.5/dist-packages/pyaudio.py", line 750, in open
    stream = Stream(self, *args, **kwargs)
  File "/usr/local/lib/python3.5/dist-packages/pyaudio.py", line 441, in __init__
    self._stream = pa.open(**arguments)

我尝试打印设备列表以选择正确的设备。 这是检测到的设备列表-

  0 bcm2835 ALSA: - (hw:0,0), ALSA (0 in, 2 out)
  1 bcm2835 ALSA: IEC958/HDMI (hw:0,1), ALSA (0 in, 2 out)
  2 USB PnP Sound Device: Audio (hw:1,0), ALSA (0 in, 2 out)
  3 sysdefault, ALSA (0 in, 128 out)
  4 dmix, ALSA (0 in, 2 out)
< 5 default, ALSA (0 in, 2 out)

0 个答案:

没有答案