aboutsummaryrefslogtreecommitdiff
path: root/src/argaze/AreaOfInterest/AOI2DScene.py
blob: 564f65c1b2115ea8d3579d21027ff1f7eae2c1fa (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
#!/usr/bin/env python

""" """

__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 argaze import DataStructures
from argaze.AreaOfInterest import AOIFeatures, AOI3DScene
from argaze import GazeFeatures

import cv2
import numpy

AOI2DSceneType = TypeVar('AOI2DScene', bound="AOI2DScene")
# Type definition for type annotation convenience

AOI3DSceneType = TypeVar('AOI3DScene', bound="AOI3DScene")
# Type definition for type annotation convenience

class AOI2DScene(AOIFeatures.AOIScene):
	"""Define AOI 2D scene."""

	def __init__(self, aois_2d: dict = None):

		super().__init__(2, aois_2d)

	def draw(self, image: numpy.array, draw_aoi: dict = None, exclude=[]):
		"""Draw AOI polygons on image.

		Parameters:
			draw_aoi: AOIFeatures.AOI.draw parameters (if None, no aoi is drawn)
		"""

		for name, aoi in self.items():

			if name in exclude:
				continue

			if draw_aoi:
				aoi.draw(image, **draw_aoi)

	def raycast(self, pointer:tuple) -> Tuple[str, "AOIFeatures.AreaOfInterest", bool]:
		"""Iterate over aoi to know which aoi is matching the given pointer position.
		Returns:
				aoi name
				aoi object
				matching status
		"""

		for name, aoi in self.items():

			matching = aoi.contains_point(pointer)

			yield name, aoi, matching

	def draw_raycast(self, image: numpy.array, pointer:tuple, exclude=[], base_color=(0, 0, 255), matching_color=(0, 255, 0)):
		"""Draw AOIs with their matching status."""

		for name, aoi, matching in self.raycast(pointer):

			if name in exclude:
				continue
			
			color = matching_color if matching else base_color

			if matching:

				top_left_corner_pixel = numpy.rint(aoi.clockwise()[0]).astype(int)
				cv2.putText(image, name, top_left_corner_pixel, cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255), 1, cv2.LINE_AA)

			# Draw form
			aoi.draw(image, color)

	def circlecast(self, center:tuple, radius:float) -> Tuple[str, "AOIFeatures.AreaOfInterest", numpy.array, float, float]:
		"""Iterate over areas to know which aoi is matched circle.
		Returns:
				aoi name
				aoi object
				matching region points
				ratio of matched region area relatively to aoi area
				ratio of matched region area relatively to circle area
		"""

		for name, aoi in self.items():

			matched_region, aoi_ratio, circle_ratio = aoi.circle_intersection(center, radius)

			yield name, aoi, matched_region, aoi_ratio, circle_ratio

	'''DEPRECATED: but maybe still usefull?
	def reframe(self, aoi: AOIFeatures.AreaOfInterest, size: tuple) -> AOI2DSceneType:
		"""
		Reframe whole scene to a scene bounded by a 4 vertices 2D AOI.

		Parameters:
			aoi: 4 vertices 2D AOI used to reframe scene
			size: size of reframed scene

		Returns:
			reframed AOI 2D scene
		"""
		
		assert(aoi.dimension == 2)
		assert(aoi.points_number == 4)

		# Edit affine transformation (M) allowing to transform source axis (Src) into destination axis (Dst)
		Src = aoi.clockwise().astype(numpy.float32)
		Src_origin = Src[0]
		Src = Src - Src_origin
		Dst = numpy.float32([[0, 0], [size[0], 0], [size[0], size[1]], [0, size[1]]])

		M = cv2.getAffineTransform(Src[:3], Dst[:3])[:, :2]

		# Apply affine transformationto each AOI
		aoi2D_scene = AOI2DScene()
		
		for name, aoi2D in self.items():

			aoi2D_scene[name] = numpy.matmul(aoi2D - Src_origin, M.T)

		return aoi2D_scene
	'''
	def dimensionalize(self, rectangle_3d: AOIFeatures.AreaOfInterest, size: tuple) -> AOI3DSceneType:
		"""
		Convert to 3D scene considering it is inside of 3D rectangular frame.

		Parameters:
			rectangle_3d: rectangle 3D AOI to use as referential plane 
			size: size of the frame in pixel

		Returns:
			AOI 3D scene
		"""

		assert(rectangle_3d.dimension == 3)
		assert(rectangle_3d.points_number == 4)

		# Vectorize outter_axis function
		vfunc = numpy.vectorize(rectangle_3d.outter_axis)

		# Prepare new AOI 3D scene
		aoi3D_scene = AOI3DScene.AOI3DScene()

		for name, aoi2D in self.items():

			X, Y = (aoi2D / size).T
			aoi3D_scene[name] = numpy.array(vfunc(X, Y)).T.view(AOIFeatures.AreaOfInterest)

		return aoi3D_scene