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