-
Notifications
You must be signed in to change notification settings - Fork 2
/
Mash991.py
76 lines (62 loc) · 2.49 KB
/
Mash991.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import speech_recognition as sr
import pyttsx3
# Initialize the recognizer
r = sr.Recognizer()
# Initialize the text-to-speech engine
engine = pyttsx3.init()
# Function to perform speech recognition
def recognize_speech():
# Use the default microphone as the audio source
with sr.Microphone() as source:
print("Listening...")
# Adjust for ambient noise levels
r.adjust_for_ambient_noise(source)
# Capture the audio
audio = r.listen(source)
print("Recognizing...")
try:
# Convert speech to text
text = r.recognize_google(audio)
return text
except sr.UnknownValueError:
print("Speech recognition could not understand audio.")
except sr.RequestError as e:
print("Could not request results from Google Speech Recognition service; {0}".format(e))
# If an error occurred or no speech was recognized, return an empty string
return ""
# Function to generate speech from text
def speak_text(text):
engine.say(text)
engine.runAndWait()
# Main function
def main():
running = False
while True:
if not running:
speak_text("Hello! I can perform math calculations. Say 'math' for math calculations or 'speech' for regular speech processing.")
user_input = recognize_speech()
if user_input.lower() == "1":
running = True
speak_text("Sure! You can provide a mathematical expression.")
elif user_input.lower() == "speech":
running = False
speak_text("Alright! I will continue processing speech input.")
else:
speak_text("Sorry, I didn't understand. Please say 'math' or 'speech'.")
else:
speak_text("Please provide a mathematical expression or say 'stop' to pause math calculations.")
user_input = recognize_speech()
if user_input.lower() == "stop":
running = False
speak_text("Paused math calculations.")
else:
try:
result = eval(user_input)
response = "The result is: " + str(result)
except Exception:
response = "Sorry, I couldn't compute the expression."
print(response)
speak_text(response)
# Run the main function
if __name__ == "__main__":
main()