-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
executable file
·121 lines (96 loc) · 4.1 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
111
112
113
114
115
116
117
118
119
120
121
import cv2
import numpy as np
import sys
import time
from logo import display_logo
from gps_helper import get_current_position
from circle_color_detector import CircleColorDetector
import colors
detector_circle_radius = 95
color_detector = CircleColorDetector(colors.colors)
def cardinal_direction(latitude, longitude):
if latitude > 0:
lat_direction = 'N'
else:
lat_direction = 'S'
if longitude > 0:
long_direction = 'E'
else:
long_direction = 'W'
return f"{lat_direction}{long_direction}"
def draw_close_button(frame):
cv2.rectangle(frame, (frame.shape[1] - 100, 10), (frame.shape[1] - 20, 30), (0, 0, 255), -1)
cv2.putText(frame, "X", (frame.shape[1] - 70, 28), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255), 1, cv2.LINE_AA)
def click_event(event, x, y, flags, param):
global data_displayed, start_time, last_click_x, last_click_y, top_right_clicks
if event == cv2.EVENT_LBUTTONDOWN:
if x > frame.shape[1] - 100 and y < 30:
top_right_clicks += 1
if top_right_clicks == 3:
cv2.destroyAllWindows()
sys.exit()
else:
top_right_clicks = 0
data_displayed = True
start_time = time.time()
last_click_x = x
last_click_y = y
cap = cv2.VideoCapture(0)
window_name = 'frame'
cv2.namedWindow(window_name, cv2.WND_PROP_FULLSCREEN)
cv2.setWindowProperty(window_name, cv2.WND_PROP_FULLSCREEN, cv2.WINDOW_FULLSCREEN)
logo_path = 'logo.png'
display_logo(window_name, logo_path, cap)
data_displayed = False
start_time = None
last_click_x = None
last_click_y = None
top_right_clicks = 0
while True:
ret, frame = cap.read()
if not ret:
break
if data_displayed:
elapsed_time = time.time() - start_time
if elapsed_time > 3:
data_displayed = False
original_frame = frame.copy()
draw_close_button(frame)
if data_displayed and last_click_x is not None and last_click_y is not None:
x, y = last_click_x, last_click_y
color_name, color_rgb = color_detector.detect_color(original_frame, (x, y), detector_circle_radius)
if color_name is not None:
center_x = frame.shape[1] // 2
color_text_size, _ = cv2.getTextSize(color_name, cv2.FONT_HERSHEY_COMPLEX, 3.75, 6)
# Draw color name with a fat black border
for i in range(-6, 7):
for j in range(-6, 7):
cv2.putText(frame, color_name,
(center_x - color_text_size[0] // 2 + i, color_text_size[1] + 20 + j),
cv2.FONT_HERSHEY_COMPLEX, 3.75, (0, 0, 0), 5, cv2.LINE_AA)
cv2.putText(frame, color_name, (center_x - color_text_size[0] // 2, color_text_size[1] + 20),
cv2.FONT_HERSHEY_COMPLEX, 3.75, (255, 255, 255), 5, cv2.LINE_AA)
lat, long = get_current_position()
if lat is not None and long is not None:
cardinal_dir = cardinal_direction(lat, long)
else:
cardinal_dir = "OFF"
gps_info = f"{cardinal_dir} Wall"
gps_text_size, _ = cv2.getTextSize(gps_info, cv2.FONT_HERSHEY_COMPLEX, 1.75, 3)
# Draw GPS info with a fat black border
for i in range(-4, 5):
for j in range(-4, 5):
cv2.putText(frame, gps_info,
(int(frame.shape[1] / 3 - gps_text_size[0] / 3) + i,
frame.shape[0] - gps_text_size[1] - 10 + j),
cv2.FONT_HERSHEY_COMPLEX, 2.75, (0, 0, 0), 3, cv2.LINE_AA)
cv2.putText(frame, gps_info,
(int(frame.shape[1] / 3 - gps_text_size[0] / 3), frame.shape[0] - gps_text_size[1] - 10),
cv2.FONT_HERSHEY_COMPLEX, 2.75, (255, 255, 255), 3, cv2.LINE_AA)
cv2.circle(frame, (x, y), detector_circle_radius, (0, 255, 0), 2)
cv2.imshow('frame', frame)
cv2.setMouseCallback('frame', click_event)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()