在计算机音箱上使用python进行语音识别 - OSError: [Errno -9998] 无效的通道数。

问题描述 投票:1回答:1

我正试图使用 语音识别 在python中的模块(我使用python 3.7.0)来检测我的电脑扬声器发出的语音(例如检测Skype电话中有人在说什么)

import speech_recognition as sr


def get_speakers_index(list_microphone_names):
    list_index = []
    for i in range(len(list_microphone_names)):
        if "speakers" in list_microphone_names[i].lower():
            list_index.append(i)
    return list_index


def main():
    r = sr.Recognizer()
    list_speakers_index = get_speakers_index(sr.Microphone.list_microphone_names())
    for speakers_index in list_speakers_index:

        mic = sr.Microphone(device_index=speakers_index)

        with mic as source:
            print("listening")
            audio = r.listen(source)
            text = ""

            try:
                text = r.recognize_google(audio)
            except Exception as e:
                print("Exception " + str(e))


if __name__ == '__main__':
    main()

但在每一个扬声器选项上我都得到这个错误。

Traceback (most recent call last):
  File "C:/Users/User/Desktop/Project/mic.py", line 35, in <module>
    main()
  File "C:/Users/User/Desktop/Project/mic.py", line 21, in main
    with mic as source:
  File "C:\Python37\lib\site-packages\speech_recognition\__init__.py", line 141, in __enter__
    input=True,  # stream is an input stream
  File "C:\Python37\lib\site-packages\pyaudio.py", line 750, in open
    stream = Stream(self, *args, **kwargs)
  File "C:\Python37\lib\site-packages\pyaudio.py", line 441, in __init__
    self._stream = pa.open(**arguments)
OSError: [Errno -9998] Invalid number of channels

我还试着一个一个地输入扬声器的索引,以避免创建多个。Microphone() 实例,但没有任何帮助。

先谢谢你

python speech-recognition python-3.7 pyaudio oserror
1个回答
0
投票

与此相反,可以尝试只使用一个麦克风作为输入,麦克风从扬声器中接收。它简化了它,而且是直接的。有点像这里的这个程序... ...

def takeCommand():
    r = sr.Recognizer()

    with sr.Microphone() as source:

        print("Listening...")
        r.pause_threshold = 1
        audio = r.listen(source)

    try:
        print("Recognizing...")
        query = r.recognize_google(audio, language='en-us')
        print("User said: {query}\n")

    except Exception as e:
        print(e)
        print("I can't hear you sir.")
        return "None"

    return query
© www.soinside.com 2019 - 2024. All rights reserved.