-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
110 lines (96 loc) · 3.7 KB
/
main.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
from gpiozero import Button
import subprocess
import time
import pyttsx3
import socket
# Initialize Text to Speech engine
engine = pyttsx3.init()
# Define button pins
button1 = Button(26)
button2 = Button(19)
button3 = Button(21)
button4 = Button(24)
button5 = Button(27)
# Define commands corresponding to buttons
commands = {
button1: ("/home/sightsense/Desktop/sys-env/bin/python", "/home/sightsense/Desktop/codeBase/ocr.py"),
button2: ("/home/sightsense/Desktop/sys-env/bin/python", "/home/sightsense/Desktop/codeBase/obj.py"),
button3: ("/home/sightsense/Desktop/sys-env/bin/python", "/home/sightsense/Desktop/codeBase/jarviseng.py"),
button4: ("/home/sightsense/Desktop/sys-env/bin/python", "/home/sightsense/Desktop/codeBase/obs/tf-lite-object_det/test.py"),
button5: ("/home/sightsense/Desktop/sys-env/bin/python", "/home/sightsense/Desktop/codeBase/jarviseng.py")
}
# Function to run command when a button is pressed
def run_command(command):
subprocess.run(command, shell=False) # Using shell=False to prevent command injection
# Function to say the given message
def speak(message):
print(message)
engine.say(message)
engine.runAndWait()
# Function to check internet connectivity
def check_internet():
try:
# Check if it's possible to resolve a known external domain name
host = socket.gethostbyname("www.google.com")
# If successful, return True (internet connection exists)
return True
except socket.error:
# If an error occurs, return False (no internet connection)
return False
# Function to handle button press event with debounce
def handle_button_press(button):
# Implementing debounce
time.sleep(0.1) # Wait for 200ms
if button.is_pressed:
if button == button1:
message = "OCR is activated"
elif button == button2:
message = "Object Detection is activated"
elif button == button3:
message = "AI Assistant is activated"
elif button == button4:
message = "Obstacle Detection is activated"
elif button == button5:
message = "Ai Assistant is activated, Start speaking in the next 5 seconds"
else:
message = " "
print(message)
engine.say(message)
engine.runAndWait()
run_command(commands[button])
# Assign the corresponding command to each button press event
button1.when_pressed = lambda: handle_button_press(button1)
button2.when_pressed = lambda: handle_button_press(button2)
button3.when_pressed = lambda: handle_button_press(button3)
button4.when_pressed = lambda: handle_button_press(button4)
button5.when_pressed = lambda: handle_button_press(button5)
try:
# Say "system has booted"
speak("System has booted")
# Check for internet connectivity
internet_connected = check_internet()
if internet_connected:
speak("Network connected")
else:
# Wait for 2 minutes for network connection
time.sleep(60)
internet_connected = check_internet()
if not internet_connected:
speak("Please connect to the internet")
# Further wait for another minute
time.sleep(10)
internet_connected = check_internet()
if not internet_connected:
speak("Please connect to the internet")
# Keep the script running
while True:
time.sleep(0.1) # Adjust the sleep time as needed
except KeyboardInterrupt:
# Clean up GPIO resources
pass
finally:
button1.close()
button2.close()
button3.close()
button4.close()
button5.close()