简体   繁体   English

Python SpeechRecognition - OSError: [Errno -9998] 通道数无效

[英]Python SpeechRecognition - OSError: [Errno -9998] Invalid number of channels

I've been programming for several years but am quite new to Python, I'm implenting some speech recognition on a Raspberry Pi using https://pypi.org/project/SpeechRecognition/ and streaming the microphone output direction to Wit.ai.我已经编程好几年了,但对 Python 还很陌生,我正在使用https://pypi.org/project/SpeechRecognition/在 Raspberry Pi 上实现一些语音识别,并将麦克风 output 方向传输到 Wit.ai。

I've been using PocketSphinx to constantly listen for the "wake word" and on my ubuntu VM the code below is working fine我一直在使用 PocketSphinx 不断监听“唤醒词”,在我的 ubuntu VM 上,下面的代码运行良好

class Recognition:
    def detection(self):
        speech = LiveSpeech(lm=False, keyphrase='alexa', kws_threshold=1e-20)

        print('Listening for wake word.');

        for phrase in speech:
            return self.start()

    def start(self):
        recognizer = sr.Recognizer()

        print('Heard wake word, listening and streaming audio.')

        with sr.Microphone(device_index=2) as source:
            audio = recognizer.listen(source)
        try:
            content = recognizer.recognize_wit(audio, key=WIT_AI_KEY)

            print(content)
        except sr.UnknownValueError:
            print("Unable to understand audio")
        except sr.RequestError as e:
            print("Error from Wit.ai: {}".format(e))


Recognition().detection()

But when running it on my Raspberry Pi, once the "wake word" has been triggered I'm getting the following error from PyAudio: OSError: [Errno -9998] Invalid number of channels但是在我的 Raspberry Pi 上运行它时,一旦触发了“唤醒词”,我就会从 PyAudio 收到以下错误: OSError: [Errno -9998] Invalid number of channels

If I run Recognition.start() directly it seems to be working absolutely fine as long as I remove the device_index from the Microphone() ?如果我直接运行Recognition.start() ,只要我从Microphone()中删除device_index ,它似乎就可以正常工作?

Does anyone know what the cause of this might be?有谁知道这可能是什么原因? As if I remove device_index and still run Recognition().detection() from the start I get a IOError: No Default Input Device Available就好像我删除了device_index并仍然从一开始就运行Recognition().detection()我得到一个IOError: No Default Input Device Available

Just to note, when recording via arecord on my Raspberry Pi, the audio input/microphone seems to be recording absolutely fine.请注意,在我的 Raspberry Pi 上通过arecord录制时,音频输入/麦克风似乎录制得非常好。

For anyone that comes across this in the future, it seems to be an issue with using LiveSpeech and SpeechRecognition python modules and them having their own PyAudio instances.对于将来遇到此问题的任何人来说,使用LiveSpeechSpeechRecognition python 模块以及它们拥有自己的PyAudio实例似乎是一个问题。

I've had to change the code to initialise my own PyAudio instance and then use the Decoder sent with PocketSphinx for the wake word and then directly continue the same stream directly to requests for Wit.ai.我不得不更改代码以初始化我自己的PyAudio实例,然后使用与PocketSphinx一起发送的Decoder作为唤醒词,然后直接继续相同的stream直接requests Wit.ai。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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