简体   繁体   中英

Speech_Recognition with python on computer speakers - OSError: [Errno -9998] Invalid number of channels

I'm trying to use the Speech_Recognition module in python (I am using python 3.7.0) to detect the speech coming out from my computer speakers (eg to detect what somebody is saying in a skype call)

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

But on every one of the speakers option I get this error:

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

I also tried to enter the speaker's indexes one by one to not create multiple Microphone() instances, but it didn't help.

Thanks in advance

Instead of this, try using just a microphone as the input that the mic takes from the speakers. It simplifies it and is straight forward. Sort of like this program right here...

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM