-
Notifications
You must be signed in to change notification settings - Fork 0
/
traffic_light.py
89 lines (74 loc) · 2.04 KB
/
traffic_light.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
import cv2
import numpy as np
import time
# Create blank background image
img = np.zeros((700, 900, 3), np.uint8)
# Define traffic light colors
green = (0, 255, 0)
yellow = (0, 255, 255)
red = (0, 0, 255)
red_us = (34, 34, 162)
yellow_us = (81, 165, 165)
green_us = (40, 151, 40)
# Initial color is green
color = green
# Set countdown timer
x = 5 #xanh
y = 3 #vang
z = x + y #do
cor_x = 250
cor_y = 570
timer = x
while True:
# Draw colored circle for traffic light
cv2.circle(img,(250, 130), 100, red_us, -1)
cv2.circle(img,(250, 350), 100, yellow_us, -1)
cv2.circle(img,(250, 570), 100, green, -1)
# Display countdown text
cv2.putText(img, str(timer), (cor_x, cor_y), cv2.FONT_HERSHEY_SIMPLEX, 2, (255,255,255), 3)
# Display image
cv2.imshow('Traffic light', img)
# Count down each second
if timer > 0:
timer -= 1
time.sleep(1)
# Change light color
# if timer == 0 and color == green:
# timer = y
# yellow_us = yellow
# green = green_us
# cor_y = 350
# elif timer == 0 and color == yellow:
# timer = z
# red_us = red
# yellow = yellow_us
# cor_y = 570
# elif timer == 0 and color == red:
# timer = x
# red = red_us
# green_us = green
# cor_y = 130
if timer == 0:
if color == green: #yellow
timer = y
color = yellow
yellow_us = yellow
cor_y = 350
green = green_us
elif color == yellow: #red
timer = z
color = red
red_us = red
cor_y = 130
yellow = yellow_us
elif color == red: #green
timer = x
color = green
green_us = green
red = red_us
cor_y = 570
# Check for ESC key to exit
k = cv2.waitKey(30) & 0xff
if k == 27:
break
cv2.destroyAllWindows()