convert: Convert Between Annotation Formats¶
The convert command group converts annotations between Pascal VOC, YOLO, and JSON formats.
convert voc-yolo¶
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 convert voc-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 existingyolo.namesfile to use for class name mapping
Output¶
OUTPUT_DIR/labels/: Directory containing YOLO annotation files (one.txtfile 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>: 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)
Examples¶
Convert VOC XMLs using an existing class mapping:
m3-download convert voc-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:
convert yolo-voc¶
Converts YOLO format annotations (text files) to Pascal VOC XML format. Since YOLO annotations don't include image dimensions, corresponding images must be provided to extract this information.
Usage¶
Parameters¶
YOLO_DIR: Directory containing YOLO annotation text filesIMAGE_DIR: Directory containing corresponding images (PNG or JPG)NAMES_FILE: File containing class names, one per lineOUTPUT_DIR: Directory where VOC XML files will be saved--verbose, -v: (Optional) Display additional information about file matching
How It Works¶
- The command matches annotation files with image files by their filename stems
- For each matched pair, it:
- Reads the image dimensions using the
imagesizelibrary - Converts YOLO's normalized coordinates to absolute pixel coordinates
- Creates a Pascal VOC XML using the
pascal_voc_writerlibrary - Saves the XML to the output directory
- Reads the image dimensions using the
File Requirements
- YOLO annotations must have the
.txtfile extension - Image files must have
.pngor.jpgfile extensions - Files are matched based on filename stem (name without extension)
Missing Matches
If an annotation file has no matching image file or vice versa, a warning will be displayed and those files will be skipped.
Examples¶
Basic conversion:
Verbose output to debug file matching issues:
convert yolo-json¶
Converts YOLO format annotations to a more readable and portable JSON format. This can be useful for data examination, sharing, or as an intermediate format for further processing.
Usage¶
Parameters¶
INPUT_DIR: Directory containing YOLO annotation text filesNAMES_FILE: File containing class names, one per lineWIDTH: Image width (for scaling normalized YOLO coordinates)HEIGHT: Image height (for scaling normalized YOLO coordinates)OUTPUT_FILE: Path to save the output JSON file--round: (Optional) Round the scaled coordinates to the nearest whole number
Output Format¶
The JSON output follows this structure:
{
"/absolute/path/to/annotation1.txt": [
{
"concept": "class_name",
"x": 100,
"y": 200,
"width": 50,
"height": 40
},
...
],
"/absolute/path/to/annotation2.txt": [
...
]
}
How It Works¶
- The command reads all YOLO annotation files (
.txt) in the input directory - For each annotation, it converts:
- Class indices to human-readable class names using the provided names file
- Normalized coordinates to absolute pixel coordinates using the provided dimensions
- Center-based coordinates (YOLO) to top-left coordinates (traditional)
- All annotations are compiled into a single JSON file
Coordinate Precision
Use the --round flag when you need integer coordinates (e.g., for displaying in pixel-based applications).
Omit it when you need to preserve the exact floating-point precision (e.g., for mathematical analysis).
Examples¶
Basic conversion with integer coordinates:
Preserving floating point precision: