Uberi / speech_recognition

Speech recognition module for Python, supporting several engines and APIs, online and offline.
https://pypi.python.org/pypi/SpeechRecognition/
BSD 3-Clause "New" or "Revised" License
8.33k stars 2.4k forks source link

AssertionError: Audio source must be entered before listening, see documentation for ``AudioSource``; are you using ``source`` outside of a ``with`` statement? #765

Open SAAD0206 opened 1 week ago

SAAD0206 commented 1 week ago
import pyttsx3
import speech_recognition as sr

engine = pyttsx3.init("sapi5")
voices = engine.getProperty("voices")

# text to speech
def speak(audio):
    engine.say(audio)
    print(audio)
    engine.runAndWait()

def take_command():
    rec = sr.Recognizer()
    with sr.Microphone() as source:
        print("listening")
        rec.pause_threshold = 1
        audio = rec.listen(source)

    try:
        print("RECOGNIZING...")
        words = rec.recognize_google_cloud(audio,language="en-in")
        print(f"user said: {words}")

    except Exception as exp:
        speak("say that again please...")
        return "none"
    return words

if __name__ == "__main__":
    take_command()
    #speak("MY NAME IS JARVIS I AM HERE TO ASSIST U")
  1. (How do you make the issue happen? Does it happen every time you try it?) every time i run this code
  2. (Make sure to go into as much detail as needed to reproduce the issue. Posting your code here can help us resolve the problem much faster!)
  3. (If there are any files, like audio recordings, don't forget to include them.)

Expected behaviour

it should be listening my voice command

Actual behaviour

'NoneType' object has no attribute 'close'
  File "C:\Users\atif\Documents\PYTHON\JARVIS.py", line 20, in take_command
    audio = rec.listen(source)
            ^^^^^^^^^^^^^^^^^^
AssertionError: Audio source must be entered before listening, see documentation for ``AudioSource``; are you using ``source`` outside of a ``with`` statement?

During handling of the above exception, another exception occurred:

System information

(Delete all the statements that don't apply.)

My system is . (For example, "Ubuntu 16.04 LTS x64", "Windows 10 x64", or "macOS Sierra".)

My Python version is . (You can check this by running python -V.)

My Pip version is . (You can check this by running pip -V.)

My SpeechRecognition library version is . (You can check this by running python -c "import speech_recognition as sr;print(sr.__version__)".)

My PyAudio library version is / I don't have PyAudio installed. (You can check this by running python -c "import pyaudio as p;print(p.__version__)".)

My microphones are: (You can check this by running python -c "import speech_recognition as sr;print(sr.Microphone.list_microphone_names())".)

My working microphones are: (You can check this by running python -c "import speech_recognition as sr;print(sr.Microphone.list_working_microphones())".)

I installed PocketSphinx from . (For example, from the Debian repositories, from Homebrew, or from the source code.)

JeziahAnon commented 4 days ago

It states that the source is used outside of the with scope. Are you sure that the try/except block and the rec.listen(source) are inside the "with" block? Its just unclear because of the formatting on github :0

I have it like this and it works.


import pyttsx3
import speech_recognition as sr

engine = pyttsx3.init("sapi5")
voices = engine.getProperty("voices")

def speak(audio):
    engine.say(audio)
    print(audio)
    engine.runAndWait()

def take_command():
    rec = sr.Recognizer()

    with sr.Microphone() as source:
        print("listening")
        rec.pause_threshold = 1
        audio = rec.listen(source)

        try:
            print("RECOGNIZING...")
            words = rec.recognize_google(audio,language="en-in")
            print(f"user said: {words}")

        except Exception as exp:
            speak("say that again please...")
            return "none"
        return words

if __name__ == "__main__":
    take_command()
ftnext commented 3 days ago

@JeziahAnon Thanks. I added python syntax highlight

@SAAD0206 Please check JeziahAnon's reply