import speech_recognition import playsound # Play audio clip and attempt to recognise speech. def recogniseCommand(filename): print('Now testing', filename) playsound.playsound(filename) recognizer = speech_recognition.Recognizer() with speech_recognition.AudioFile(filename) as source: audio = recognizer.listen(source) text = recognizer.recognize_google(audio) print('I heard:', text) if 'on' in text: print('The light is ON.') elif 'off' in text: print('The light is OFF.') else: print('Voice command unrecognised.') # Main program. print('Type 1 to turn the light on or 0 to turn it off.') while True: command = input() if (command == '1'): recogniseCommand('lightSwitchOn.wav') # Use your audio clip filename here. if (command == '0'): recogniseCommand('lightSwitchOff.wav') # Use your audio clip filename here.