Search code examples
pythonpython-3.xspeech-recognitionpyaudio

Having trouble with speech to text in Python


import speech_recognition as sr

rec = sr.Recognizer()
with sr.Microphone as source:
    print('say something')
    audio = rec.listen(source)
    voice_data = rec.recognize_google(audio)
    print(voice_data)

This is my code which is shown in plenty of tutorials that I watched online, I installed SpeechRecognizer and pyAudio (using whl file, not with pip, I don't know if it makes a difference).

When I try to run it it gives me an error about something in the pydevd.py and at the end gives me:

with sr.Microphone as source:

AttributeError: enter

How can I resolve this?


Solution

  • I got it to work by moving the statements not related to capturing the audio out of the with statement, this should work for you:

    import speech_recognition as sr
    
    rec = sr.Recognizer()
    
    print('say something')
    with sr.Microphone() as source:
        audio = rec.listen(source)
        voice_data = rec.recognize_google(audio)
    
    print(voice_data)