-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvoice.py
57 lines (44 loc) · 1.5 KB
/
voice.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
56
57
import speech_recognition as sr
import pyttsx3
# Initialize the recognizer
r = sr.Recognizer()
# Initialize the engine
engine = pyttsx3.init()
# Define a function to speak
def speak(text):
engine.say(text)
engine.runAndWait()
# Define a function to recognize speech
def recognize_speech():
with sr.Microphone() as source:
# Adjust for ambient noise
r.adjust_for_ambient_noise(source)
print("Speak now...")
audio = r.listen(source)
try:
# Use Google Speech Recognition to transcribe audio
text = r.recognize_google(audio)
print("You said: " + text)
return text
except sr.UnknownValueError:
print("Could not understand audio")
except sr.RequestError as e:
print("Could not request results; {0}".format(e))
# Define your assistant's commands
def process_command(command):
if "hello" in command.lower():
speak("Hello! How can I help you?")
elif "how are you" in command.lower():
speak("I'm doing well, thank you for asking!")
elif "what time is it" in command.lower():
speak("It's currently [insert current time here].")
elif "stop" in command.lower():
speak("Goodbye!")
exit()
else:
speak("Sorry, I didn't understand that.")
# Start the assistant
speak("Hello! How can I help you?")
while True:
command = recognize_speech()
process_command(command)