-
Notifications
You must be signed in to change notification settings - Fork 1
/
theJarvis_Code2.py
55 lines (32 loc) · 2.41 KB
/
theJarvis_Code2.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import speech_recognition as sr #use google speech recognition program
from gtts import gTTS #use google text-to-speech program
from pygame import mixer #use the mixer to play computer's speech
# GOOGLE SPEECH RECOGNITION CODE TO RECOGNIZE YOUR SPEECH! :)
while True: # continuously listen to user for input
r = sr.Recognizer() #using the Recognizer program from speech recognition tool to recognize your speech
with sr.Microphone()as source: #turning the microphone as a listening source
r.adjust_for_ambient_noise(source) # this code cancels any background noise
print('Talk now: ') # Asks the user to start talking
audio = r.listen(source) #Recognizer listens to the microphone's source and stores that content in audio
try:
message = (r.recognize_google(audio)) #takes the audio content to recognizes the user's speech and stores in message
print(message) #outputs and prints the user message, such as 'Hello', etc
#GOOGLE TEXT TO SPEECH CODE RESPOND BACK TO YOU! :)
#condition to respond to "hello"
if 'hello' in message: # if google speech recognition hears hello from you
speech = ('Hello, you look great') # this is the text response to 'hello'
tts = gTTS(text=speech, lang='en') # convert above written text into speech
tts.save('D:\\My Data\\Documents\\Cyborg\\hello.mp3')# save speech in mp3 (change to your file location)
mixer.init() # start mixer to play mp3
mixer.music.load('D:\\My Data\\Documents\\Cyborg\\hello.mp3') # load file
mixer.music.play() # play file
#condition to respond to "good morning"
if 'good morning' in message:
speech = ('Good Morning, how are you')
tts = gTTS(text=speech, lang='en')
tts.save('D:\\My Data\\Documents\\Cyborg\\morning_greeting.mp3')
mixer.init()
mixer.music.load('D:\\My Data\\Documents\\Cyborg\\morning_greeting.mp3')
mixer.music.play()
except Exception as e: # throw an exception or error
print("Could not understand") # when user speech is meaningless