2

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.

theashwanisingla
  • 412
  • 4
  • 13
  • can you please post the contents of the `my_mic` object? I have a hunch about what's going on but want to check the hunch. – Kathy Reid Jun 26 '23 at 10:13
  • @KathyReid, I have updated my question. Please check. – theashwanisingla Jun 29 '23 at 10:30
  • hi @theashwanisingla, it wasn't what I thought it was. There is a similar question here - https://stackoverflow.com/questions/68895962/microphone-on-windows-not-working-for-speech-recognition-by-using-python - so you could try adjusting for ambient noise. But that doesn't explain why the microphone isn't being recognised though. There's also this question - https://stackoverflow.com/questions/51259274/taking-audio-input-from-pc-microphone-using-python - which looks at the parameters that can be passed to sr.Microphone. Can you do some experimenting with these parameters? – Kathy Reid Jun 29 '23 at 22:45
  • What I have seen in the past is that the `sr.Microphone()` is expecting 48kHz audio in the SAMPLE_RATE, but many PC microphones sample at 96kHz by default, so the SAMPLE_RATE has to be explicitly passed. That doesn't seem to be the case here, as it's not recognising the microphone at all. – Kathy Reid Jun 29 '23 at 22:47
  • Another thought - is `pyaudio` installed system-wide or are you using a virtual environment? I wonder if that affects where the microphone stream is created, and the permissions around it? – Kathy Reid Jun 29 '23 at 22:48
  • @KathyReid, I already tried the above-mentioned solutions. The same code is working well on my other computer. ```pyaudio``` is installed system-wide and I am not using any virtual environment. – theashwanisingla Jul 01 '23 at 10:09
  • Maybe you can check if you have a woking mic in windows? In `settings` looking at [System] > [sound] > [all sound devices], you can test all audio input devices. – Leon Klute Jul 04 '23 at 14:47
  • @LeonKlute, the mic is working fine. This was the first thing that I tested. – theashwanisingla Jul 04 '23 at 17:42

2 Answers2

0

If it's not the software, chances are it is the hardware

Ensure your microphone is working: Test your microphone using other applications or tools to verify that it is functioning correctly. If it's not working, you may need to troubleshoot or reinstall the microphone drivers. At least that is what I would do to begin with on a new platform.

My device index is 1

Instead of relying on the default microphone, you can try explicitly specifying the microphone name when initializing Microphone. First, you need to list the available microphones on your system. Modify your code as follows:

import speech_recognition as s_r

microphone_list = s_r.Microphone.list_microphone_names()
print(microphone_list)

Run the above code and check the list of microphone names printed (is it even listed?). Identify the name of the working microphone you want to use and update the code accordingly:

my_mic = s_r.Microphone(device_index=microphone_list.index("your_microphone
mrk
  • 8,059
  • 3
  • 56
  • 78
  • 1
    I have tried all these methods. There is a long list of microphone names while when I print the working microphone, the list is empty. I checked that normally my microphone is working fine. This is a weird situation. – theashwanisingla Jul 03 '23 at 09:56
  • 1
    If I talk about ``` my device is 1```, I have tried with multiple and the default one. But the same result. – theashwanisingla Jul 03 '23 at 09:57
  • thanks for the response. If I have another idea, I ll be back. – mrk Jul 03 '23 at 10:20
0

Finally, I found the root cause of the issue. As I mentioned, I am using Windows 11. And it does not allow the Microphone access to every application. We need to provide permission for it explicitly.

The way to provide the permission is:

Settings > Privacy and Security > Microphone

Then enable the access by allowing other applications to use the Microphone.

Permission Setting

This has fixed my issue and the code is working fine. Thank you, everyone, for your valuable suggestions.

theashwanisingla
  • 412
  • 4
  • 13