Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (19.9k points)

I am running a python script for speech-recognition:

def readAudioFile(audio_file):

    r = sr.Recognizer()

    file = sr.AudioFile(audio_file)

    with file as source:

        audio = r.record(source)

        type(audio)

    try:

        message = r.recognize_google(audio)

        print(message)

        print("Data extracted from phone conversation:\n" + message)

    except sr.UnknownValueError:

        print("Google Speech Recognition could not understand audio")

But when I run this, I get NameError: name 'sr' is not defined  error. How to solve this?

1 Answer

0 votes
by (25.1k points)

It's most probably because you are not importing speech recognition module correctly. put this line at the top of your file:

import speech_recognition as sr

If you want to get a deeper understanding of data science and libraries used in it you can watch this youtube video: 

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...