Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
376 changes: 149 additions & 227 deletions JARVIS/JARVIS.py
Original file line number Diff line number Diff line change
@@ -1,240 +1,162 @@
#########

__author__ = 'Mohammed Shokr <mohammedshokr2014@gmail.com>'
__version__ = 'v 0.1'
__author__ = "Mohammed Shokr <mohammedshokr2014@gmail.com>"
__version__ = "v 0.1"

"""
JARVIS:
- Control windows programs with your voice
"""


# import modules
from datetime import datetime # datetime module supplies classes for manipulating dates and times
import subprocess # subprocess module allows you to spawn new processes
# master
import pyjokes
import requests
import json
#for 30 seconds clip "Jarvis, clip that!" and discord ctrl+k quick-move (might not come to fruition)
from pynut import keyboard
# =======
from playsound import * #for sound output
# master
import speech_recognition as sr # speech_recognition Library for performing speech recognition with support for Google Speech Recognition, etc..

# pip install pyttsx3
# need to run only once to install the library

# importing the pyttsx3 library
import pyttsx3

import datetime
import subprocess
import webbrowser
import speech_recognition as sr
import pyttsx3
import pyjokes
from PIL import ImageGrab


class VoiceEngine:
def __init__(self):
self.engine = pyttsx3.init()
voices = self.engine.getProperty("voices")
self.engine.setProperty("voice", voices[0].id)
self.engine.setProperty("rate", 150)

def speak(self, text):
self.engine.say(text)
self.engine.runAndWait()


class SpeechRecognizer:
def __init__(self):
self.recognizer = sr.Recognizer()

def listen(self):
with sr.Microphone() as source:
print("Listening...")
self.recognizer.pause_threshold = 1
audio = self.recognizer.listen(source)

try:
print("Recognizing...")
query = self.recognizer.recognize_google(audio, language="en-in")
print(f"User said: {query}")
return query.lower()

except Exception:
print("Could not understand.")
return ""


class SystemTasks:
def open_notepad(self):
subprocess.call(["Notepad.exe"])

def open_calculator(self):
subprocess.call(["calc.exe"])

def open_cmd(self):
subprocess.call(["cmd.exe"])

def open_paint(self):
subprocess.call(["mspaint.exe"])

def open_youtube(self):
webbrowser.open("https://youtube.com")

def open_google(self):
webbrowser.open("https://google.com")

def open_github(self):
webbrowser.open("https://github.com")

def take_screenshot(self):
image = ImageGrab.grab()
filename = datetime.datetime.now().strftime("%Y%m%d_%H%M%S") + ".jpg"
image.save(filename)
print("Screenshot saved")


# initialisation
engine = pyttsx3.init()
voices = engine.getProperty('voices')
engine.setProperty('voice', voices[0].id)
engine.setProperty('rate', 150)

def speak_news():
url = 'http://newsapi.org/v2/top-headlines?sources=the-times-of-india&apiKey=yourapikey'
news = requests.get(url).text
news_dict = json.loads(news)
arts = news_dict['articles']
speak('Source: The Times Of India')
speak('Todays Headlines are..')
for index, articles in enumerate(arts):
speak(articles['title'])
if index == len(arts)-1:
break
speak('Moving on the next news headline..')
speak('These were the top headlines, Have a nice day Sir!!..')


def sendEmail(to, content):
server = smtplib.SMTP('smtp.gmail.com', 587)
server.ehlo()
server.starttls()
server.login('youremail@gmail.com', 'yourr-password-here')
server.sendmail('youremail@gmail.com', to, content)
server.close()


# obtain audio from the microphone
r = sr.Recognizer()
with sr.Microphone() as source:
print('[JARVIS]:' + "Say something")
engine.say("Say something")
engine.runAndWait()
audio = r.listen(source)


#for audio output instead of print
def voice(p):
myobj=gTTS(text=p,lang='en',slow=False)
myobj.save('try.mp3')
playsound('try.mp3')

# recognize speech using Google Speech Recognition
Query = r.recognize_google(audio, language = 'en-IN', show_all = True )
print(Query)


# Run Application with Voice Command Function
# only_jarvis
class Jarvis:
def __init__(self, Q):
self.query = Q

def sub_call(self, exe_file):
"""
This method can directly use call method of subprocess module and according to the
argument(exe_file) passed it returns the output.

exe_file:- must pass the exe file name as str object type.

"""
return subprocess.call([exe_file])

def get_dict(self):
'''
This method returns the dictionary of important task that can be performed by the
JARVIS module.

Later on this can also be used by the user itself to add or update their preferred apps.
'''
_dict = dict(
time=datetime.now(),
notepad='Notepad.exe',
calculator='calc.exe',
stickynot='StickyNot.exe',
shell='powershell.exe',
paint='mspaint.exe',
cmd='cmd.exe',
browser='C:\Program Files\Internet Explorer\iexplore.exe',
)
return _dict

