Skip to content

Base Converter

Bases: ABC

Base converter class. Based on the source and destination formats, defines reader and writer classes for processing data, defines default source and destination annotation file suffixes

Source code in tools/annotation_converter/converter/base.py
 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
class BaseConverter(ABC):
    """
    Base converter class. Based on the source and destination formats, defines reader and writer classes for
        processing data, defines default source and destination annotation file suffixes

    """
    def __init__(
            self,
            source_format: str,
            dest_format: str,
            log_level: str = LevelMapping.debug,
            log_path: Optional[Path] = None,
            **kwargs
    ):
        """
        Initialize the converter class.

        Args:
            source_format (str): The format of source annotation (e.g., 'yolo').
            dest_format (str): The format of output annotations (e.g., 'voc').
            log_level: (str): The lowest logging level print to (e.g., 'debug').
            **kwargs (dict): Additional parameters like 'img_path' or 'labels_path'.
        """
        self.reader_mapping = {
            ".xml": XMLReader,
            ".txt": TXTReader
        }

        self.writer_mapping = {
            ".txt": YoloWriter,
            ".xml": XMLWriter
        }

        self.suffix_mapping = {
            "voc": ".xml",
            "yolo": ".txt"
        }

        self.source_suffix = self.suffix_mapping.get(source_format)
        self.dest_suffix = self.suffix_mapping.get(dest_format)
        self.reader = self.reader_mapping[self.source_suffix]()
        self.writer = self.writer_mapping[self.dest_suffix]()

        self.logger = LoggerConfigurator.setup(
            name=self.__class__.__name__,
            log_level=log_level,
            log_path=Path(log_path) / f"{self.__class__.__name__}.log" if log_path else None
        )


    @abstractmethod
    def convert(self, file_paths: Tuple[Path], target_path: Path, n_jobs: int = 1) -> None:
        """
        Abstract method to convert source annotation file to destination annotation file by running custom workers in
            subclasses

        Args:
            file_paths (Tuple[Path]): List of paths to the annotation files.
            target_path (Path): Directory where converted files will be stored.
            n_jobs (int): Number of parallel workers to use. Defaults to 1.
        """
        pass

    @property
    def reader(self) -> BaseReader:
        """BaseReader: returns a reader for the annotation file."""
        return self._reader

    @reader.setter
    def reader(self, reader: BaseReader) -> None:
        """
        Sets a reader for the annotation filetype.

        Args:
            reader (BaseReader): Reader for the annotation filetype.
        """
        self._reader = reader

    @property
    def writer(self) -> BaseWriter:
        """BaseWriter: returns a writer for the annotation filetype."""
        return self._writer

    @writer.setter
    def writer(self, writer: BaseWriter) -> None:
        """
        Sets a writer for the annotation filetype.

        Args:
            writer (BaseWriter): Writer for the annotation filetype.
        """
        self._writer = writer

reader property writable

BaseReader: returns a reader for the annotation file.

writer property writable

BaseWriter: returns a writer for the annotation filetype.

__init__(source_format, dest_format, log_level=LevelMapping.debug, log_path=None, **kwargs)

Initialize the converter class.

Parameters:

Name Type Description Default
source_format str

The format of source annotation (e.g., 'yolo').

required
dest_format str

The format of output annotations (e.g., 'voc').

required
log_level str

(str): The lowest logging level print to (e.g., 'debug').

debug
**kwargs dict

Additional parameters like 'img_path' or 'labels_path'.

{}
Source code in tools/annotation_converter/converter/base.py
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
def __init__(
        self,
        source_format: str,
        dest_format: str,
        log_level: str = LevelMapping.debug,
        log_path: Optional[Path] = None,
        **kwargs
):
    """
    Initialize the converter class.

    Args:
        source_format (str): The format of source annotation (e.g., 'yolo').
        dest_format (str): The format of output annotations (e.g., 'voc').
        log_level: (str): The lowest logging level print to (e.g., 'debug').
        **kwargs (dict): Additional parameters like 'img_path' or 'labels_path'.
    """
    self.reader_mapping = {
        ".xml": XMLReader,
        ".txt": TXTReader
    }

    self.writer_mapping = {
        ".txt": YoloWriter,
        ".xml": XMLWriter
    }

    self.suffix_mapping = {
        "voc": ".xml",
        "yolo": ".txt"
    }

    self.source_suffix = self.suffix_mapping.get(source_format)
    self.dest_suffix = self.suffix_mapping.get(dest_format)
    self.reader = self.reader_mapping[self.source_suffix]()
    self.writer = self.writer_mapping[self.dest_suffix]()

    self.logger = LoggerConfigurator.setup(
        name=self.__class__.__name__,
        log_level=log_level,
        log_path=Path(log_path) / f"{self.__class__.__name__}.log" if log_path else None
    )

convert(file_paths, target_path, n_jobs=1) abstractmethod

Abstract method to convert source annotation file to destination annotation file by running custom workers in subclasses

Parameters:

Name Type Description Default
file_paths Tuple[Path]

List of paths to the annotation files.

required
target_path Path

Directory where converted files will be stored.

required
n_jobs int

Number of parallel workers to use. Defaults to 1.

1
Source code in tools/annotation_converter/converter/base.py
65
66
67
68
69
70
71
72
73
74
75
76
@abstractmethod
def convert(self, file_paths: Tuple[Path], target_path: Path, n_jobs: int = 1) -> None:
    """
    Abstract method to convert source annotation file to destination annotation file by running custom workers in
        subclasses

    Args:
        file_paths (Tuple[Path]): List of paths to the annotation files.
        target_path (Path): Directory where converted files will be stored.
        n_jobs (int): Number of parallel workers to use. Defaults to 1.
    """
    pass