Skip to content

TXT writer

Bases: BaseWriter

A writer class for saving annotations in the YOLO text format (.txt).

This class takes a list of processed annotation strings and saves them into a text file. It ensures that each object is written on a new line and handles the creation of necessary directories.

Source code in tools/annotation_converter/writer/yolo.py
 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
class YoloWriter(BaseWriter):
    """
    A writer class for saving annotations in the YOLO text format (.txt).

    This class takes a list of processed annotation strings and saves them
    into a text file. It ensures that each object is written on a new line
    and handles the creation of necessary directories.
    """
    def write(self, data: List[str], file_path: Path) -> None:
        """
        Writes a list of strings to a YOLO-formatted text file.

        This method automatically creates any missing parent directories.
        It filters out empty lines and adds a newline character after
        each record.

        Args:
            data (List[str]): A list of strings, where each string represents
                an object (class_id, x_center, y_center, width, height).
            file_path (Path): The target file path where the annotation
                will be saved.

        Returns:
            None: This method does not return any value.

        Raises:
            IOError: If there is a problem creating the directory or writing
                the file.
        """
        file_path.parent.mkdir(parents=True, exist_ok=True)

        with open(file_path, "w") as file:
            file.writelines(f"{line}\n" for line in data if line)

write(data, file_path)

Writes a list of strings to a YOLO-formatted text file.

This method automatically creates any missing parent directories. It filters out empty lines and adds a newline character after each record.

Parameters:

Name Type Description Default
data List[str]

A list of strings, where each string represents an object (class_id, x_center, y_center, width, height).

required
file_path Path

The target file path where the annotation will be saved.

required

Returns:

Name Type Description
None None

This method does not return any value.

Raises:

Type Description
IOError

If there is a problem creating the directory or writing the file.

Source code in tools/annotation_converter/writer/yolo.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
def write(self, data: List[str], file_path: Path) -> None:
    """
    Writes a list of strings to a YOLO-formatted text file.

    This method automatically creates any missing parent directories.
    It filters out empty lines and adds a newline character after
    each record.

    Args:
        data (List[str]): A list of strings, where each string represents
            an object (class_id, x_center, y_center, width, height).
        file_path (Path): The target file path where the annotation
            will be saved.

    Returns:
        None: This method does not return any value.

    Raises:
        IOError: If there is a problem creating the directory or writing
            the file.
    """
    file_path.parent.mkdir(parents=True, exist_ok=True)

    with open(file_path, "w") as file:
        file.writelines(f"{line}\n" for line in data if line)