Creating a speech recognition program using pyaudio but having an issue

190 views Asked by At

I'm trying to create a speech recognition program using pyaudio but I'm having some issues.

I wrote a little bit of code to test some stuff, but for some reason my program is hanging on the following piece of code "audio = r.listen(source)" and I dont know why. Here is my full code so far:

import speech_recognition as sr

r = sr.Recognizer()
with sr.Microphone() as source:
    #r.adjust_for_ambient_noise(source,duration=1) -> this didnt help either
    print('Say Something')
    audio = r.listen(source)
    voice_data = r.recognize_google(audio)
    print(voice_data)

and this is what I get when I quit the program:

^CTraceback (most recent call last):
  File "main.py", line 9, in <module>
    audio = r.listen(source)
  File "/Users/user1/Documents/speech-assistant/venv/lib/python3.7/site-packages/speech_recognition/__init__.py", line 620, in listen
    buffer = source.stream.read(source.CHUNK)
  File "/Users/user1/Documents/speech-assistant/venv/lib/python3.7/site-packages/speech_recognition/__init__.py", line 161, in read
    return self.pyaudio_stream.read(size, exception_on_overflow=False)
  File "/Users/user1/Documents/speech-assistant/venv/lib/python3.7/site-packages/pyaudio.py", line 608, in read
    return pa.read_stream(self._stream, num_frames, exception_on_overflow)
KeyboardInterrupt

I've been stuck with this all day and still can't find a solution and looked through so many other posts online. If you can please help this would be greatly appreciated as I'm a beginner with this stuff. Thanks so much.

(p.s.) It's really weird because there is no output and no error message when I run the program. I checked to make my microphone was connected and stuff but still getting the same thing.

1

There are 1 answers

0
Zak B. On

Nevermind, I figured it out now. VS Code wasn't picking up my mic from the system. All good now.

Note: I found PyCharm to be much better for these types of projects than VS Code. PyCharm has many of these packages already pre-installed into their IDE, that you can easily just add/install into your project of preference. Hope this helps for anyone in the future!