Skip to content

yolo-to-json: Convert YOLO to JSON Format

The yolo-to-json command 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

m3-download yolo-to-json INPUT_DIR NAMES_FILE WIDTH HEIGHT OUTPUT_FILE [--round]

Parameters

  • INPUT_DIR: Directory containing YOLO annotation text files
  • NAMES_FILE: File containing class names, one per line
  • WIDTH: 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

  1. The command reads all YOLO annotation files (.txt) in the input directory
  2. 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)
  3. 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:

m3-download yolo-to-json yolo_annotations/ yolo.names 1920 1080 localizations.json --round

Preserving floating point precision:

m3-download yolo-to-json yolo_annotations/ yolo.names 1920 1080 localizations.json