1

我正在尝试使用 python 中的Speech_Recognition模块(我正在使用 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()实例,但这没有帮助。

提前致谢

4

1 回答 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
于 2020-05-23T04:21:32.780 回答