This is the set of tiny python scripts to convert coco json to other format. The following formats are supported:
- Convert to XML used by Pascal VOC
- Convert to CSV used by keras-retinanet
Python 3.6, cocoapi (pycocotools), pillow (coco_to_xml)
project
│ coco_to_csv.py
│ coco_to_xml.py
│ train.json
│ val.json
│
└───train
│ │ image001.jpg
│ │ image002.jpg
│ │ ...
│
└───val
│ image001.jpg
│ image002.jpg
│ ...
The directory structure described below is the same as above (train.json and the image files in train folder).
coco_to_xml.py
script can convert coco json to xml files. AND IT IS FAST (a few seconds for thousands of images)!
python coco_to_xml.py --coco_json=train.json --coco_folder=train --save_xml=train_xml --database_name=Database
By default, this script skip background category (class) because usually it is not required. If you want to add background category, use this flag.
python coco_to_xml.py .... --no_skip_background
Also you can check the help.
python coco_to_xml.py --help
The annotations xml file format is:
<annotation>
<folder>{image_folder_name}</folder>
<filename>{image_name}</filename>
<source>
<database>{database_name}</database>
</source>
<size>
<width>{width}</width>
<height>{height}</height>
<depth>{depth}</depth>
</size>
<segmented>0</segmented>
<object>
<name>{category_name}</name>
<pose>Unspecified</pose>
<truncated>0</truncated>
<difficult>0</difficult>
<bndbox>
<xmin>{xmin}</xmin>
<ymin>{ymin}</ymin>
<xmax>{xmax}</xmax>
<ymax>{ymax}</ymax>
</bndbox>
</object>
</annotation>
And the xml file was saved like this:
project
│ coco_to_xml.py
│ train.json
│ val.json
│
└───train
│ │ image001.jpg
│ │ image002.jpg
│ │ ...
│
└───train_xml
│ image001.xml
│ image002.xml
│ ...
coco_to_xml.py
script can convert coco json to single csv file.
python coco_to_csv.py --coco_json=train.json --coco_folder=train --save_ann=train.csv --save_cat=class.csv
By default, this script skip background category (class) because usually it is not required. If you want to add background category, use this flag.
python coco_to_csv.py .... --no_skip_background
And this script will check image file is exist, you can ignore that using this flag.
python coco_to_csv.py .... --no_check_file
Also you can check the help.
python coco_to_csv.py --help
The annotations csv file format is:
path/to/image.jpg,x1,y1,x2,y2,class_name
The classes csv file format is:
class_name,id