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
|
#!/usr/bin/env python
""" """
__author__ = "Théo de la Hogue"
__credits__ = []
__copyright__ = "Copyright 2023, Ecole Nationale de l'Aviation Civile (ENAC)"
__license__ = "BSD"
from typing import TypeVar, Tuple
from argaze import DataStructures
from argaze.AreaOfInterest import AOIFeatures
from argaze import GazeFeatures
import cv2
import numpy
AOI2DSceneType = TypeVar('AOI2DScene', bound="AOI2DScene")
# Type definition for type annotation convenience
class AOI2DScene(AOIFeatures.AOIScene):
"""Define AOI 2D scene."""
def __init__(self, aois_2d: dict = None):
super().__init__(2, aois_2d)
def draw(self, image: numpy.array, exclude=[], color=(0, 255, 255)):
"""Draw AOI polygons on image."""
for name, aoi in self.items():
if name in exclude:
continue
aoi.draw(image, color)
def raycast(self, pointer:tuple) -> Tuple[str, "AOIFeatures.AreaOfInterest", bool]:
"""Iterate over aoi to know which aoi is matching the given pointer position.
Returns:
aoi name
aoi object
matching status
"""
for name, aoi in self.items():
matching = aoi.contains_point(pointer)
yield name, aoi, matching
def draw_raycast(self, image: numpy.array, pointer:tuple, exclude=[], base_color=(0, 0, 255), matching_color=(0, 255, 0)):
"""Draw AOIs with their matching status."""
for name, aoi, matching in self.raycast(pointer):
if name in exclude:
continue
color = matching_color if matching else base_color
if matching:
top_left_corner_pixel = numpy.rint(aoi.clockwise()[0]).astype(int)
cv2.putText(image, name, top_left_corner_pixel, cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255), 1, cv2.LINE_AA)
# Draw form
aoi.draw(image, color)
def circlecast(self, center:tuple, radius:float) -> Tuple[str, "AOIFeatures.AreaOfInterest", numpy.array, float, float]:
"""Iterate over areas to know which aoi is matching circle.
Returns:
aoi name
aoi object
matching region points
ratio of matching region area relatively to aoi area
ratio of matching region area relatively to circle area
"""
for name, aoi in self.items():
matching_region, aoi_ratio, circle_ratio = aoi.circle_intersection(center, radius)
yield name, aoi, matching_region, aoi_ratio, circle_ratio
def draw_circlecast(self, image: numpy.array, center:tuple, radius:float, matching_aoi = [], exclude=[], base_color=(0, 0, 255), matching_color=(0, 255, 0)):
"""Draw AOIs with their matching status and matching region."""
for name, aoi, matching_region, aoi_ratio, circle_ratio in self.circlecast(center, radius):
if name in exclude:
continue
color = base_color
# Draw matching region
if aoi_ratio > 0:
matching_region.draw(image, color, 4)
# Is aoi part of matching aoi?
if name in matching_aoi:
color = matching_color
top_left_corner_pixel = numpy.rint(aoi.clockwise()[0]).astype(int)
cv2.putText(image, name, top_left_corner_pixel, cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255), 1, cv2.LINE_AA)
# Draw matching region
matching_region.draw(image, matching_color, 4)
# Draw form
aoi.draw(image, color)
def reframe(self, aoi_name: str, size: tuple) -> AOI2DSceneType:
"""
Reframe whole scene to a scene bounded by an AOI.
Parameters:
aoi: name of AOI used to reframe scene
Returns:
reframed AOI 2D scene
"""
assert(self[aoi_name].points_number == 4)
# Edit affine transformation (M) allowing to transform source axis (Src) into destination axis (Dst)
Src = self[aoi_name].clockwise().astype(numpy.float32)
Src_origin = Src[0]
Src = Src - Src_origin
Dst = numpy.float32([[0, 0], [size[0], 0], [size[0], size[1]], [0, size[1]]])
M = cv2.getAffineTransform(Src[:3], Dst[:3])[:, :2]
# Apply affine transformationto each AOI
aoi2D_scene = AOI2DScene()
for name, aoi2D in self.items():
aoi2D_scene[name] = numpy.matmul(aoi2D - Src_origin, M.T)
return aoi2D_scene
|