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
|
#!/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, DataFeatures
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):
super().__init__()
self.__reset()
def __reset(self):
self.__looked_aoi_data = (None, None)
self.__matched_gaze_movement = None
@DataFeatures.PipelineStepMethod
def match(self, timestamp: int|float, aoi_scene, gaze_movement) -> 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 of 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: 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:
# 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)
@property
def looked_aoi(self) -> AOIFeatures.AreaOfInterest:
"""Get most likely looked aoi for current fixation."""
return self.__looked_aoi_data[1]
@property
def looked_aoi_name(self) -> str:
"""Get most likely looked aoi name for current fixation."""
return self.__looked_aoi_data[0]
|