Python speech recognition error - Invalid number of channels

前端 未结 2 1216
不知归路
不知归路 2020-12-21 19:19

I\'m running a speech recognition code on python as part of a project. I\'m facing a really odd kind of a problem When I put the speech recognition code inside a function l

2条回答
  •  爱一瞬间的悲伤
    2020-12-21 19:51

    Try that way:

    r = sr.Recognizer()
    m = sr.Microphone(device_index=2)
    
    def loop():
        with m as source:
            print("say something")
            audio = r.listen(source)
            try:
                print("you said "+r.recognize_google(audio))
            except sr.UnknownValueError:
                print("Could not understand")
            except sr.RequestError as e:
                print("errpr: {0}".format(e))
    
    loop()
    

    Don't create multiple Microphone() instances.

提交回复
热议问题