aboutsummaryrefslogtreecommitdiff
path: root/src/argaze.test/ArUcoMarker/ArUcoScene.py
blob: fb3fd89c34c4e0814f9ab27b387eee88a6ca05a3 (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
""" """

"""
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 <https://www.gnu.org/licenses/>.
"""

__author__ = "Théo de la Hogue"
__credits__ = []
__copyright__ = "Copyright 2023, Ecole Nationale de l'Aviation Civile (ENAC)"
__license__ = "GPLv3"

import unittest
import os
import math

from argaze.ArUcoMarker import ArUcoMarkerGroup, ArUcoMarker

import cv2 as cv
import numpy

class TestArUcoMarkerGroupClass(unittest.TestCase):

    def new_from_obj(self):

        # Edit file path
        current_directory = os.path.dirname(os.path.abspath(__file__))
        obj_filepath = os.path.join(current_directory, 'utils/scene.obj')

        # Load file
        self.aruco_markers_group = ArUcoMarkerGroup.ArUcoMarkerGroup.from_obj(obj_filepath)

    def new_from_json(self):

        # Edit file path
        current_directory = os.path.dirname(os.path.abspath(__file__))
        json_filepath = os.path.join(current_directory, 'utils/scene.json')

        # Load file
        self.aruco_markers_group = ArUcoMarkerGroup.ArUcoMarkerGroup.from_json(json_filepath)

    def setup_markers(self):

        # Prepare detected markers
        self.detected_markers = {
            0: ArUcoMarker.ArUcoMarker('DICT_ARUCO_ORIGINAL', 0, 1.),
            1: ArUcoMarker.ArUcoMarker('DICT_ARUCO_ORIGINAL', 1, 1.),
            2: ArUcoMarker.ArUcoMarker('DICT_ARUCO_ORIGINAL', 2, 1.),
            3: ArUcoMarker.ArUcoMarker('DICT_ARUCO_ORIGINAL', 3, 1.)
        }

        # Prepare scene markers and remaining markers
        self.scene_markers, self.remaining_markers = self.aruco_markers_group.filter_markers(self.detected_markers())

    def test_new_from_obj(self):
        """Test ArUcoMarkerGroup creation."""

        self.new_from_obj()
        self.setup_markers()

         # Check ArUcoMarkerGroup creation
        self.assertEqual(len(self.aruco_markers_group.places), 3)
        self.assertIsNone(numpy.testing.assert_array_equal(self.aruco_markers_group.identifiers, [0, 1, 2]))
        self.assertEqual(self.aruco_markers_group.marker_size, 1.)

        self.assertEqual(self.aruco_markers_group.places[0].marker.identifier, 0)
        self.assertIsNone(numpy.testing.assert_array_equal(self.aruco_markers_group.places[0].translation, [0., 0., 0.]))
        self.assertIsNone(numpy.testing.assert_array_equal(self.aruco_markers_group.places[0].rotation, [[1., 0., 0.], [0., 1., 0.], [0., 0., 1.]]))

        self.assertEqual(self.aruco_markers_group.places[1].marker.identifier, 1)
        self.assertIsNone(numpy.testing.assert_array_equal(self.aruco_markers_group.places[1].translation, [10., 10., 0.]))
        self.assertIsNone(numpy.testing.assert_array_equal(self.aruco_markers_group.places[1].rotation, [[1., 0., 0.], [0., 1., 0.], [0., 0., 1.]]))

        self.assertEqual(self.aruco_markers_group.places[2].marker.identifier, 2)
        self.assertIsNone(numpy.testing.assert_array_equal(self.aruco_markers_group.places[2].translation, [0., 10., 0.]))
        self.assertIsNone(numpy.testing.assert_array_equal(self.aruco_markers_group.places[2].rotation, [[1., 0., 0.], [0., 1., 0.], [0., 0., 1.]]))

    def test_new_from_json(self):
        """Test ArUcoMarkerGroup creation."""

        self.new_from_json()
        self.setup_markers()

         # Check ArUcoMarkerGroup creation
        self.assertEqual(len(self.aruco_markers_group.places), 3)
        self.assertIsNone(numpy.testing.assert_array_equal(self.aruco_markers_group.identifiers, [0, 1, 2]))
        self.assertEqual(self.aruco_markers_group.marker_size, 1.)

        self.assertEqual(self.aruco_markers_group.places[0].marker.identifier, 0)
        self.assertIsNone(numpy.testing.assert_array_equal(self.aruco_markers_group.places[0].translation, [0., 0., 0.]))
        self.assertIsNone(numpy.testing.assert_array_equal(self.aruco_markers_group.places[0].rotation, [[1., 0., 0.], [0., 1., 0.], [0., 0., 1.]]))

        self.assertEqual(self.aruco_markers_group.places[1].marker.identifier, 1)
        self.assertIsNone(numpy.testing.assert_array_equal(self.aruco_markers_group.places[1].translation, [10., 10., 0.]))
        self.assertIsNone(numpy.testing.assert_array_equal(self.aruco_markers_group.places[1].rotation, [[1., 0., 0.], [0., 1., 0.], [0., 0., 1.]]))

        self.assertEqual(self.aruco_markers_group.places[2].marker.identifier, 2)
        self.assertIsNone(numpy.testing.assert_array_equal(self.aruco_markers_group.places[2].translation, [0., 10., 0.]))
        self.assertIsNone(numpy.testing.assert_array_equal(self.aruco_markers_group.places[2].rotation, [[1., 0., 0.], [0., 1., 0.], [0., 0., 1.]]))

    def test_filter_markers(self):
        """Test ArUcoMarkerGroup markers filtering."""

        self.new_from_obj()
        self.setup_markers()

        # Check scene markers and remaining markers
        self.assertEqual(len(self.scene_markers), 3)
        self.assertEqual(len(self.remaining_markers), 1)

        self.assertIsNone(numpy.testing.assert_array_equal(list(self.scene_markers.keys()), self.aruco_markers_group.identifiers))
        self.assertIsNone(numpy.testing.assert_array_equal(list(self.remaining_markers.keys()), [3]))

    def test_check_markers_consistency(self):
        """Test ArUcoMarkerGroup markers consistency checking."""

        self.new_from_obj()
        self.setup_markers()

        # Edit consistent marker poses
        self.scene_markers[0].translation = numpy.array([1., 1., 5.])
        self.scene_markers[0].rotation = numpy.array([[1., 0., 0.], [0., 1., 0.], [0., 0., 1.]])

        self.scene_markers[1].translation = numpy.array([11., 11., 5.])
        self.scene_markers[1].rotation = numpy.array([[1., 0., 0.], [0., 1., 0.], [0., 0., 1.]])

        self.scene_markers[2].translation = numpy.array([1., 11., 5.])
        self.scene_markers[2].rotation = numpy.array([[1., 0., 0.], [0., 1., 0.], [0., 0., 1.]])

        # Check consistency
        consistent_markers, unconsistent_markers, unconsistencies = self.aruco_markers_group.check_markers_consistency(self.scene_markers, 1, 1)

        # Check consistent markers, unconsistent markers and unconsistencies
        self.assertEqual(len(consistent_markers), 3)
        self.assertEqual(len(unconsistent_markers), 0)
        self.assertEqual(len(unconsistencies['rotation']), 0)
        self.assertEqual(len(unconsistencies['translation']), 0)

        self.assertIsNone(numpy.testing.assert_array_equal(list(consistent_markers.keys()), self.aruco_markers_group.identifiers))

        # Edit unconsistent marker poses
        self.scene_markers[2].translation = numpy.array([5., 15., 5.])

        # Check consistency
        consistent_markers, unconsistent_markers, unconsistencies = self.aruco_markers_group.check_markers_consistency(self.scene_markers, 1, 1)

        # Check consistent markers, unconsistent markers and unconsistencies
        self.assertEqual(len(consistent_markers), 2)
        self.assertEqual(len(unconsistent_markers), 1)
        self.assertEqual(len(unconsistencies['rotation']), 0)
        self.assertEqual(len(unconsistencies['translation']), 2)

        self.assertIsNone(numpy.testing.assert_array_equal(list(unconsistent_markers.keys()), [2]))
        self.assertIsNone(numpy.testing.assert_array_equal(list(unconsistencies['translation'].keys()), ['0/2', '1/2']))
        self.assertIsNone(numpy.testing.assert_array_equal(list(unconsistencies['translation']['0/2'].keys()), ['current', 'expected']))
        self.assertIsNone(numpy.testing.assert_array_equal(list(unconsistencies['translation']['1/2'].keys()), ['current', 'expected']))

    def test_estimate_pose_from_single_marker(self):
        """Test ArUcoMarkerGroup pose estimation from single marker."""

        self.new_from_obj()
        self.setup_markers()

        # Edit marke pose
        self.scene_markers[0].translation = numpy.array([1., 1., 5.])
        self.scene_markers[0].rotation = numpy.array([[1., 0., 0.], [0., 1., 0.], [0., 0., 1.]])

        # Estimate pose
        tvec, rmat = self.aruco_markers_group.estimate_pose_from_single_marker(self.scene_markers[0])

        self.assertIsNone(numpy.testing.assert_array_equal(tvec, [1., 1., 5.]))
        self.assertIsNone(numpy.testing.assert_array_equal(rmat, [[1., 0., 0.], [0., 1., 0.], [0., 0., 1.]]))

    def test_estimate_pose_from_markers(self):
        """Test ArUcoMarkerGroup pose estimation from markers."""

        self.new_from_obj()
        self.setup_markers()

        # Edit markers pose
        self.scene_markers[0].translation = numpy.array([1., 1., 5.])
        self.scene_markers[0].rotation = numpy.array([[1., 0., 0.], [0., 1., 0.], [0., 0., 1.]])

        self.scene_markers[1].translation = numpy.array([11., 11., 5.])
        self.scene_markers[1].rotation = numpy.array([[1., 0., 0.], [0., 1., 0.], [0., 0., 1.]])

        self.scene_markers[2].translation = numpy.array([1., 11., 5.])
        self.scene_markers[2].rotation = numpy.array([[1., 0., 0.], [0., 1., 0.], [0., 0., 1.]])

        # Estimate pose
        tvec, rmat = self.aruco_markers_group.estimate_pose_from_markers(self.scene_markers)

        self.assertIsNone(numpy.testing.assert_array_equal(tvec, [1., 1., 5.]))
        self.assertIsNone(numpy.testing.assert_array_equal(rmat, [[1., 0., 0.], [0., 1., 0.], [0., 0., 1.]]))

    @unittest.skip("ArUcoMarkerGroup estimate_pose_from_axis_markers method is broken.")
    def test_estimate_pose_from_axis_markers(self):
        """Test ArUcoMarkerGroup pose estimation from axis markers."""

        self.new_from_obj()
        self.setup_markers()

        # Edit markers pose
        self.scene_markers[0].translation = numpy.array([1., 1., 5.])
        self.scene_markers[0].rotation = numpy.array([[1., 0., 0.], [0., 1., 0.], [0., 0., 1.]])

        self.scene_markers[1].translation = numpy.array([11., 11., 5.])
        self.scene_markers[1].rotation = numpy.array([[1., 0., 0.], [0., 1., 0.], [0., 0., 1.]])

        self.scene_markers[2].translation = numpy.array([1., 11., 5.])
        self.scene_markers[2].rotation = numpy.array([[1., 0., 0.], [0., 1., 0.], [0., 0., 1.]])

        # Estimate pose
        tvec, rmat = self.aruco_markers_group.estimate_pose_from_axis_markers(self.scene_markers[2], self.scene_markers[1], self.scene_markers[0])

        self.assertIsNone(numpy.testing.assert_array_equal(tvec, [1., 1., 5.]))
        self.assertIsNone(numpy.testing.assert_array_equal(rmat, [[1., 0., 0.], [0., -1., 0.], [0., 0., -1.]]))

if __name__ == '__main__':

    unittest.main()