· 5 years ago · Dec 11, 2020, 04:02 AM
1#!/usr/bin/python
2# Requires PyAudio and PySpeech.
3
4import speech_recognition as sr
5import time
6import os
7from gtts import gTTS
8import webbrowser
9
10my_file = "contacts.txt"
11
12def banner():
13 print("""
14 _ Virtual
15 | | __ _ _ __ ___ _ __ _ _ Assistant
16 _ | |/ _` | '_ \ / _ \ | '_ \| | | |
17| |_| | (_| | | | | __/_| |_) | |_| |
18 \___/ \__,_|_| |_|\___(_) .__/ \__, |
19 |_| |___/
20 """)
21# initialization
22
23def speak(audioString):
24 print(audioString)
25 tts = gTTS(text=audioString, lang='en')
26 tts.save("audio.mp3")
27 os.system("mpg123 audio.mp3")
28
29def recordAudio():
30 # Record Audio
31 r = sr.Recognizer()
32 with sr.Microphone() as source:
33 banner()
34 print("Say something:")
35 audio = r.listen(source)
36
37 # Speech recognition using Google Speech Recognition
38 data = ""
39 try:
40 # Uses the default API key
41 # To use another API key: `r.recognize_google(audio, key="GOOGLE_SPEECH_RECOGNITION_API_KEY")`
42 data = r.recognize_google(audio)
43 print("You said: " + data)
44 except sr.UnknownValueError:
45 print("Google Speech Recognition could not understand audio")
46 except sr.RequestError as e:
47 print("Could not request results from Google Speech Recognition service; {0}".format(e))
48
49 return data
50
51def sendSMS():
52 numbers = []
53 names = []
54 speak("Select acontact by number: ")
55
56 # open file in read mode
57 with open(my_file, 'r') as file_handle:
58 # convert file contents into a list
59 lines = file_handle.read().splitlines()
60 for i, val in enumerate(lines):
61 #split each line and appends name and number to respective list
62 person = val.split(" ", 1)
63 names.append(person[0])
64 numbers.append(person[1])
65 print(i, names[i], numbers[i])
66 if(i + 1 >= len(lines)):
67 break
68 my_file.close()
69
70 data = recordAudio()
71 i = data
72 i = int(i)
73 dest = numbers[i]
74 speak("Record your message")
75 data = recordAudio()
76 message = data
77 speak("Would you like to send " + message + " to " + names[i] + "?")
78 data = recordAudio()
79 if "yes" in data:
80 os.system("kdeconnect-cli --send-sms '%s' -n s20 --destination %s" % (message, dest))
81 speak("Message sent to " + names[i])
82
83
84def jarvis(data):
85 if "how are you" in data:
86 speak("I am fine, thanks")
87
88 if "what time is it" in data:
89 speak(time.strftime("%A %B %d %I:%M %p"))
90
91 if "where is" in data:
92 data = data.split(" ", 2)
93 location = data[2]
94 speak("Hold on Tim, I will show you where " + location + " is.")
95 webbrowser.open_new_tab("https://www.google.com/maps/place/" + location + "/&")
96
97 if "search for" in data:
98 data = data.split(" ", 2)
99 search = data[2]
100 speak("Hold on Tim, I will search for " + search)
101 webbrowser.open_new_tab('http://www.google.com/search?btnG=1&q=' + search)
102
103 if "start" in data:
104 data = data.split(" ", 1)
105 start = data[1]
106 speak("Starting " + start)
107 os.system(start + "&")
108
109 if "signal" in data:
110 os.system("signal-desktop &")
111
112 if "hey Jane" in data:
113 speak("Hey Tim, what's up?")
114
115 if "send text" in data:
116 sendSMS()
117
118 if "open Instagram" in data:
119 instagram = 'istekram'
120 os.system(instagram + "&")
121
122# initialization
123time.sleep(.5)
124banner()
125speak("Hi Tim, how can I help?")
126while 1:
127 data = recordAudio()
128 jarvis(data)
129