'i am not getting the required output it tells "say something" "speechrecognition could not understand audio" "say something"

import speech_recognition as sr
import gtts
from playsound import playsound
import os

r = sr.Recognizer()

ACTIVATION_COMMAND = "hey sam"

def get_audio():
    with sr.Microphone() as source:
        print("say something")
        audio = r.listen(source)
    return audio

def audio_to_text(audio):
    text = ""
    try:
        text = r.recognize_google(audio)
    except sr.UnknownValueError:
        print("speechrecognition could not understand audio")
    except sr.RequestError:
        print("could not request results from API")
    return text

def play_sound(text):
    try:
        tts = gtts.gTTS(text)
        tempfile = "./temp.mp3"
        tts.save(tempfile)
        playsound(tempfile)
        os.remove(tempfile)

    except AssertionError:
        print("could not play sound")


if __name__ == "__main__":
    while True:
        a = get_audio()
        command = audio_to_text(a)

        if ACTIVATION_COMMAND in command.lower():
            print("activate")
            play_sound("what can i do for you")

#i cannot get the desired output it should show activate if the Activation Command works by just saying 'hey sam' but it just shows say something and then speechrecognition could not understand audio is executed whereas it should execute 'activate'



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source