Thursday, 29 June 2023

Error in r.listen(source) while using the speech recognition module

I am trying to run a basic code for speech recognition. But I am getting an error in the following line:

audio = r.listen(source)

The small piece of code that I am trying is:

import speech_recognition as s_r
print(s_r.__version__) # just to print the version not required
r = s_r.Recognizer()
my_mic = s_r.Microphone() #my device index is 1, you have to put your device index
with my_mic as source:
    print("Say now!!!!")
    audio = r.listen(source) #take voice input from the microphone
print(r.recognize_google(audio)) #to print voice into text

I have developed multiple projects using this code but this time I am getting the following error:

3.10.0
Say now!!!!
Traceback (most recent call last):
  File "D:\professional\JARVIS\assistant\test.py", line 7, in <module>
    audio = r.listen(source) #take voice input from the microphone
  File "C:\Users\theas\AppData\Roaming\Python\Python38\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 "D:\professional\JARVIS\assistant\test.py", line 7, in <module>
    audio = r.listen(source) #take voice input from the microphone
  File "C:\Users\theas\AppData\Roaming\Python\Python38\site-packages\speech_recognition\__init__.py", line 189, in __exit__
    self.stream.close()
AttributeError: 'NoneType' object has no attribute 'close'
>>> 

I am using this code on a new system with Windows 11. I have installed a fresh Python with default settings with version 3.11.4. The following library versions have been installed for speech recognition:

SpeechRecognition: 3.10.0
Pyaudio: 0.2.13

The same code is working in my other system. Please help me find this silly mistake I made during the setup.

UPDATE: When I try to print the objects of my_mic. I get the following output:

['CHUNK', 'MicrophoneStream', 'SAMPLE_RATE', 'SAMPLE_WIDTH', '__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__enter__', '__eq__', '__exit__', '__format__', '__ge__', '__getattribute__', '__getstate__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'audio', 'device_index', 'format', 'get_pyaudio', 'list_microphone_names', 'list_working_microphones', 'pyaudio_module', 'stream']

When I try to get a list of all the microphones present, I get a large list. But when I try to get the list of working microphones, that is empty.



from Error in r.listen(source) while using the speech recognition module

No comments:

Post a Comment