-
Notifications
You must be signed in to change notification settings - Fork 0
/
unihiker_trafficlight.py
87 lines (65 loc) · 2.7 KB
/
unihiker_trafficlight.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
# Unihiker board Traffic Light
# Roni Bandini, October 2023
# Buenos Aires, Argentina
import time
from pinpong.board import *
from pinpong.extension.unihiker import *
from unihiker import GUI
from unihiker import Audio
import subprocess
import requests
gui = GUI()
Board().begin()
print("No helmet no green - Traffic Light Module")
print("@RoniBandini - October 2023")
print("")
currentLight="green"
waitTime=6
detectionLimit=0.5
noHelmet=0
serverUrl='https://YourServer/YourFolder/'
def updateTrafficLight():
global currentLight
gui.clear()
noHelmet=0
if currentLight=="green":
print("Yellow")
currentLight="yellow"
img = gui.draw_image(x=0, y=0, w=240, h=320, image='/home/traffic/images/yellow.png')
gui.draw_text(x = 120,y=260,text="Missing helmet: "+str(int(noHelmet*100))+" %", font_size=10, color="black", origin='top')
time.sleep(waitTime/2)
gui.clear()
print("Red light")
currentLight="red"
img = gui.draw_image(x=0, y=0, w=240, h=320, image='/home/traffic/images/red.png')
gui.draw_text(x = 120,y=260,text="Missing helmet: "+str(int(noHelmet*100))+" %", font_size=10, color="black", origin='top')
time.sleep(waitTime)
elif currentLight=="red":
# read helmet detection rate from server
print("Getting inference from server...")
r = requests.get(serverUrl+'helmet.ini')
noHelmet=r.json()
print("There are "+str(noHelmet*100)+"% of a rider not wearing a helmet")
if float(noHelmet)>detectionLimit:
img = gui.draw_image(x=0, y=0, w=240, h=320, image='/home/traffic/images/nohelmet.png')
# loop until helmet is on
while float(noHelmet)>detectionLimit:
r = requests.get(serverUrl+'helmet.ini')
noHelmet=r.json()
print("There are "+str(noHelmet*100)+"% of a rider not wearing a helmet")
time.sleep(3)
print("Yellow light")
currentLight="yellow"
img = gui.draw_image(x=0, y=0, w=240, h=320, image='/home/traffic/images/yellow.png')
gui.draw_text(x = 120,y=260,text="Missing helmet: "+str(int(noHelmet*100))+" %", font_size=10, color="black", origin='top')
time.sleep(waitTime/2)
gui.clear()
print("Green light")
currentLight="green"
img = gui.draw_image(x=0, y=0, w=240, h=320, image='/home/traffic/images/green.jpg')
gui.draw_text(x = 120,y=260,text="Missing helmet: "+str(int(noHelmet*100))+"%", font_size=10, color="black", origin='top')
time.sleep(waitTime)
# main loop
while True:
updateTrafficLight()
time.sleep(1)