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
|
#!/usr/bin/env python
from dataclasses import dataclass, field
from argaze.ArUcoMarkers import ArUcoMarkersDictionary
import numpy
import cv2 as cv
import cv2.aruco as aruco
@dataclass
class ArUcoMarker():
"""Define ArUco marker class."""
dictionary: ArUcoMarkersDictionary.ArUcoMarkersDictionary
"""Dictionary to which it belongs."""
identifier: int
"""Index into dictionary"""
size: float
"""Size of marker in centimeters."""
corners: numpy.array = field(init=False, repr=False)
"""Estimated 2D corner positions in camera image referential."""
translation: numpy.array = field(init=False, repr=False)
"""Estimated 3D center position in camera referential."""
rotation: numpy.array = field(init=False, repr=False)
"""Estimated 3D marker rotation in camera referential."""
points: numpy.array = field(init=False, repr=False)
"""Estimated 3D corners positions in camera referential."""
def center(self, i):
"""Get 2D center position in camera image referential.
**Returns:** numpy.array"""
return self.corners[0].mean(axis=0)
def draw(self, frame, K, D):
"""Draw marker in frame."""
# Draw marker axis if pose has been estimated
if self.translation.size == 3 and self.rotation.size == 3:
cv.drawFrameAxes(frame, K, D, self.rotation, self.translation, self.size)
aruco.drawDetectedMarkers(frame, [self.corners], numpy.array([self.identifier]))
|