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
|
"""ArCamera based of ArUco markers technology.
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 <http://www.gnu.org/licenses/>.
"""
__author__ = "Théo de la Hogue"
__credits__ = []
__copyright__ = "Copyright 2023, Ecole Nationale de l'Aviation Civile (ENAC)"
__license__ = "GPLv3"
from typing import TypeVar, Tuple
import json
import os
import time
from argaze import ArFeatures, DataFeatures
from argaze.ArUcoMarkers import ArUcoMarkersDictionary, ArUcoDetector, ArUcoOpticCalibrator, ArUcoScene
from argaze.AreaOfInterest import AOI2DScene
import cv2
import numpy
ArUcoCameraType = TypeVar('ArUcoCamera', bound="ArUcoCamera")
# Type definition for type annotation convenience
# Define default ArUcoCamera image_paremeters values
DEFAULT_ARUCOCAMERA_IMAGE_PARAMETERS = {
"draw_detected_markers": {
"color": (0, 255, 0),
"draw_axes": {
"thickness": 3
}
}
}
class ArUcoCamera(ArFeatures.ArCamera):
"""
Define an ArCamera based on ArUco marker detection.
"""
@DataFeatures.PipelineStepInit
def __init__(self, **kwargs):
"""Initialize ArUcoCamera"""
# Init parent class
super().__init__(**kwargs)
# Init private attribute
self.__aruco_detector = None
self.__image_parameters = {**ArFeatures.DEFAULT_ARFRAME_IMAGE_PARAMETERS, **DEFAULT_ARUCOCAMERA_IMAGE_PARAMETERS}
@property
def aruco_detector(self) -> ArUcoDetector.ArUcoDetector:
"""ArUco marker detector."""
return self.__aruco_detector
@aruco_detector.setter
@DataFeatures.PipelineStepAttributeSetter
def aruco_detector(self, aruco_detector: ArUcoDetector.ArUcoDetector):
self.__aruco_detector = aruco_detector
# Check optic parameters
if self.__aruco_detector.optic_parameters is not None:
# Optic parameters dimensions should be equal to camera frame size
if self.__aruco_detector.optic_parameters.dimensions != self.size:
raise ArFeatures.LoadingFailed('ArUcoCamera: aruco_detector.optic_parameters.dimensions have to be equal to size.')
# No optic parameters loaded
else:
# Create default optic parameters adapted to frame size
# Note: The choice of 1000 for default focal length should be discussed...
self.__aruco_detector.optic_parameters = ArUcoOpticCalibrator.OpticParameters(rms=-1, dimensions=self.size, K=ArUcoOpticCalibrator.K0(focal_length=(1000., 1000.), width=self.size[0], height=self.size[1]))
# Edit parent
if self.__aruco_detector is not None:
self.__aruco_detector.parent = self
@ArFeatures.ArCamera.scenes.setter
def scenes(self, scenes: dict):
self.__scenes = {}
for scene_name, scene_data in scenes.items():
self.__scenes[scene_name] = ArUcoScene.ArUcoScene(working_directory = self.working_directory, name = scene_name, **scene_data)
# Edit parent
for name, scene in self.__scenes.items():
scene.parent = self
@ArFeatures.ArFrame.image_parameters.setter
def image_parameters(self, image_parameters: dict):
self.__image_parameters = image_parameters
if 'draw_layers' not in self.__image_parameters:
self.__image_parameters['draw_layers'] = {}
for layer_name in self.layers.keys():
self.__image_parameters['draw_layers'][layer_name] = ArFeatures.DEFAULT_ARLAYER_DRAW_PARAMETERS
@DataFeatures.PipelineStepMethod
def watch(self, image: numpy.array):
"""Detect environment aruco markers from image and project scenes into camera frame."""
# Use camera frame locker feature
with self._lock:
# Detect aruco markers
self.__aruco_detector.detect_markers(image, timestamp=self.timestamp)
# Fill camera frame background with image
self.background = image
# Clear former layers projection into camera frame
for layer_name, layer in self.layers.items():
layer.aoi_scene.clear()
# Project each aoi 3d scene into camera frame
for scene_name, scene in self.scenes.items():
''' TODO: Enable aruco_aoi processing
if scene.aruco_aoi:
try:
# Build AOI scene directly from detected ArUco marker corners
self.layers[??].aoi_2d_scene |= scene.build_aruco_aoi_scene(self.__aruco_detector.detected_markers())
except ArFeatures.PoseEstimationFailed:
pass
'''
# Estimate scene pose from detected scene markers
tvec, rmat, _ = scene.estimate_pose(self.__aruco_detector.detected_markers(), timestamp=self.timestamp)
# Project scene into camera frame according estimated pose
for layer_name, layer_projection in scene.project(tvec, rmat, self.visual_hfov, self.visual_vfov, timestamp=self.timestamp):
try:
# Update camera layer aoi
self.layers[layer_name].aoi_scene |= layer_projection
# Timestamp camera layer
self.layers[layer_name].timestamp = self.timestamp
except KeyError:
pass
def __image(self, draw_detected_markers: dict = None, draw_scenes: dict = None, draw_optic_parameters_grid: dict = None, **kwargs: dict) -> numpy.array:
"""Get frame image with ArUco detection visualisation.
Parameters:
draw_detected_markers: ArucoMarker.draw parameters (if None, no marker drawn)
draw_scenes: ArUcoScene.draw parameters (if None, no scene drawn)
draw_optic_parameters_grid: OpticParameter.draw parameters (if None, no grid drawn)
kwargs: ArCamera.image parameters
"""
# Get camera frame image
# Note: don't lock/unlock camera frame here as super().image manage it.
image = super().image(**kwargs)
# Use frame locker feature
with self._lock:
# Draw optic parameters grid if required
if draw_optic_parameters_grid is not None:
self.__aruco_detector.optic_parameters.draw(image, **draw_optic_parameters_grid)
# Draw scenes if required
if draw_scenes is not None:
for scene_name, draw_scenes_parameters in draw_scenes.items():
self.scenes[scene_name].draw(image, **draw_scenes_parameters)
# Draw detected markers if required
if draw_detected_markers is not None:
self.__aruco_detector.draw_detected_markers(image, draw_detected_markers)
return image
def image(self, **kwargs: dict) -> numpy.array:
"""
Get frame image.
Parameters:
kwargs: ArUcoCamera.__image parameters
"""
# Use image_parameters attribute if no kwargs
if kwargs:
return self.__image(**kwargs)
return self.__image(**self.image_parameters)
|