Python3 OSError: [Errno -9988] Stream closed

怎甘沉沦 提交于 2021-01-28 06:10:07

问题


I'm working on voice recognition system with Raspberry pi 3B and while running some code I face some problem which is given below. I was tried so many stuffs from the internet but couldn't found some specific problem so please help me about below error!

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

I face above error while performing below .py code in Raspberry pi:

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:


[Errno -9998] Invalid number of channels

I improve/clean your code:

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)

You can follow the steps given below for install:

sudo pip install SpeechRecognition

sudo pip install pyaudio

If you found error:

sudo apt-get install python-pyaudio

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

Then again:

sudo pip install pyaudio

If you found error try this:

sudo pip install --upgrade pyaudio

NOTE:
Maybe this link help you up:
https://stackoverflow.com/a/48460182/3702377



来源:https://stackoverflow.com/questions/50772605/python3-oserror-errno-9988-stream-closed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!