aboutsummaryrefslogtreecommitdiff
path: root/src/argaze/ArUcoMarkers/ArUcoDetector.py
blob: 5891f166e1fd4986667b62ba383e7e10e2851c67 (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
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
#!/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 dataclasses import dataclass, field
import json
from collections import Counter

from argaze.ArUcoMarkers import ArUcoMarkersDictionary, ArUcoMarker, ArUcoOpticCalibrator

import numpy
import cv2 as cv
import cv2.aruco as aruco

ArUcoMarkerDictionaryType = TypeVar('ArUcoMarkerDictionary', bound="ArUcoMarkerDictionary")
# Type definition for type annotation convenience

ArUcoMarkerType = TypeVar('ArUcoMarker', bound="ArUcoMarker")
# Type definition for type annotation convenience

OpticParametersType = TypeVar('OpticParameters', bound="OpticParameters")
# Type definition for type annotation convenience

DetectorParametersType = TypeVar('DetectorParameters', bound="DetectorParameters")
# Type definition for type annotation convenience

ArUcoDetectorType = TypeVar('ArUcoDetector', bound="ArUcoDetector")
# Type definition for type annotation convenience

class DetectorParameters():
    """Wrapper class around ArUco marker detector parameters.

    .. note:: More details on [opencv page](https://docs.opencv.org/4.x/d1/dcd/structcv_1_1aruco_1_1DetectorParameters.html)
    """

    __parameters = aruco.DetectorParameters()
    __parameters_names = [
        'adaptiveThreshConstant',
        'adaptiveThreshWinSizeMax',
        'adaptiveThreshWinSizeMin',
        'adaptiveThreshWinSizeStep',
        'aprilTagCriticalRad',
        'aprilTagDeglitch',
        'aprilTagMaxLineFitMse',
        'aprilTagMaxNmaxima',
        'aprilTagMinClusterPixels',
        'aprilTagMinWhiteBlackDiff',
        'aprilTagQuadDecimate',
        'aprilTagQuadSigma',
        'cornerRefinementMaxIterations',
        'cornerRefinementMethod',
        'cornerRefinementMinAccuracy',
        'cornerRefinementWinSize',
        'markerBorderBits',
        'minMarkerPerimeterRate',
        'maxMarkerPerimeterRate',
        'minMarkerDistanceRate',
        'detectInvertedMarker',
        'errorCorrectionRate',
        'maxErroneousBitsInBorderRate',
        'minCornerDistanceRate',
        'minDistanceToBorder',
        'minOtsuStdDev',
        'perspectiveRemoveIgnoredMarginPerCell',
        'perspectiveRemovePixelPerCell',
        'polygonalApproxAccuracyRate'
    ]

    def __init__(self, **kwargs):

        for parameter, value in kwargs.items():

            setattr(self.__parameters, parameter, value)

        self.__dict__.update(kwargs)

    def __setattr__(self, parameter, value):

        setattr(self.__parameters, parameter, value)

    def __getattr__(self, parameter):

        return getattr(self.__parameters, parameter)

    @classmethod
    def from_json(self, json_filepath) -> DetectorParametersType:
        """Load detector parameters from .json file."""

        with open(json_filepath) as configuration_file:

            return DetectorParameters(**json.load(configuration_file))

    def __str__(self, print_all=False) -> str:
        """Detector paremeters string representation."""

        output = ''

        for parameter in self.__parameters_names:

            if parameter in self.__dict__.keys():

                output += f'\t*{parameter}: {getattr(self.__parameters, parameter)}\n'

            elif print_all:

                output += f'\t{parameter}: {getattr(self.__parameters, parameter)}\n'

        return output

    @property
    def internal(self):
        return self.__parameters

@dataclass
class ArUcoDetector():
    """ArUco markers detector."""

    dictionary: ArUcoMarkersDictionary.ArUcoMarkersDictionary = field(default_factory=ArUcoMarkersDictionary.ArUcoMarkersDictionary)
    """ArUco markers dictionary to detect."""

    marker_size: float = field(default=0.)
    """Size of ArUco markers to detect in centimeter."""

    optic_parameters: ArUcoOpticCalibrator.OpticParameters = field(default_factory=ArUcoOpticCalibrator.OpticParameters)
    """Optic parameters to use for ArUco detection into frame."""

    parameters: DetectorParameters = field(default_factory=DetectorParameters)
    """ArUco detector parameters."""

    def __post_init__(self):

        # Init detected markers data
        self.__detected_markers = {}
        self.__detected_markers_corners = []
        self.__detected_markers_ids = []

        # Init detected board data
        self.__board = None
        self.__board_corners_number = 0
        self.__board_corners = []
        self.__board_corners_ids = []

        # Init detect metrics data
        self.__detection_count = 0
        self.__detected_ids = []

    @classmethod
    def from_json(self, json_filepath: str) -> ArUcoDetectorType:
        """Load ArUcoDetector setup from .json file."""

        with open(json_filepath) as configuration_file:

            data = json.load(configuration_file)

            new_dictionary = ArUcoMarkersDictionary.ArUcoMarkersDictionary(**data.pop('dictionary'))
            new_marker_size = data.pop('marker_size')
            new_optic_parameters = ArUcoOpticCalibrator.OpticParameters(**data.pop('optic_parameters'))
            new_parameters = DetectorParameters(**data.pop('parameters'))

            return ArUcoDetector(new_dictionary, new_marker_size, new_optic_parameters, new_parameters)

    def __str__(self) -> str:
        """String display"""

        output = f'\n\tDictionary: {self.dictionary}\n'
        output += f'\tMarker size: {self.marker_size} cm\n\n'
        output += f'\tOptic parameters:\n{self.optic_parameters}\n'
        output += f'\tDetection Parameters:\n{self.parameters}'

        return output

    def detect_markers(self, frame: numpy.array):
        """Detect all ArUco markers into a frame.

        .. danger:: DON'T MIRROR FRAME
           It makes the markers detection to fail.
        """

        # Reset detected markers data
        self.__detected_markers, self.__detected_markers_corners, self.__detected_markers_ids = {}, [], []

        # Detect markers into gray picture
        self.__detected_markers_corners, self.__detected_markers_ids, _ = aruco.detectMarkers(cv.cvtColor(frame, cv.COLOR_BGR2GRAY), self.dictionary.markers, parameters = self.parameters.internal)

        # Is there detected markers ?
        if len(self.__detected_markers_corners) > 0:

            # Transform markers ids array into list
            self.__detected_markers_ids = self.__detected_markers_ids.T[0]

            # Gather detected markers data and update metrics
            self.__detection_count += 1

            for i, marker_id in enumerate(self.__detected_markers_ids):

                marker = ArUcoMarker.ArUcoMarker(self.dictionary, marker_id, self.marker_size)

                marker.corners = self.__detected_markers_corners[i]

                # No pose estimation: call estimate_pose to get one
                marker.translation = numpy.empty([0])
                marker.rotation = numpy.empty([0])
                marker.points = numpy.empty([0])

                self.__detected_markers[marker_id] = marker

                self.__detected_ids.append(marker_id)

    def estimate_markers_pose(self, markers_ids: list = []):
        """Estimate pose of current detected markers or of given markers id list."""

        # Is there detected markers ?
        if len(self.__detected_markers_corners) > 0:

            # Is there a marker selection ?
            if len(markers_ids) > 0:

                selected_markers_corners = tuple()
                selected_markers_ids = []

                for i, marker_id in enumerate(self.__detected_markers_ids):

                    if marker_id in markers_ids:

                        selected_markers_corners += (self.__detected_markers_corners[i],)
                        selected_markers_ids.append(marker_id)

            # Otherwise, estimate pose of all markers
            else:

                selected_markers_corners = self.__detected_markers_corners
                selected_markers_ids = self.__detected_markers_ids

            # Estimate pose of selected markers
            if len(selected_markers_corners) > 0:

                markers_rvecs, markers_tvecs, markers_points = aruco.estimatePoseSingleMarkers(selected_markers_corners, self.marker_size, numpy.array(self.optic_parameters.K), numpy.array(self.optic_parameters.D)) 

                for i, marker_id in enumerate(selected_markers_ids):

                    marker = self.__detected_markers[marker_id]

                    marker.translation = markers_tvecs[i][0]
                    marker.rotation, _ = cv.Rodrigues(markers_rvecs[i][0])
                    marker.points = markers_points.reshape(4, 3)

    @property
    def detected_markers(self) -> dict[ArUcoMarkerType]:
        """Access to detected markers dictionary."""

        return self.__detected_markers

    @property
    def detected_markers_number(self) -> int:
        """Return detected markers number."""

        return len(list(self.__detected_markers.keys()))

    def draw_detected_markers(self, frame: numpy.array):
        """Draw traked markers."""

        for marker_id, marker in self.__detected_markers.items():

            marker.draw(frame, self.optic_parameters.K, self.optic_parameters.D)
            
    def detect_board(self, frame: numpy.array, board, expected_markers_number):
        """Detect ArUco markers board in frame setting up the number of detected markers needed to agree detection.

        .. danger:: DON'T MIRROR FRAME
           It makes the markers detection to fail.
        """
        
        # detect markers from gray picture
        gray = cv.cvtColor(frame, cv.COLOR_BGR2GRAY)
        self.__detected_markers_corners, self.__detected_markers_ids, _ = aruco.detectMarkers(gray, self.dictionary.markers, parameters = self.parameters.internal)

         # if all board markers are detected
        if len(self.__detected_markers_corners) == expected_markers_number:

            self.__board = board
            self.__board_corners_number, self.__board_corners, self.__board_corners_ids = aruco.interpolateCornersCharuco(self.__detected_markers_corners, self.__detected_markers_ids, gray, self.__board.model)

        else:

            self.__board = None
            self.__board_corners_number = 0
            self.__board_corners = []
            self.__board_corners_ids = []

    def draw_board(self, frame: numpy.array):
        """Draw detected board corners in frame."""

        if self.__board != None:

            cv.drawChessboardCorners(frame, ((self.__board.size[0] - 1 ), (self.__board.size[1] - 1)), self.__board_corners, True)

    def reset_detection_metrics(self):
        """Enable marker detection metrics."""

        self.__detection_count = 0
        self.__detected_ids = []

    @property
    def detection_metrics(self) -> Tuple[int, dict]:
        """Get marker detection metrics.
        Returns:
                number of detect function call
                dict with number of detection for each marker identifier"""

        return self.__detection_count, Counter(self.__detected_ids)

    @property
    def board_corners_number(self) -> int:
        """Get detected board corners number."""

        return self.__board_corners_number

    @property
    def board_corners_identifier(self) -> list[int]:
        """Get detected board corners identifier."""

        return self.__board_corners_ids

    @property
    def board_corners(self) -> list:
        """Get detected board corners."""

        return self.__board_corners