YOLO annotation adapter.

This adapter is tested with the YOLOv3 Pytorch and YOLOv5 Pytorch repositories. Bounding boxes are normalized between [0,1]. Images should be in a separate folder (named e.g. images/). Annotations should be in a separate folder (named e.g. labels/) and must have the same file name as corresponding image source file but with the ending .txt. The categories.txt file in the parent folder list all category labels which position (index) is used to label the bounding box class (see Annotation Format paragraph below). Below is an example structure:

categories.txt
images/
    image1.jpg
labels/
    image1.txt

Supported annotations:

  • rectangle

Bounding Box Format

The bounding box values are normalized between 0 and 1.

  • Normalization Formula: x / image_width or y / image height
  • Normalization Formula: x * image_width or y * image height

A bounding box has the following 4 parameters:

  • Center X (bx)
  • Center Y (by)
  • Width (bw)
  • Height (bh)

YOLO-BoundingBox
YOLO bounding box parameters.

Annotation Format

Each annotation file (e.g. image1.txt) is a space separated CSV file where every row represents a bounding box in the image. A row has the following format: <class_number> <center_x> <center_y> <width> <height>

  • class_number: The index of the class as listed in categories.txt
  • center_x: The normalized bounding box center x value
  • center_y: The normalized bounding box center y value
  • width: The normalized bounding box width value
  • height: The normalized bounding box height value

Parameters

The adapter has the following parameters:

  • --path: the path to the base folder containing the annotations (e.g.: data/object_detection/my_collection)
  • --categories_file_name: tThe path to the categories file if not set, default to categories.txt
  • --images_folder_name: the name of the folder containing the image files, if not set, default to images
  • --annotations_folder_name: The name of the folder containing the image annotations, if not set, default to labels

class YOLOAnnotationAdapter[source]

YOLOAnnotationAdapter(path, categories_file_name=None, images_folder_name=None, annotations_folder_name=None) :: AnnotationAdapter

Adapter to read and write annotations in the YOLO format.

AnnotationAdapter.list_files[source]

AnnotationAdapter.list_files(subset_type=<SubsetType.TRAINVAL: 'trainval'>)

List all physical files in a sub-set. subset_type: the subset type to list return: a list of file paths if subset type exist, else an empty list

YOLOAnnotationAdapter.read_annotations[source]

YOLOAnnotationAdapter.read_annotations(subset_type=<SubsetType.TRAINVAL: 'trainval'>)

Reads YOLO annotations. subset_type: the subset type to read return: the annotations as dictionary

AnnotationAdapter.read_categories[source]

AnnotationAdapter.read_categories()

Read categories. return: a list of category names

AnnotationAdapter.write_files[source]

AnnotationAdapter.write_files(file_paths, subset_type=<SubsetType.TRAINVAL: 'trainval'>)

Write physical files in a sub-set. file_paths: a list of file paths to write subset_type: the subset type to write into return: a list of written target file paths

YOLOAnnotationAdapter.write_annotations[source]

YOLOAnnotationAdapter.write_annotations(annotations, subset_type=<SubsetType.TRAINVAL: 'trainval'>)

Writes YOLO annotations to the annotations folder and copy the corresponding source files. annotations: the annotations as dictionary subset_type: the subset type to write return: a list of written target file paths

AnnotationAdapter.write_categories[source]

AnnotationAdapter.write_categories(categories)

Write categories. categories: a list of category names

YOLOAnnotationAdapter.argparse[source]

YOLOAnnotationAdapter.argparse(prefix=None)

Returns the argument parser containing argument definition for command line use. prefix: a parameter prefix to set, if needed return: the argument parser