Python3 OSError:[Errno -9988] Stream已关闭

时间:2018-06-09 09:01:07

标签: python python-3.x speech-recognition raspberry-pi3 voice-recognition

我正在使用 Raspberry pi 3B 处理语音识别系统,在运行某些代码时,我遇到了一些问题,如下所示。我从互联网上尝试了这么多东西但是找不到一些具体问题所以请帮我解决下面的错误!

ALSA lib pcm.c:2495:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.rear
ALSA lib pcm.c:2495:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.center_lfe
ALSA lib pcm.c:2495:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.side
ALSA lib pcm.c:2495:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.hdmi
ALSA lib pcm.c:2495:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.hdmi
ALSA lib pcm.c:2495:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.modem
ALSA lib pcm.c:2495:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.modem
ALSA lib pcm.c:2495:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.phoneline
ALSA lib pcm.c:2495:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.phoneline
ALSA lib confmisc.c:1281:(snd_func_refer) Unable to find definition 'defaults.bluealsa.device'
ALSA lib conf.c:4528:(_snd_config_evaluate) function snd_func_refer returned error: No such file or directory
ALSA lib conf.c:4996:(snd_config_expand) Args evaluate error: No such file or directory
ALSA lib pcm.c:2495:(snd_pcm_open_noupdate) Unknown PCM bluealsa
ALSA lib confmisc.c:1281:(snd_func_refer) Unable to find definition 'defaults.bluealsa.device'
ALSA lib conf.c:4528:(_snd_config_evaluate) function snd_func_refer returned error: No such file or directory
ALSA lib conf.c:4996:(snd_config_expand) Args evaluate error: No such file or directory
ALSA lib pcm.c:2495:(snd_pcm_open_noupdate) Unknown PCM bluealsa
Cannot connect to server socket err = No such file or directory
Cannot connect to server request channel
jack server is not running or cannot be started
JackShmReadWritePtr::~JackShmReadWritePtr - Init not done for -1, skipping unlock
JackShmReadWritePtr::~JackShmReadWritePtr - Init not done for -1, skipping unlock
ALSA lib pcm.c:2495:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.rear
ALSA lib pcm.c:2495:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.center_lfe
ALSA lib pcm.c:2495:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.side
ALSA lib pcm.c:2495:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.hdmi
ALSA lib pcm.c:2495:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.hdmi
ALSA lib pcm.c:2495:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.modem
ALSA lib pcm.c:2495:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.modem
ALSA lib pcm.c:2495:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.phoneline
ALSA lib pcm.c:2495:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.phoneline
ALSA lib confmisc.c:1281:(snd_func_refer) Unable to find definition 'defaults.bluealsa.device'
ALSA lib conf.c:4528:(_snd_config_evaluate) function snd_func_refer returned error: No such file or directory
ALSA lib conf.c:4996:(snd_config_expand) Args evaluate error: No such file or directory
ALSA lib pcm.c:2495:(snd_pcm_open_noupdate) Unknown PCM bluealsa
ALSA lib confmisc.c:1281:(snd_func_refer) Unable to find definition 'defaults.bluealsa.device'
ALSA lib conf.c:4528:(_snd_config_evaluate) function snd_func_refer returned error: No such file or directory
ALSA lib conf.c:4996:(snd_config_expand) Args evaluate error: No such file or directory
ALSA lib pcm.c:2495:(snd_pcm_open_noupdate) Unknown PCM bluealsa
Cannot connect to server socket err = No such file or directory
Cannot connect to server request channel
jack server is not running or cannot be started
JackShmReadWritePtr::~JackShmReadWritePtr - Init not done for -1, skipping unlock
JackShmReadWritePtr::~JackShmReadWritePtr - Init not done for -1, skipping unlock
Say Somrthing For Google Search
Traceback (most recent call last):
  File "openBrowser.py", line 18, in <module>
    audio = r.listen(source)
  File "/home/pi/Desktop/voiceModule/speech_recognition/__init__.py", line 657, in listen
    buffer = source.stream.read(source.CHUNK)
  File "/home/pi/Desktop/voiceModule/speech_recognition/__init__.py", line 198, in read
    return self.pyaudio_stream.read(size, exception_on_overflow=False)
  File "/usr/lib/python3/dist-packages/pyaudio.py", line 608, in read
    return pa.read_stream(self._stream, num_frames, exception_on_overflow)
OSError: [Errno -9988] Stream closed

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "openBrowser.py", line 19, in <module>
    except exception_on_overflow as e:
NameError: name 'exception_on_overflow' is not defined

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "openBrowser.py", line 21, in <module>
    print('Now, Processing on your request!')
  File "/home/pi/Desktop/voiceModule/speech_recognition/__init__.py", line 188, in __exit__
    self.stream.close()
  File "/home/pi/Desktop/voiceModule/speech_recognition/__init__.py", line 203, in close
    if not self.pyaudio_stream.is_stopped():
  File "/usr/lib/python3/dist-packages/pyaudio.py", line 543, in is_stopped
    return pa.is_stream_stopped(self._stream)
OSError: [Errno -9988] Stream closed

我在 Raspberry pi 中执行以下.py代码时遇到上述错误:

import speech_recognition as sr
import textToVoice as tv

r = sr.Recognizer()

with sr.Microphone(device_index = 0, sample_rate = 44100, chunk_size = 512) as source:
    print('Say Somrthing For Google Search')
    try:
        audio = r.listen(source)
    except exception_on_overflow as e:
        print('prob')
    print('Now, Processing on your request!')

try:
    text =r.recognize_google(audio)
    lang = 'en'
    tv.tts(text, lang)
    print('You Say:\n' + text)

except Exception as e:
    print('problem For Fatchning Page')
    print(e)

1 个答案:

答案 0 :(得分:0)

  

[Errno -9998]频道数无效

我改进/清理你的代码:

import speech_recognition as sr
import textToVoice as tv

r = sr.Recognizer()

with sr.Microphone() as source:  # your problem was at this line.
    print('Say Something For Google Search')
    try:
        audio = r.listen(source)
    except Exception as e:
        print(e)
    print('Now, Processing on your request!')

try:
    text =r.recognize_google(audio)
    lang = 'en'
    tv.tts(text, lang)
    print('You Say:\n' + text)

except Exception as e:
    print('problem For Fatchning Page')
    print(e)

您可以按照以下步骤进行安装:

sudo pip install SpeechRecognition

sudo pip install pyaudio

如果您发现错误:

sudo apt-get install python-pyaudio

sudo apt-get install libjack-jackd2-dev portaudio19-dev

然后再说:

sudo pip install pyaudio

如果您发现错误,请尝试以下操作:

sudo pip install --upgrade pyaudio

注意
也许这link可以帮到你:
https://stackoverflow.com/a/48460182/3702377