Skip to content

XML writer

Bases: BaseWriter

A writer class for saving annotations in the Pascal VOC XML format.

This class takes processed XML strings (typically generated by a converter) and writes them to the file system as standard .xml files for object detection datasets. It handles the creation of necessary directories.

Source code in tools/annotation_converter/writer/voc.py
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
class XMLWriter(BaseWriter):
    """
    A writer class for saving annotations in the Pascal VOC XML format.

    This class takes processed XML strings (typically generated by a converter)
    and writes them to the file system as standard .xml files for object
    detection datasets. It handles the creation of necessary directories.
    """
    def write(self, data: str, file_path: Path) -> None:
        """
        Writes an XML annotation string to the specified file path.
        This method automatically creates any missing parent directories.

        Args:
            data (str): A string containing the formatted XML data.
            file_path (Path): The target destination where the file will be saved.

        Raises:
            IOError: If the file cannot be opened or written to the disk.
        """
        file_path.parent.mkdir(parents=True, exist_ok=True)

        with open(file_path, "w") as file:
            file.write(data)

write(data, file_path)

Writes an XML annotation string to the specified file path. This method automatically creates any missing parent directories.

Parameters:

Name Type Description Default
data str

A string containing the formatted XML data.

required
file_path Path

The target destination where the file will be saved.

required

Raises:

Type Description
IOError

If the file cannot be opened or written to the disk.

Source code in tools/annotation_converter/writer/voc.py
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
def write(self, data: str, file_path: Path) -> None:
    """
    Writes an XML annotation string to the specified file path.
    This method automatically creates any missing parent directories.

    Args:
        data (str): A string containing the formatted XML data.
        file_path (Path): The target destination where the file will be saved.

    Raises:
        IOError: If the file cannot be opened or written to the disk.
    """
    file_path.parent.mkdir(parents=True, exist_ok=True)

    with open(file_path, "w") as file:
        file.write(data)