Explore Courses Blog Tutorials Interview Questions
0 votes
1 view
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)



        message = r.recognize_google(audio)


        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: 

Welcome to Intellipaat Community. Get your technical queries answered by top developers!

28.4k questions

29.7k answers


94.1k users

Browse Categories