aboutsummaryrefslogtreecommitdiff
path: root/src/argaze/GazeAnalysis/DeviationCircleCoverage.py
blob: 3d910c7afe8e75fb51d1a1dc0cc2220ad30b3f46 (plain)
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
"""Module for matching algorithm based on fixation's deviation circle coverage over AOI."""

"""
This program is free software: you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free Software
Foundation, either version 3 of the License, or (at your option) any later
version.
This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with
this program. If not, see <https://www.gnu.org/licenses/>.
"""

__author__ = "Théo de la Hogue"
__credits__ = []
__copyright__ = "Copyright 2023, Ecole Nationale de l'Aviation Civile (ENAC)"
__license__ = "GPLv3"

import cv2
import numpy

from argaze import GazeFeatures, DataFeatures
from argaze.AreaOfInterest import AOIFeatures
from argaze.GazeAnalysis import DispersionThresholdIdentification, VelocityThresholdIdentification


class AOIMatcher(GazeFeatures.AOIMatcher):
    """Matching algorithm based on fixation's deviation circle coverage over AOI."""

    @DataFeatures.PipelineStepInit
    def __init__(self, **kwargs):

        # Init AOIMatcher class
        super().__init__()

        self.__coverage_threshold = 0

        self.__reset()

    @property
    def coverage_threshold(self) -> float:
        """Minimal coverage ratio to consider a fixation over an AOI (1 means that whole fixation's deviation circle have to be over the AOI)."""
        return self.__coverage_threshold

    @coverage_threshold.setter
    def coverage_threshold(self, coverage_threshold: float):

        self.__coverage_threshold = coverage_threshold
    
    def __reset(self):

        self.__look_count = 0
        self.__looked_aoi_data = (None, None)
        self.__looked_probabilities = {}
        self.__circle_ratio_sum = {}
        self.__matched_gaze_movement = None
        self.__matched_region = None

    @DataFeatures.PipelineStepMethod
    def match(self, gaze_movement: GazeFeatures.GazeMovement, aoi_scene) -> 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():

                # DispersionThresholdIdentification.Fixation: use maximal deviation
                if issubclass(type(gaze_movement), DispersionThresholdIdentification.Fixation):

                    fixation_circle_radius = gaze_movement.deviation_max

                # VelocityThresholdIdentification.Fixation: use amplitude
                elif issubclass(type(gaze_movement), VelocityThresholdIdentification.Fixation):

                    fixation_circle_radius = gaze_movement.amplitude

                # Otherwise, compute maximal deviation
                else:

                    fixation_circle_radius = max(gaze_movement.distances(gaze_movement.focus))

                # Intersect
                region, _, circle_ratio = aoi.circle_intersection(gaze_movement.focus, fixation_circle_radius)

                if name not in self.exclude and circle_ratio > self.__coverage_threshold:

                    # 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
                # noinspection PyAttributeOutsideInit
                self.__looked_aoi_data = most_likely_looked_aoi_data

                # Calculate circle ratio means as looked probabilities
                # noinspection PyAttributeOutsideInit
                self.__looked_probabilities = {}

                for aoi_name, circle_ratio_sum in self.__circle_ratio_sum.items():

                    circle_ratio_mean = circle_ratio_sum / self.__look_count

                    # Avoid probability greater than 1
                    self.__looked_probabilities[aoi_name] = circle_ratio_mean if circle_ratio_mean < 1 else 1

                # Update matched gaze movement
                # noinspection PyAttributeOutsideInit
                self.__matched_gaze_movement = gaze_movement

                # Update matched region
                # noinspection PyAttributeOutsideInit
                self.__matched_region = matched_region

                # Return
                return self.__looked_aoi_data

        elif GazeFeatures.is_saccade(gaze_movement):

            self.__reset()

        elif not gaze_movement:

            self.__reset()            

        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 on the loaded
                gaze movement identifier module, 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)
            update_looked_aoi:
            looked_aoi_name_color: color of text (if None, no looked aoi name is drawn)
            looked_aoi_name_offset: offset 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:

                            # noinspection PyAttributeOutsideInit
                            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)

    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]

    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]

    def looked_probabilities(self) -> dict:
        """Get probabilities to be looked by current fixation for each aoi.

        !!! note 
            aoi where fixation deviation circle never passed the coverage threshold will be missing.
        """

        return self.__looked_probabilities