├── README.md └── speech_rec.py /README.md: -------------------------------------------------------------------------------- 1 | "# jarvis" 2 | -------------------------------------------------------------------------------- /speech_rec.py: -------------------------------------------------------------------------------- 1 | import subprocess 2 | import os 3 | import speech_recognition as sr 4 | import time 5 | r = sr.Recognizer() 6 | m = sr.Microphone() 7 | 8 | fav_song_path = "path to song" 9 | 10 | def execute_command(string): 11 | if("play" in string): 12 | subprocess.Popen(['path to espeak.exe', "Playing your favorite song sir"]) 13 | time.sleep(2) 14 | subprocess.Popen([r'path to vlc.exe', fav_song_path]) 15 | def listen_to_audio(): 16 | try: 17 | print("A moment of silence, please...") 18 | with m as source: r.adjust_for_ambient_noise(source) 19 | print("Set minimum energy threshold to {}".format(r.energy_threshold)) 20 | print("Say something!") 21 | with m as source: audio = r.listen(source) 22 | print("Got it! Now to recognize it...") 23 | try: 24 | # recognize speech using Google Speech Recognition 25 | value = r.recognize_google(audio) 26 | 27 | # we need some special handling here to correctly print unicode characters to standard output 28 | if str is bytes: # this version of Python uses bytes for strings (Python 2) 29 | print(u"You said {}".format(value).encode("utf-8")) 30 | execute_command(u"{}".format(value).encode("utf-8")) 31 | else: # this version of Python uses unicode for strings (Python 3+) 32 | print("You said {}".format(value)) 33 | execute_command("{}".format(value)) 34 | except sr.UnknownValueError: 35 | print("Oops! Didn't catch that") 36 | except sr.RequestError as e: 37 | print("Uh oh! Couldn't request results from Google Speech Recognition service; {0}".format(e)) 38 | except KeyboardInterrupt: 39 | pass 40 | 41 | 42 | while(True): 43 | listen_to_audio() 44 | time.sleep(1) 45 | --------------------------------------------------------------------------------