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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
|
"""ArCamera based of ArUco markers technology."""
__author__ = "Théo de la Hogue"
__credits__ = []
__copyright__ = "Copyright 2023, Ecole Nationale de l'Aviation Civile (ENAC)"
__license__ = "BSD"
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.
"""
def __init__(self, aruco_detector: ArUcoDetector.ArUcoDetector, **kwargs):
""" Initialize ArUcoCamera
Parameters:
aruco_detector: ArUco marker detector
"""
# Init parent class
super().__init__(**kwargs)
# Init private attribute
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 pipeline step objects parent
if self.__aruco_detector is not None:
self.__aruco_detector.parent = self
@property
def aruco_detector(self) -> ArUcoDetector.ArUcoDetector:
"""Get ArUco detector object."""
return self.__aruco_detector
@classmethod
def from_dict(cls, aruco_camera_data: dict, working_directory: str = None) -> ArUcoCameraType:
"""
Load ArUcoCamera from dictionary.
Parameters:
aruco_camera_data: dictionary
working_directory: folder path where to load files when a dictionary value is a relative filepath.
"""
# Load ArUco detector
new_aruco_detector = ArUcoDetector.ArUcoDetector.from_dict(aruco_camera_data.pop('aruco_detector'), working_directory)
# Load ArUcoScenes
new_scenes = {}
try:
for aruco_scene_name, aruco_scene_data in aruco_camera_data.pop('scenes').items():
# Append name
aruco_scene_data['name'] = aruco_scene_name
# Create new aruco scene
new_aruco_scene = ArUcoScene.ArUcoScene.from_dict(aruco_scene_data, working_directory)
# Append new scene
new_scenes[aruco_scene_name] = new_aruco_scene
except KeyError:
pass
# Set image_parameters to default if there is not
if 'image_parameters' not in aruco_camera_data.keys():
aruco_camera_data['image_parameters'] = {**ArFeatures.DEFAULT_ARFRAME_IMAGE_PARAMETERS, **DEFAULT_ARUCOCAMERA_IMAGE_PARAMETERS}
# Set draw_layers to default if there is not
if 'draw_layers' not in aruco_camera_data['image_parameters'].keys():
aruco_camera_data['image_parameters']['draw_layers'] = {}
for layer_name, layer_data in aruco_camera_data['layers'].items():
aruco_camera_data['image_parameters']['draw_layers'][layer_name] = ArFeatures.DEFAULT_ARLAYER_DRAW_PARAMETERS
# Load temporary frame from aruco_camera_data then export it as dict
temp_frame_data = ArFeatures.ArFrame.from_dict(aruco_camera_data, working_directory).as_dict()
# Create new aruco camera using temporary ar frame values
return ArUcoCamera( \
aruco_detector = new_aruco_detector, \
scenes = new_scenes, \
**temp_frame_data \
)
@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)
|