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
|
#!/usr/bin/env python
"""Module for matching algorithm based on fixation's deviation circle coverage over AOI.
"""
__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 dataclasses import dataclass, field
import math
from argaze import GazeFeatures
from argaze.AreaOfInterest import AOIFeatures
import numpy
import cv2
GazeMovementType = TypeVar('GazeMovement', bound="GazeMovement")
# Type definition for type annotation convenience
@dataclass
class AOIMatcher(GazeFeatures.AOIMatcher):
"""Matching algorithm based on fixation's deviation circle coverage over AOI."""
coverage_threshold: float = field(default = 0.)
"""Minimal coverage ratio to consider a fixation over an AOI (1 means that whole fixation's deviation circle have to be over the AOI)."""
def __post_init__(self):
"""Init looked aoi data."""
self.__look_count = 0
self.__looked_aoi_data = (None, None)
self.__circle_ratio_sum = {}
self.__aoi_coverages = {}
self.__matched_gaze_movement = None
self.__matched_region = None
def match(self, aoi_scene, gaze_movement) -> Tuple[str, AOIFeatures.AreaOfInterest]:
"""Returns AOI with the maximal fixation's deviation circle coverage if above coverage threshold."""
if GazeFeatures.is_fixation(gaze_movement):
self.__look_count += 1
max_coverage = 0.
most_likely_looked_aoi_data = (None, None)
matched_region = None
for name, aoi in aoi_scene.items():
# BAD: we use deviation_max attribute which is an attribute of DispersionThresholdIdentification.Fixation class
region, _, circle_ratio = aoi.circle_intersection(gaze_movement.focus, gaze_movement.deviation_max)
if name not in self.exclude and circle_ratio > 0:
# Sum circle ratio to update aoi coverage
try:
self.__circle_ratio_sum[name] += circle_ratio
except KeyError:
self.__circle_ratio_sum[name] = circle_ratio
# Update maximal coverage and most likely looked aoi
if self.__circle_ratio_sum[name] > max_coverage:
max_coverage = self.__circle_ratio_sum[name]
most_likely_looked_aoi_data = (name, aoi)
matched_region = region
# Check that aoi coverage happens
if max_coverage > 0:
# Update looked aoi data
self.__looked_aoi_data = most_likely_looked_aoi_data
# Calculate looked aoi circle ratio means
self.__aoi_coverages = {}
for aoi_name, circle_ratio_sum in self.__circle_ratio_sum.items():
circle_ratio_mean = circle_ratio_sum / self.__look_count
# filter circle ration mean greater than 1
self.__aoi_coverages[aoi_name] = circle_ratio_mean if circle_ratio_mean < 1 else 1
# Update matched gaze movement
self.__matched_gaze_movement = gaze_movement
# Update matched region
self.__matched_region = matched_region
# Return
if self.__aoi_coverages[most_likely_looked_aoi_data[0]] > self.coverage_threshold:
return self.__looked_aoi_data
elif GazeFeatures.is_saccade(gaze_movement):
self.__post_init__()
elif not gaze_movement.valid:
self.__post_init__()
return (None, None)
def draw(self, image: numpy.array, aoi_scene: AOIFeatures.AOIScene, draw_matched_fixation: dict = None, draw_matched_region: dict = None, draw_looked_aoi: dict = None, update_looked_aoi: bool = False, looked_aoi_name_color: tuple = None, looked_aoi_name_offset: tuple = (0, 0)):
"""Draw matching into image.
Parameters:
image: where to draw
aoi_scene: to refresh looked aoi if required
draw_matched_fixation: Fixation.draw parameters (which depends of the loaded gaze movement identifier module, if None, no fixation is drawn)
draw_matched_fixation_positions: GazeMovement.draw_positions parameters (if None, no fixation is drawn)
draw_matched_region: AOIFeatures.AOI.draw parameters (if None, no matched region is drawn)
draw_looked_aoi: AOIFeatures.AOI.draw parameters (if None, no looked aoi is drawn)
looked_aoi_name_color: color of text (if None, no looked aoi name is drawn)
looked_aoi_name_offset: ofset of text from the upper left aoi bounding box corner
"""
if self.__matched_gaze_movement is not None:
if GazeFeatures.is_fixation(self.__matched_gaze_movement):
# Draw matched fixation if required
if draw_matched_fixation is not None:
self.__matched_gaze_movement.draw(image, **draw_matched_fixation)
# Draw matched aoi
if self.looked_aoi.all() is not None:
if update_looked_aoi:
try:
self.__looked_aoi_data = (self.looked_aoi_name, aoi_scene[self.looked_aoi_name])
except KeyError:
pass
# Draw looked aoi if required
if draw_looked_aoi is not None:
self.looked_aoi.draw(image, **draw_looked_aoi)
# Draw matched region if required
if draw_matched_region is not None:
self.__matched_region.draw(image, **draw_matched_region)
# Draw looked aoi name if required
if looked_aoi_name_color is not None:
top_left_corner_pixel = numpy.rint(self.looked_aoi.bounding_box[0]).astype(int) + looked_aoi_name_offset
cv2.putText(image, self.looked_aoi_name, top_left_corner_pixel, cv2.FONT_HERSHEY_SIMPLEX, 1, looked_aoi_name_color, 1, cv2.LINE_AA)
@property
def looked_aoi(self) -> AOIFeatures.AreaOfInterest:
"""Get most likely looked aoi for current fixation (e.g. the aoi with the highest coverage mean value)"""
return self.__looked_aoi_data[1]
@property
def looked_aoi_name(self) -> str:
"""Get most likely looked aoi name for current fixation (e.g. the aoi with the highest coverage mean value)"""
return self.__looked_aoi_data[0]
@property
def aoi_coverages(self) -> dict:
"""Get all aoi coverage means for current fixation.
It represents the ratio of fixation deviation circle surface that used to cover the aoi."""
return self.__aoi_coverages
|