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
296
297
298
299
300
301
302
303
304
305
306
307
|
#!/usr/bin/env python
from typing import TypeVar, Tuple
import json
from argaze import DataStructures
import cv2 as cv
import matplotlib.path as mpath
import numpy
from shapely.geometry import Polygon
from shapely.geometry.point import Point
AreaOfInterestType = TypeVar('AreaOfInterest', bound="AreaOfInterest")
# Type definition for type annotation convenience
class AreaOfInterest(numpy.ndarray):
"""Define Area Of Interest as an array of points of any dimension."""
def __new__(cls, points: numpy.ndarray) -> AreaOfInterestType:
"""View casting inheritance."""
return numpy.array(points).view(AreaOfInterest)
def __repr__(self):
"""String representation"""
return repr(self.tolist())
def __str__(self):
"""String display"""
return repr(self.tolist())
@property
def dimension(self) -> int:
"""Number of axis coding area points positions."""
return self.shape[1]
@property
def size(self) -> int:
"""Number of points defining the area."""
return self.shape[0]
@property
def center(self) -> numpy.array:
"""Center of mass."""
return self.mean(axis=0)
@property
def bounding_box(self) -> numpy.array:
"""Get area's bounding box.
.. warning::
Available for 2D AOI only."""
assert(self.size > 1)
assert(self.dimension == 2)
min_x, min_y = numpy.min(self, axis=0)
max_x, max_y = numpy.max(self, axis=0)
return numpy.array([(min_x, min_y), (max_x, min_y), (max_x, max_y), (min_x, max_y)])
def clockwise(self) -> AreaOfInterestType:
"""Get area points in clockwise order.
.. warning::
Available for 2D AOI only."""
assert(self.dimension == 2)
O = self.center
OP = (self - O) / numpy.linalg.norm(self - O)
angles = numpy.arctan2(OP[:, 1], OP[:, 0])
return self[numpy.argsort(angles)]
def contains_point(self, point: tuple) -> bool:
"""Is a point inside area?
.. warning::
Available for 2D AOI only.
.. danger::
The AOI points must be sorted in clockwise order."""
assert(self.dimension == 2)
assert(len(point) == self.dimension)
return mpath.Path(self).contains_points([point])[0]
def inner_axis(self, point: tuple) -> tuple:
"""Transform the coordinates from the global axis to the AOI's axis.
.. warning::
Available for 2D AOI only.
.. danger::
The AOI points must be sorted in clockwise order."""
assert(self.dimension == 2)
Src = self
Src_origin = Src[0]
Src = (Src - Src_origin).reshape((len(Src)), 2).astype(numpy.float32)
Dst = numpy.array([[0., 0.], [1., 0.], [1., 1.], [0., 1.]]).astype(numpy.float32)
P = cv.getPerspectiveTransform(Src, Dst)
X = numpy.append(numpy.array(numpy.array(point) - Src_origin), [1.0]).astype(numpy.float32)
Y = numpy.dot(P, X)
La = (Y/Y[2])[:-1]
return tuple(numpy.around(La, 4))
def outter_axis(self, point: tuple) -> tuple:
"""Transform the coordinates from the AOI's axis to the global axis.
.. warning::
Available for 2D AOI only.
.. danger::
The AOI points must be sorted in clockwise order."""
assert(self.dimension == 2)
Src = numpy.array([[0., 0.], [1., 0.], [1., 1.], [0., 1.]]).astype(numpy.float32)
Dst = self.astype(numpy.float32)
Dst_origin = Dst[0]
Dst = (Dst - Dst_origin).reshape((len(Dst)), 2)
P = cv.getPerspectiveTransform(Src, Dst)
X = numpy.array([point[0], point[1], 1.0]).astype(numpy.float32)
Y = numpy.dot(P, X)
Lp = Dst_origin + (Y/Y[2])[:-1]
return tuple(numpy.rint(Lp).astype(int))
def circle_intersection(self, center: tuple, radius: float) -> Tuple[numpy.array, float, float]:
"""Get intersection shape with a circle, intersection area / AOI area ration and intersection area / circle area ration.
.. warning::
Available for 2D AOI only."""
assert(self.dimension == 2)
self_polygon = Polygon(self)
args_circle = Point(center).buffer(radius)
if self_polygon.intersects(args_circle):
intersection = self_polygon.intersection(args_circle)
intersection_array = numpy.array([list(xy) for xy in intersection.exterior.coords[:]]).astype(numpy.float32).view(AreaOfInterest)
return intersection_array, intersection.area / self_polygon.area, intersection.area / args_circle.area
else:
empty_array = numpy.array([list([])]).astype(numpy.float32).view(AreaOfInterest)
return empty_array, 0., 0.
def draw(self, frame, color, border_size=1):
"""Draw 2D AOI into frame.
.. warning::
Available for 2D AOI only."""
assert(self.dimension == 2)
if len(self) > 1:
# Draw form
pixels = numpy.rint(self).astype(int)
cv.line(frame, pixels[-1], pixels[0], color, border_size)
for A, B in zip(pixels, pixels[1:]):
cv.line(frame, A, B, color, border_size)
# Draw center
center_pixel = numpy.rint(self.center).astype(int)
cv.circle(frame, center_pixel, 1, color, -1)
AOISceneType = TypeVar('AOIScene', bound="AOIScene")
# Type definition for type annotation convenience
class AOIScene():
"""Define AOI scene as a dictionary of AOI."""
def __init__(self, dimension: int, areas: dict = None):
"""Initialisation."""
assert(dimension > 0)
self.__dimension = dimension
# NEVER USE {} as default function argument
if areas == None:
self.__areas = {}
else:
self.__areas = areas
def __getitem__(self, name) -> AreaOfInterest:
"""Get an AOI from the scene."""
return AreaOfInterest(self.__areas[name]) #.astype(numpy.float32).view(AreaOfInterest)
def __setitem__(self, name, aoi: AreaOfInterest):
"""Add an AOI to the scene."""
assert(aoi.dimension == self.__dimension)
self.__areas[name] = AreaOfInterest(aoi) #.tolist()
def __delitem__(self, key):
"""Remove an AOI from the scene."""
del self.__areas[key]
def __repr__(self):
"""String representation"""
return str(self.__areas)
def __str__(self) -> str:
"""String display"""
output = ''
for name, area in self.__areas.items():
output += f'\n\t{name}:\n{area}'
return output
def items(self) -> Tuple[str, AreaOfInterest]:
"""Iterate over areas."""
return self.__areas.items()
#for name, area in self.__areas.items():
#yield name, AreaOfInterest(area) #.astype(numpy.float32).view(AreaOfInterest)
def keys(self) -> list[str]:
"""Get areas name."""
return self.__areas.keys()
@property
def dimension(self) -> int:
"""Dimension of the AOIs in scene."""
return self.__dimension
@property
def bounds(self) -> numpy.array:
"""Get scene's bounds."""
all_vertices = []
for area in self.__areas.values():
for vertice in area:
all_vertices.append(vertice)
all_vertices = numpy.array(all_vertices) #.astype(numpy.float32)
min_bounds = numpy.min(all_vertices, axis=0)
max_bounds = numpy.max(all_vertices, axis=0)
return numpy.array([min_bounds, max_bounds])
@property
def center(self) -> numpy.array:
"""Get scene's center point."""
min_bounds, max_bounds = self.bounds
return (min_bounds + max_bounds) / 2
@property
def size(self) -> numpy.array:
"""Get scene size."""
min_bounds, max_bounds = self.bounds
return max_bounds - min_bounds
def copy(self, exclude=[]) -> AOISceneType:
"""Copy scene partly excluding AOI by name."""
scene_copy = type(self)()
for name, area in self.__areas.items():
if name not in exclude:
scene_copy[name] = AreaOfInterest(area) #.astype(numpy.float32).view(AreaOfInterest)
return scene_copy
class TimeStampedAOIScenes(DataStructures.TimeStampedBuffer):
"""Define timestamped buffer to store AOI scenes in time."""
def __setitem__(self, ts, scene):
"""Force value to inherit from AOIScene."""
assert(type(scene).__bases__[0] == AOIScene)
super().__setitem__(ts, scene)
|