aboutsummaryrefslogtreecommitdiff
path: root/src/argaze/GazeAnalysis/FocusPointInside.py
blob: 361ea7517d593bbc046b8ba759cc938bebdf2380 (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
"""Module for matching algorithm based on fixation's focus point


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


class AOIMatcher(GazeFeatures.AOIMatcher):
    """Matching algorithm based on fixation's focus point."""

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

        # Init AOIMatcher class
        super().__init__()

        self.__looked_aoi_data = (None, None)
        self.__matched_gaze_movement = None

    def __reset(self):

        self.__looked_aoi_data = (None, None)
        self.__matched_gaze_movement = None

    @DataFeatures.PipelineStepMethod
    def match(self, gaze_movement: GazeFeatures.GazeMovement, aoi_scene) -> tuple[str, AOIFeatures.AreaOfInterest]:
        """Returns AOI containing fixation focus point."""

        if GazeFeatures.is_fixation(gaze_movement):

            for name, aoi in aoi_scene.items():

                if name not in self.exclude and aoi.contains_point(gaze_movement.focus):

                    # Update looked aoi data
                    self.__looked_aoi_data = (name, aoi)

                    # Update matched gaze movement
                    self.__matched_gaze_movement = gaze_movement

                    return self.__looked_aoi_data

        elif GazeFeatures.is_saccade(gaze_movement):

            self.__reset()

        return (None, None)

    def draw(self, image: numpy.array, aoi_scene: AOIFeatures.AOIScene, draw_matched_fixation: dict = None,
             draw_looked_aoi: dict = None, 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_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: 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:

                    # Draw looked aoi if required
                    if draw_looked_aoi is not None:

                        self.looked_aoi().draw(image, **draw_looked_aoi)

                    # 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."""

        return self.__looked_aoi_data[1]

    def looked_aoi_name(self) -> str:
        """Get most likely looked aoi name for current fixation."""

        return self.__looked_aoi_data[0]