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.2k stars 2.39k forks source link

Problem with sr.Microphone #688

Open IamVictheCG opened 1 year ago

IamVictheCG commented 1 year ago

Steps to reproduce

Expected behaviour

I am trying to build a voice activated control system that would be integrated with Arduino

Actual behaviour

This is my code;

import speech_recognition as sr from pyfirmata import Arduino, SERVO, util from time import sleep

r = sr.Recognizer()

mic = sr.Microphone()

mic = sr.Microphone(device_index=1)

print(mic.list_microphone_names())

with mic as source: r.adjust_for_ambient_noise(source)

while True :
    audio = r.listen(source)
    try:  
        if r.recognize_google(audio) == 'switch on':
            print('Switch is on')

        else:
            print('Speech detected,Comman not found')

    except:
        print('no audio')

And this is the error I keep getting;

Traceback (most recent call last): File "c:\Users\Administrator\Desktop\Voice recognition_PYTHON\Voice Recognition_1\program.py", line 13, in r.adjust_for_ambient_noise(source) File "C:\Users\Administrator\AppData\Local\Programs\Python\Python310\lib\site-packages\speech_recognition\init__.py", line 383, in adjust_for_ambient_noise assert source.stream is not None, "Audio source must be entered before adjusting, see documentation for AudioSource; are you using source outside of a with statement?" AssertionError: Audio source must be entered before adjusting, see documentation for AudioSource; are you using source outside of a with statement?

During handling of the above exception, another exception occurred:

Traceback (most recent call last): File "c:\Users\Administrator\Desktop\Voice recognition_PYTHON\Voice Recognition_1\program.py", line 12, in with mic as source: File "C:\Users\Administrator\AppData\Local\Programs\Python\Python310\lib\site-packages\speech_recognition\init.py", line 189, in exit__ self.stream.close() AttributeError: 'NoneType' object has no attribute 'close'

Please help 😭😭😭

System information

My system is <Windows 11 x64>. My Python version is <Python 3.10>.

My Pip version is <pip 23.1.2>.

My SpeechRecognition library version is <SpeechRecognition 3.10.0>.

My PyAudio library version is <PyAudio 0.2.13>

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: ['Microsoft Sound Mapper - Input', 'Microphone Array (Realtek High ', 'Microsoft Sound Mapper - Output', 'Speaker/Headphone (Realtek High', 'Primary Sound Capture Driver', 'Microphone Array (Realtek High Definition Audio)', 'Primary Sound Driver', 'Speaker/Headphone (Realtek High Definition Audio)', 'Speaker/Headphone (Realtek High Definition Audio)', 'Microphone Array (Realtek High Definition Audio)', 'Headset (@System32\drivers\bthhfenum.sys,#2;%1 Hands-Free%0\r\n;(T2))', 'Headset (@System32\drivers\bthhfenum.sys,#2;%1 Hands-Free%0\r\n;(T2))', 'Speakers (Realtek HD Audio output)', 'Microphone Array (Realtek HD Audio Mic input)', 'Stereo Mix (Realtek HD Audio Stereo input)', 'Headphones ()', 'Headphones ()', 'Headset (@System32\drivers\bthhfenum.sys,#2;%1 Hands-Free%0\r\n;(TG-126))', 'Headset (@System32\drivers\bthhfenum.sys,#2;%1 Hands-Free%0\r\n;(TG-126))', 'Output (@System32\drivers\bthhfenum.sys,#4;%1 Hands-Free HF Audio%0\r\n;(OPPO A77s))', 'Input (@System32\drivers\bthhfenum.sys,#4;%1 Hands-Free HF Audio%0\r\n;(OPPO A77s))', 'Input ()']

renatocava commented 9 months ago

Hi @IamVictheCG

I am getting the same error with my code:

import speech_recognition as sr

with sr.Microphone() as source:
    r = sr.Recognizer()
    print("Say something!")
    audio = r.listen(source)

Then getting this error:

Traceback (most recent call last):
  File "C:\Users\renato\Documents\mvp_1\reconocimiento_voz.py", line 6, in <module>
    audio = r.listen(source)
            ^^^^^^^^^^^^^^^^
  File "C:\Users\renato\Documents\entornos\mvp_1\Lib\site-packages\speech_recognition\__init__.py", line 465, in listen
    assert source.stream is not None, "Audio source must be entered before listening, see documentation for ``AudioSource``; are you using ``source`` outside of a ``with`` statement?"
           ^^^^^^^^^^^^^^^^^^^^^^^^^
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:

Traceback (most recent call last):
  File "C:\Users\renato\Documents\mvp_1\reconocimiento_voz.py", line 3, in <module>
    with sr.Microphone() as source:
  File "C:\Users\renato\Documents\entornos\mvp_1\Lib\site-packages\speech_recognition\__init__.py", line 189, in __exit__
    self.stream.close()
    ^^^^^^^^^^^^^^^^^
AttributeError: 'NoneType' object has no attribute 'close'
Say something!

Run in Windows 10 and PyCharm IDE

RESOLVED:

I solved the problem by uninstalling my Kaspersky antivirus. The problem was the firewall of this antivirus. I uninstalled it because I had no way to disable it, but surely disabling it will also correct the problem.

I tried uninstalling the antivirus because I read it in a comment in the following link: https://github.com/Uberi/speech_recognition/issues/423#issuecomment-531973098