Teeradej Sawettraporn
commited on
Upload coco_to_xml.py
Browse files- coco_to_xml.py +120 -0
coco_to_xml.py
ADDED
|
@@ -0,0 +1,120 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import argparse
|
| 2 |
+
|
| 3 |
+
parser = argparse.ArgumentParser(description='Coco Json to Pascal VOC XML Converter.')
|
| 4 |
+
|
| 5 |
+
parser.add_argument('--coco_json', required=True, help='Target json file to convert to csv.')
|
| 6 |
+
parser.add_argument('--coco_folder', required=False, default='', help='Target folder to find images.')
|
| 7 |
+
parser.add_argument('--save_xml', required=True, help='The folder to save annotations xmls.')
|
| 8 |
+
parser.add_argument('--database_name', required=False, default='', help='The name of database.')
|
| 9 |
+
parser.add_argument('--no_skip_background', dest='skip_background', action='store_false', help='Do not skip \'background\' category.')
|
| 10 |
+
parser.set_defaults(skip_background=True)
|
| 11 |
+
|
| 12 |
+
args = parser.parse_args()
|
| 13 |
+
|
| 14 |
+
from pycocotools.coco import COCO
|
| 15 |
+
from PIL import Image
|
| 16 |
+
from pathlib import Path
|
| 17 |
+
import os
|
| 18 |
+
|
| 19 |
+
def write_to_xml(image_name, bboxes, image_folder_name, data_folder, save_folder, database_name):
|
| 20 |
+
|
| 21 |
+
with Image.open(os.path.join(data_folder, image_name)) as img:
|
| 22 |
+
width, height = img.size
|
| 23 |
+
if img.mode == 'YCbCr':
|
| 24 |
+
depth = 3
|
| 25 |
+
else:
|
| 26 |
+
depth = len(img.mode)
|
| 27 |
+
|
| 28 |
+
objects = ''
|
| 29 |
+
|
| 30 |
+
for bbox in bboxes:
|
| 31 |
+
objects = objects + '''
|
| 32 |
+
<object>
|
| 33 |
+
<name>{category_name}</name>
|
| 34 |
+
<pose>Unspecified</pose>
|
| 35 |
+
<truncated>0</truncated>
|
| 36 |
+
<difficult>0</difficult>
|
| 37 |
+
<bndbox>
|
| 38 |
+
<xmin>{xmin}</xmin>
|
| 39 |
+
<ymin>{ymin}</ymin>
|
| 40 |
+
<xmax>{xmax}</xmax>
|
| 41 |
+
<ymax>{ymax}</ymax>
|
| 42 |
+
</bndbox>
|
| 43 |
+
</object>'''.format(
|
| 44 |
+
category_name = bbox[0],
|
| 45 |
+
xmin = bbox[1],
|
| 46 |
+
ymin = bbox[2],
|
| 47 |
+
xmax = bbox[3],
|
| 48 |
+
ymax = bbox[4]
|
| 49 |
+
)
|
| 50 |
+
|
| 51 |
+
xml = '''<annotation>
|
| 52 |
+
<folder>{image_folder_name}</folder>
|
| 53 |
+
<filename>{image_name}</filename>
|
| 54 |
+
<source>
|
| 55 |
+
<database>{database_name}</database>
|
| 56 |
+
</source>
|
| 57 |
+
<size>
|
| 58 |
+
<width>{width}</width>
|
| 59 |
+
<height>{height}</height>
|
| 60 |
+
<depth>{depth}</depth>
|
| 61 |
+
</size>
|
| 62 |
+
<segmented>0</segmented>{objects}
|
| 63 |
+
</annotation>'''.format(
|
| 64 |
+
image_folder_name = image_folder_name,
|
| 65 |
+
image_name = image_name,
|
| 66 |
+
database_name = database_name,
|
| 67 |
+
width = width,
|
| 68 |
+
height = height,
|
| 69 |
+
depth = depth,
|
| 70 |
+
objects = objects
|
| 71 |
+
)
|
| 72 |
+
|
| 73 |
+
anno_path = os.path.join(save_folder, os.path.splitext(image_name)[0] + '.xml')
|
| 74 |
+
|
| 75 |
+
with open(anno_path, 'w') as file:
|
| 76 |
+
file.write(xml)
|
| 77 |
+
|
| 78 |
+
Path(args.save_xml).mkdir(parents=True, exist_ok=True)
|
| 79 |
+
|
| 80 |
+
image_folder_name = os.path.basename(os.path.abspath(args.coco_folder))
|
| 81 |
+
|
| 82 |
+
coco = COCO(args.coco_json)
|
| 83 |
+
|
| 84 |
+
imgIds = coco.getImgIds()
|
| 85 |
+
|
| 86 |
+
if not args.database_name:
|
| 87 |
+
args.database_name = image_folder_name
|
| 88 |
+
|
| 89 |
+
print('Write annotations file...')
|
| 90 |
+
|
| 91 |
+
total = len(imgIds)
|
| 92 |
+
now = 1
|
| 93 |
+
|
| 94 |
+
for imgId in imgIds:
|
| 95 |
+
|
| 96 |
+
img = coco.loadImgs(imgId)[0]
|
| 97 |
+
|
| 98 |
+
if not os.path.isfile(os.path.join(args.coco_folder, img['file_name'])):
|
| 99 |
+
print('The image {} is not exist.'.format(img['file_name']))
|
| 100 |
+
exit()
|
| 101 |
+
|
| 102 |
+
anno_list = []
|
| 103 |
+
|
| 104 |
+
annIds = coco.getAnnIds(imgIds=imgId)
|
| 105 |
+
|
| 106 |
+
for annId in annIds:
|
| 107 |
+
ann = coco.loadAnns(annId)[0]
|
| 108 |
+
cat = coco.loadCats(ann['category_id'])[0]
|
| 109 |
+
|
| 110 |
+
if cat['name'] == 'background' and args.skip_background == True:
|
| 111 |
+
continue
|
| 112 |
+
|
| 113 |
+
box = ann['bbox']
|
| 114 |
+
anno_list.append([cat['name'], int(box[0]), int(box[1]), int(box[0]+box[2]), int(box[1]+box[3])])
|
| 115 |
+
|
| 116 |
+
write_to_xml(img['file_name'], anno_list, image_folder_name, args.coco_folder, args.save_xml, args.database_name)
|
| 117 |
+
print('Write xml files ({} / {})'.format(now, total))
|
| 118 |
+
now = now + 1
|
| 119 |
+
|
| 120 |
+
print('Annotations file was written!')
|