Skip to content

TXT reader

Bases: BaseReader

A reader class for parsing YOLO-style text files (.txt).

This class reads raw text data from YOLO annotations or class list files and transforms the content into a dictionary format suitable for DataForge processing.

Source code in tools/annotation_converter/reader/yolo.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
29
30
31
32
33
34
35
class TXTReader(BaseReader):
    """
    A reader class for parsing YOLO-style text files (.txt).

    This class reads raw text data from YOLO annotations or class list files
    and transforms the content into a dictionary format suitable for
    DataForge processing.
    """

    def read(self, file_path: Path) -> dict:
        """
        Reads a YOLO text file and converts its lines into a dictionary.

        Each non-empty line in the file becomes a key in the dictionary,
        and its line position (index as a string) becomes the corresponding value.

        Args:
            file_path (Path): The path to the source .txt annotation file.

        Returns:
            dict: A dictionary where keys are text lines and values are their
                string indices.

        Raises:
            FileNotFoundError: If the .txt file cannot be found at the given path.
        """
        with open(file_path, "r") as file:
            text = file.read()

        data = {key: str(value) for value, key in enumerate(text.split("\n")) if key}
        return data

read(file_path)

Reads a YOLO text file and converts its lines into a dictionary.

Each non-empty line in the file becomes a key in the dictionary, and its line position (index as a string) becomes the corresponding value.

Parameters:

Name Type Description Default
file_path Path

The path to the source .txt annotation file.

required

Returns:

Name Type Description
dict dict

A dictionary where keys are text lines and values are their string indices.

Raises:

Type Description
FileNotFoundError

If the .txt file cannot be found at the given path.

Source code in tools/annotation_converter/reader/yolo.py
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
def read(self, file_path: Path) -> dict:
    """
    Reads a YOLO text file and converts its lines into a dictionary.

    Each non-empty line in the file becomes a key in the dictionary,
    and its line position (index as a string) becomes the corresponding value.

    Args:
        file_path (Path): The path to the source .txt annotation file.

    Returns:
        dict: A dictionary where keys are text lines and values are their
            string indices.

    Raises:
        FileNotFoundError: If the .txt file cannot be found at the given path.
    """
    with open(file_path, "r") as file:
        text = file.read()

    data = {key: str(value) for value, key in enumerate(text.split("\n")) if key}
    return data