-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutills.py
295 lines (193 loc) · 7.27 KB
/
utills.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
import math
from dataclasses import dataclass
from functools import cached_property
from typing import Sequence, Self, Optional
import numpy as np
import cv2
DEGREES_X_MIN, DEGREES_X_MAX = (30, 150)
DEGREES_Y_MIN, DEGREES_Y_MAX = (-28, 10)
Y_PIXEL_TO_DEGREE_NORM_CONST = 11
X_PIXEL_TO_DEGREE_NORM_CONST = 13
@dataclass
class DegVector:
x: int = 90
y: int = 0
def distance(self, other: Self):
dx2 = (self.x - other.x) ** 2
dy2 = (self.y - other.y) ** 2
return int(math.sqrt(dx2 + dy2))
def as_tuple(self):
return self.x, self.y
def __eq__(self, other):
return self.x == other.x and self.y == other.y
def __sub__(self, other):
return DegVector(self.x - other.x, self.y - other.y)
def __add__(self, other):
return DegVector(self.x + other.x, self.y + other.y)
def __str__(self):
return self.as_tuple().__str__()
@property
def is_inside_border(self):
return DEGREES_X_MIN < self.x < DEGREES_X_MAX and DEGREES_Y_MIN < self.y < DEGREES_Y_MAX
@dataclass
class PixelVector:
x: int
y: int
perspective_point: Optional[DegVector] = None
def distance(self, other: Self):
dx2 = (self.x - other.x) ** 2
dy2 = (self.y - other.y) ** 2
return int(math.sqrt(dx2 + dy2))
def as_tuple(self):
return self.x, self.y
def __eq__(self, other):
return self.x == other.x and self.y == other.y
def __sub__(self, other):
return PixelVector(self.x - other.x, self.y - other.y)
def __add__(self, other):
return PixelVector(self.x + other.x, self.y + other.y)
def __str__(self):
return self.as_tuple().__str__()
@dataclass
class Contour:
frame_middle_point: PixelVector # Beam center
obj: tuple[Sequence[cv2.UMat], cv2.UMat]
x: int
y: int
w: int
h: int
area: int # pixels area
degree_point: Optional[DegVector] = None
def __init__(self, c, frame_middle_point=None, *args, **kwargs):
self.obj = c
self.area = int(cv2.contourArea(c))
self.x, self.y, self.w, self.h = cv2.boundingRect(c)
self.frame_middle_point = frame_middle_point
@cached_property
def center_point(self):
return PixelVector(x=int(self.x + self.w // 2), y=int(self.y + self.h // 2))
@cached_property
def distance_from_center(self):
return self.center_point.distance(self.frame_middle_point)
@cached_property
def top_left_point(self):
return PixelVector(x=self.x, y=self.y)
@cached_property
def bottom_right_point(self):
return PixelVector(x=self.x + self.w, y=self.y + self.h)
@cached_property
def direction_vector(self):
return PixelVector(x=self.frame_middle_point.x - self.center_point.x,
y=self.frame_middle_point.y - self.center_point.y)
def get_abs_degree_location(self, frame_degree):
y_degree_delta = self.direction_vector.y / Y_PIXEL_TO_DEGREE_NORM_CONST
x_degree_delta = self.direction_vector.x / X_PIXEL_TO_DEGREE_NORM_CONST
contour_degree_location = DegVector(x=int(frame_degree.x + x_degree_delta),
y=int(frame_degree.y + y_degree_delta))
return contour_degree_location
@property
def y_direction(self):
return 1 if self.direction_vector.y > 0 else -1
@property
def x_direction(self):
return 1 if self.direction_vector.x > 0 else -1
def detect_motion(frame, last_mean):
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
result = np.abs(np.mean(gray) - last_mean)
last_mean = np.mean(gray)
print(result)
if result > 0.3:
print("Motion detected!")
print("Started recording.")
return True, last_mean
return False, last_mean
BEAM_RADIUS = 42
MIN_AREA_TO_CONSIDER = 3
COLOR_RED = (0, 0, 255)
COLOR_GREEN = (0, 255, 0)
COLOR_WHITE = (255, 255, 255)
COLOR_BLACK = (0, 0, 0)
ARROW_THICKNESS = 2
CIRCLE_THICKNESS = 2
FULL_SHAPE_THICKNESS = -1
def draw_search_radius_circle(frame, radius):
if frame is None:
return frame
center_of_circle = (frame.shape[1] // 2, frame.shape[0] // 2)
cv2.circle(frame, center_of_circle, radius, COLOR_GREEN, CIRCLE_THICKNESS)
return frame
def draw_light_beam(frame):
if frame is None:
return frame
center_of_circle = (frame.shape[1] // 2, frame.shape[0] // 2)
cv2.circle(frame, center_of_circle, BEAM_RADIUS, COLOR_RED, CIRCLE_THICKNESS)
return frame
def draw_moving_contours(frame, contours):
if not contours:
return frame
for contour in contours:
x, y, w, h = cv2.boundingRect(contour.obj)
cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2)
return frame
def is_target_in_circle(frame, target_c: Contour):
if not target_c:
return False
mask_frame = np.zeros(frame.shape)
center_of_circle = (frame.shape[1] // 2, frame.shape[0] // 2)
cv2.circle(mask_frame, center_of_circle, BEAM_RADIUS, COLOR_WHITE, FULL_SHAPE_THICKNESS)
beam_mask_frame_sum = mask_frame.sum()
cv2.rectangle(mask_frame,
target_c.top_left_point.as_tuple(),
target_c.bottom_right_point.as_tuple(),
COLOR_BLACK, FULL_SHAPE_THICKNESS)
beam_and_target_frame_sum = mask_frame.sum()
return beam_mask_frame_sum != beam_and_target_frame_sum
def mark_target_contour(frame, center_point: DegVector, target_c: Contour):
if not target_c:
return frame
cv2.rectangle(frame, (target_c.x, target_c.y), (target_c.x + target_c.w, target_c.y + target_c.h), COLOR_BLACK, 2)
contour_center = target_c.center_point
cv2.arrowedLine(frame, center_point.as_tuple(), contour_center.as_tuple(),
COLOR_BLACK, ARROW_THICKNESS)
return frame
def plant_text_bottom(frame, text):
# setup text
font = cv2.FONT_HERSHEY_SIMPLEX
# get boundary of this text
text_size = cv2.getTextSize(text, font, 1, 2)[0]
# get coords based on boundary
text_x = (frame.shape[1] - text_size[0]) // 2
text_y = (frame.shape[0] + text_size[1]) * 4 // 5
# add text centered on image
cv2.putText(frame, text, (text_x, text_y), font, 1, (255, 255, 255), 2)
return frame
def draw_cam_direction_on_frame(sauron, x_delta, y_delta):
thermal_eye = sauron.thermal_eye
x = sauron.goal_deg_coordinate.x
y = sauron.goal_deg_coordinate.y
frame = thermal_eye.frame
text = f"Moving To ({x}, {y}). Directions:"
if x_delta > 0:
text += " Left"
if x_delta < 0:
text += " Right"
if y_delta > 0:
text += " UP"
if y_delta < 0:
text += " Down"
plant_text_bottom(frame, text)
def plant_state_name_in_frame(frame, state_name):
if frame is None or not state_name:
return frame
# setup text
font = cv2.FONT_HERSHEY_SIMPLEX
# get boundary of this text
text_size = cv2.getTextSize(state_name, font, 1, 2)[0]
# get coords based on boundary
textX = (frame.shape[1] - text_size[0]) // 2
textY = (frame.shape[0] + text_size[1]) // 5
# add text centered on image
cv2.putText(frame, state_name, (textX, textY), font, 1, (255, 255, 255), 2)
return frame
def get_value_within_limits(value, bottom, top):
return min(max(value, bottom), top)