aboutsummaryrefslogtreecommitdiff
path: root/src/argaze/GazeAnalysis/FocusPointInside.py
blob: 2ac4411a2e99c1c5e5216a77abcfa0b39510d79d (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
#!/usr/bin/env python

"""Module for matching algorithm based on fixation's focus point
"""

__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 focus point."""

    def __post_init__(self):
        """Init looked aoi data."""

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

    def match(self, aoi_scene, gaze_movement, exclude=[]) -> 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 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.__post_init__()

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

            if GazeFeatures.is_fixation(self.__matched_gaze_movement):

                # Draw matched fixation if required
                if draw_matched_fixation:

                    self.__matched_gaze_movement.draw(image, **draw_matched_fixation)
                
                # Draw matched fixation positions if required
                if draw_matched_fixation_positions:

                    self.__matched_gaze_movement.draw_positions(image, **draw_matched_fixation_positions)

                # Draw matched aoi
                if self.looked_aoi.all() != None:

                    # Draw looked aoi if required
                    if draw_looked_aoi:

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

                    # Draw looked aoi name if required
                    if looked_aoi_name_color:

                        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]