pythonpython-3.xvoice-recognitionpyaudio

I have trouble working with pyaudio . How do I fix the errors?


I'm having trouble with my voice commander as it's not working properly and I think it's because of some errors in the PyAudio file. Can you suggest some steps to fix this issue? I have already installed the pyaudio version 0.2.13 which is compatible with my python version 3.8.5.

Here's the code:

import  pyttsx3 
import speech_recognition as sr
import webbrowser
import datetime
import pyjokes
import smtplib



def sptext():
    recognizer = sr.Recognizer() 
    with sr. Microphone() as source:
        print("Listening...")
        # Noise cancellation
        recognizer.adjust_for_ambient_noise(source)
        audio= recognizer.listen(source)
        try:
            print("Jack is recognizing...")
           data = recognizer.recognize_google(audio)
            print(data)
        except sr.UnknownValueError:
            print("Jack is uncertain")
            
        
        
sptext()

Traceback:

 Listening...
    Traceback (most recent call last):
      File "c:\Users\91999\.vscode\Python\tempCodeRunnerFile.py", line 16, in sptext
        audio= recognizer.listen(source)
      File "C:\Users\91999\AppData\Local\Programs\Python\Python38\lib\site-packages\speech_recognition\__init__.py", line 523, in listen
        buffer = source.stream.read(source.CHUNK)
      File "C:\Users\91999\AppData\Local\Programs\Python\Python38\lib\site-packages\speech_recognition\__init__.py", line 199, in read
        return self.pyaudio_stream.read(size, exception_on_overflow=False)
      File "C:\Users\91999\AppData\Local\Programs\Python\Python38\lib\site-packages\pyaudio\__init__.py", line 570, in read
        return pa.read_stream(self._stream, num_frames,
    OSError: [Errno -9999] Unanticipated host error
    
    During handling of the above exception, another exception occurred:
    
    Traceback (most recent call last):
      File "c:\Users\91999\.vscode\Python\tempCodeRunnerFile.py", line 27, in <module> 
        sptext()
      File "c:\Users\91999\.vscode\Python\tempCodeRunnerFile.py", line 23, in sptext   
        print(" is uncertain")
      File "C:\Users\91999\AppData\Local\Programs\Python\Python38\lib\site-packages\speech_recognition\__init__.py", line 189, in __exit__
        self.stream.close()
      File "C:\Users\91999\AppData\Local\Programs\Python\Python38\lib\site-packages\speech_recognition\__init__.py", line 204, in close
        if not self.pyaudio_stream.is_stopped():
      File "C:\Users\91999\AppData\Local\Programs\Python\Python38\lib\site-packages\pyaudio\__init__.py", line 515, in is_stopped
        return pa.is_stream_stopped(self._stream)
    OSError: [Errno -9988] Stream closed
    PS C:\Users\91999\.vscode\Python> 
    

Solution

  • This means problem with your microphone.

    For example:

    1. Run your code
    2. Turn off microphone (in OS, You can just pull the microphone out of the device if you can)
    3. Get this error

    When i run your code whithout step 2), all works correctly. So error means that stream from your microphone/source is over.

    If there is a problem with the sound source, then this is not a problem in the program. So to understand further is off-topic (Possible problems: drivers, a kind of OS, poor sound source mounting, etc.). You must deal with this yourself or with this. There you can find Troubleshooting.