From 37bfbf9369db78fe41f1564509ec66e53a6c5ad8 Mon Sep 17 00:00:00 2001 From: Théo de la Hogue Date: Wed, 12 Apr 2023 10:58:38 +0200 Subject: Making ArUcoMarkersDictionary as dataclass. --- src/argaze/ArUcoMarkers/ArUcoMarkersDictionary.py | 108 ++++++++++++---------- 1 file changed, 60 insertions(+), 48 deletions(-) (limited to 'src') diff --git a/src/argaze/ArUcoMarkers/ArUcoMarkersDictionary.py b/src/argaze/ArUcoMarkers/ArUcoMarkersDictionary.py index 8c43844..340ef77 100644 --- a/src/argaze/ArUcoMarkers/ArUcoMarkersDictionary.py +++ b/src/argaze/ArUcoMarkers/ArUcoMarkersDictionary.py @@ -1,6 +1,7 @@ #!/usr/bin/env python from typing import TypeVar +from dataclasses import dataclass, field import cv2 as cv import cv2.aruco as aruco @@ -34,93 +35,104 @@ all_aruco_markers_dictionaries = { ArUcoMarkerType = TypeVar('ArUcoMarker', bound="ArUcoMarker") # Type definition for type annotation convenience +@dataclass(frozen=True) class ArUcoMarkersDictionary(): """Handle an ArUco markers dictionary.""" - def __init__(self, name: str = 'DICT_ARUCO_ORIGINAL'): + name: str = field(default='DICT_ARUCO_ORIGINAL') + """Dictionary name""" - if all_aruco_markers_dictionaries.get(name, None) is None: - raise NameError(f'Bad ArUco markers dictionary name: {name}') + def __post_init__(self): - self.__name = name + if all_aruco_markers_dictionaries.get(self.name, None) is None: + raise NameError(f'Bad ArUco markers dictionary name: {self.name}') - dict_name_split = name.split('_') + def __str__(self) -> str: + """String display""" - self.__format = dict_name_split[1] + output = f'{self.name}\n' - # DICT_ARUCO_ORIGINAL case - if self.__format == 'ARUCO': + return output + + @property + def markers(self) -> aruco.Dictionary: + """Get all markers from dictionary.""" + + return aruco.getPredefinedDictionary(all_aruco_markers_dictionaries[self.name]) + + @property + def format(self) -> str: + """Get markers format.""" + + dict_name_split = self.name.split('_') + dict_type = dict_name_split[1] - self.__format = '5X5' - self.__number = 1024 + # DICT_ARUCO_ORIGINAL case + if dict_type == 'ARUCO': + return '5X5' # DICT_APRILTAG case - elif self.__format == 'APRILTAG': + elif dict_type == 'APRILTAG': april_tag_format = dict_name_split[2] if april_tag_format == '16h5': - - self.__format = '4X4' - self.__number = 30 + return '4X4' elif april_tag_format == '25h9': - - self.__format = '5X5' - self.__number = 30 + return '5X5' elif april_tag_format == '36h10': - - self.__format = '6X6' - self.__number = 2320 + return '6X6' elif april_tag_format == '36h11': - - self.__format = '6X6' - self.__number = 587 + return '6X6' # other cases else: + return dict_type - self.__number = int(dict_name_split[2]) + @property + def number(self) -> int: + """Get number of markers inside dictionary.""" - self.__aruco_dict = aruco.getPredefinedDictionary(all_aruco_markers_dictionaries[self.__name]) + dict_name_split = self.name.split('_') + dict_type = dict_name_split[1] - def __str__(self) -> str: - """String display""" + # DICT_ARUCO_ORIGINAL case + if dict_type == 'ARUCO': + return 1024 - output = f'{self.__name}\n' + # DICT_APRILTAG case + elif dict_type == 'APRILTAG': - return output - - @property - def name(self)-> str: - """Get dictionary name.""" + april_tag_format = dict_name_split[2] - return self.__name + if april_tag_format == '16h5': - @property - def markers(self) -> aruco.Dictionary: - """Get all markers from dictionary.""" + return 30 - return self.__aruco_dict + elif april_tag_format == '25h9': - @property - def format(self) -> str: - """Get markers format.""" + return 30 - return self.__format + elif april_tag_format == '36h10': - @property - def number(self) -> int: - """Get number of markers inside dictionary.""" + return 2320 + + elif april_tag_format == '36h11': + + return 587 + + # other cases + else: - return self.__number + return int(dict_name_split[2]) def create_marker(self, i, size) -> ArUcoMarkerType: """Create a marker.""" - if i >= 0 and i < self.__number: + if i >= 0 and i < self.number: from argaze.ArUcoMarkers import ArUcoMarker @@ -133,6 +145,6 @@ class ArUcoMarkersDictionary(): def save(self, destination_folder, size, dpi): """Save all markers dictionary into separated .png files.""" - for i in range(self.__number): + for i in range(self.number): self.create_marker(i, size).save(destination_folder, dpi) -- cgit v1.1