@property
def get_app(self):
task_dict = self.get_dict()
task = task_dict.get(self.query, None)
if task is None:
engine.say("Sorry Try Again")
engine.runAndWait()
def __init__(self):
self.voice = VoiceEngine()
self.listener = SpeechRecognizer()
self.tasks = SystemTasks()
self.running = True

def wish_me(self):
hour = datetime.datetime.now().hour

if hour < 12:
self.voice.speak("Good Morning")

elif hour < 18:
self.voice.speak("Good Afternoon")

else:
self.voice.speak("Good Evening")

self.voice.speak("I am Jarvis. How can I help you?")

def execute_command(self, command):

if "open notepad" in command:
self.tasks.open_notepad()

elif "open calculator" in command:
self.tasks.open_calculator()

elif "open cmd" in command:
self.tasks.open_cmd()

elif "open paint" in command:
self.tasks.open_paint()

elif "open youtube" in command:
self.tasks.open_youtube()

elif "open google" in command:
self.tasks.open_google()

elif "open github" in command:
self.tasks.open_github()

elif "screenshot" in command:
self.tasks.take_screenshot()
self.voice.speak("Screenshot taken")

elif "joke" in command:
joke = pyjokes.get_joke()
print(joke)
self.voice.speak(joke)

elif "time" in command:
current_time = datetime.datetime.now().strftime("%H:%M:%S")
print(current_time)
self.voice.speak(current_time)

elif "exit" in command:
self.voice.speak("Goodbye")
self.running = False

else:
if 'exe' in str(task):
return self.sub_call(task)
print(task)
return

# =======
def get_app(Q):
# master
if Q == "time":
print(datetime.now())
x=datetime.now()
voice(x)
elif Q=="news":
speak_news()

elif Q == "open notepad":
subprocess.call(['Notepad.exe'])
elif Q == "open calculator":
subprocess.call(['calc.exe'])
elif Q == "open stikynot":
subprocess.call(['StikyNot.exe'])
elif Q == "open shell":
subprocess.call(['powershell.exe'])
elif Q == "open paint":
subprocess.call(['mspaint.exe'])
elif Q == "open cmd":
subprocess.call(['cmd.exe'])
elif Q == "open discord":
subprocess.call(['discord.exe'])
elif Q == "open browser":
subprocess.call(['C:\Program Files\Internet Explorer\iexplore.exe'])
# patch-1
elif Q == "open youtube":
webbrowser.open("https://www.youtube.com/") # open youtube
elif Q == "open google":
webbrowser.open("https://www.google.com/") # open google
elif Q == "open github":
webbrowser.open
elif Q == "email to other": # here you want to change and input your mail and password whenver you implement
try:
speak("What should I say?")
r = sr.Recognizer()
with sr.Microphone() as source:
print("Listening...")
r.pause_threshold = 1
audio = r.listen(source)
to = "abc@gmail.com"
sendEmail(to, content)
speak("Email has been sent!")
except Exception as e:
print(e)
speak("Sorry, I can't send the email.")
# =======
# master
elif Q=="Take screenshot":
snapshot=ImageGrab.grab()
drive_letter = "C:\\"
folder_name = r'downloaded-files'
folder_time = datetime.datetime.now().strftime("%Y-%m-%d_%I-%M-%S_%p")
extention = '.jpg'
folder_to_save_files = drive_letter + folder_name + folder_time + extention
snapshot.save(folder_to_save_files)

elif Q=="Jokes":
speak(pyjokes.get_joke())

elif Q=="start recording":
current.add('Win', 'Alt', 'r')
speak("Started recording. just say stop recording to stop.")

elif Q=="stop recording":
current.add('Win', 'Alt', 'r')
speak("Stopped recording. check your game bar folder for the video")

elif Q=="clip that":
current.add('Win', 'Alt', 'g')
speak("Clipped. check you game bar file for the video")
with keyboard.Listener(on_press=on_press, on_release=on_release) as listener:
listener.join()





# master
else:
engine.say("Sorry Try Again")
engine.runAndWait()

# =======
# =======

apps = {
"time": datetime.now(),
"notepad": "Notepad.exe",
"calculator": "calc.exe",
"stikynot": "StikyNot.exe",
"shell": "powershell.exe",
"paint": "mspaint.exe",
"cmd": "cmd.exe",
"browser": "C:\Program Files\Internet Explorer\iexplore.exe"
}
# master

for app in apps:
if app == Q.lower():
subprocess.call([apps[app]])
break
# master
else:
engine.say("Sorry Try Again")
engine.runAndWait()
# master
return
# Call get_app(Query) Func.
Jarvis(Query).get_app
self.voice.speak("Command not found")

def run(self):
self.wish_me()

while self.running:
command = self.listener.listen()

if command:
self.execute_command(command)


if __name__ == "__main__":
jarvis = Jarvis()
jarvis.run()
Loading