Skip to content

voc-to-yolo: Convert Pascal VOC to YOLO Format

The voc-to-yolo command converts Pascal VOC annotation XMLs to YOLO format, which is commonly used for training object detection models like YOLOv3, YOLOv4, and YOLOv5.

Usage

m3-download voc-to-yolo INPUT_DIR [INPUT_DIR...] [--output-dir OUTPUT_DIR] [--yolo-names YOLO_NAMES]

Parameters

  • INPUT_DIR: One or more directories containing Pascal VOC XML files
  • --output-dir: (Optional) Output directory for YOLO annotations (default: "yolo_localizations")
  • --yolo-names: (Optional) Path to an existing yolo.names file to use for class name mapping

Output

  • OUTPUT_DIR/labels/: Directory containing YOLO annotation files (one .txt file per input XML)
  • OUTPUT_DIR/yolo.names: File containing all class names sorted alphabetically (unless provided via --yolo-names)

Directory Structure

The command creates a YOLO-friendly directory structure with all annotation files in a 'labels' subdirectory and a 'yolo.names' file at the root level. If you provide a yolo.names file, its mapping will be used for class IDs.

YOLO Format Explanation

YOLO annotations are simple text files with one line per object:

<class_id> <x_center> <y_center> <width> <height>
  • <class_id>: Integer ID of the object class (starting from 0)
  • <x_center>: X-coordinate of the object center, relative to image width (0 to 1)
  • <y_center>: Y-coordinate of the object center, relative to image height (0 to 1)
  • <width>: Width of the object bounding box, relative to image width (0 to 1)
  • <height>: Height of the object bounding box, relative to image height (0 to 1)

Example

Convert VOC XMLs using an existing class mapping:

m3-download voc-to-yolo --output-dir Sebastes_yolo/ --yolo-names my_yolo.names Sebastes_voc_1/ Sebastes_voc_2/

Convert VOC XMLs and auto-generate the class mapping:

m3-download voc-to-yolo --output-dir Sebastes_yolo/ Sebastes_voc_1/ Sebastes_voc_2/