# How to convert PASCAL VOC to YOLO

## Issue

I was trying to develop some way to convert annotations between formats, and it’s quit hard to find information but here I have :

This one is PASCAL VOC

``````<width>800</width>
<height>450</height>
<depth>3</depth>
<bndbox>
<xmin>474</xmin>
<ymin>2</ymin>
<xmax>726</xmax> <!-- shape_width = 252  -->
<ymax>449</ymax> <!-- shape_height = 447 -->
</bndbox>
``````

convert to YOLO darknet

``````2 0.750000 0.501111 0.315000 0.993333
``````

note initial `2` it’s a category

## Solution

using some math: (also can be useful to COCO)

``````categ_index [(xmin + xmax) / 2 / image_width] [(ymin + ymax) / 2 / image_height] [(xmax - xmin) / image_width] [(ymax  - ymin) / image_height]
``````

in js code

``````const categ_index = 2;

const { width: image_width, height: image_height } = {
width: 800,
height: 450,
};

const { xmin, ymin, xmax, ymax } = {
xmin: 474,
ymin: 2,
xmax: 727,
ymax: 449,
};

const x_coord = (xmin + xmax) / 2 / image_width;

const y_coord = (ymin + ymax) / 2 / image_height;

const shape_width = (xmax - xmin) / image_width;

const shape_height = (ymax - ymin) / image_height;

console.log(`\${categ_index} \${x_coord.toFixed(7)} \${y_coord.toFixed(7)} \${shape_width.toFixed(7)} \${shape_height.toFixed(7)}`);

// output
// 2 0.7506250 0.5011111 0.3162500 0.9933333``````