最近用到json格式数据集转成voc歌手,记录一下。
coco转voc可在下面代码改动即可
import os, sys
import glob
from PIL import Image
import json
import cv2
import random
out_xml_dir = 'xml/Annotations/' ###指向voc数据集的Annotations文件夹
import pycocotools.coco as coco
# (x1,y1,w,h)
json_file = '../aluminum/annotations/train.json'
img_root ='../aluminum/images'
coco = coco.COCO(json_file) # 读取json信息
images = coco.getImgIds() # 获取所有图片id
for img_id in images:
file_name = coco.loadImgs(ids=[img_id])[0]['file_name'] # 获取一张图片文件名
img_path = os.path.join(img_root, file_name) # 图片绝对路径
ann_ids = coco.getAnnIds(imgIds=[img_id]) # 获取这张图片下所有box的id
anns = coco.loadAnns(ids=ann_ids) # 获取这张图片下所有box信息
p1=file_name.find('images/')
p2=file_name.find('.jpg')
img_name=file_name[p1+7:p2]
con = coco.loadImgs(img_id)
w, h = con[0]['width'], con[0]['height']
# write in xml file
xml_file = open((out_xml_dir + '/' + img_name + '.xml'), 'w')
xml_file.write('<annotation>\n')
xml_file.write(' <folder>VOC2007</folder>\n')
xml_file.write(' <filename>' + str(img_name) + '.jpg' + '</filename>\n')
xml_file.write(' <path>' + str(img_path) + '.jpg' + '</path>\n')
xml_file.write(' <size>\n')
xml_file.write(' <width>' + str(w) + '</width>\n')
xml_file.write(' <height>' + str(h) + '</height>\n')
xml_file.write(' <depth>1</depth>\n')
xml_file.write(' </size>\n')
xml_file.write(' <segmented>0</segmented>\n')
for i in range(len(anns)):
bbox = anns[i]['bbox']
a1 = (bbox[0], bbox[1])
bbox[2], bbox[3] = bbox[0] + bbox[2], bbox[1] + bbox[3]
b1 = (bbox[2], bbox[3])
xmin, ymin, xmax, ymax = a1[0], a1[1], b1[0], b1[1]
gt_label=anns[i]['category_id']
print(gt_label)
xmin, ymin, xmax, ymax = int(xmin), int(ymin), int(xmax), int(ymax)
xml_file.write(' <object>\n')
xml_file.write(' <name>' + str(gt_label) + '</name>\n')
xml_file.write(' <pose>Unspecified</pose>\n')
xml_file.write(' <truncated>0</truncated>\n')
xml_file.write(' <difficult>0</difficult>\n')
xml_file.write(' <bndbox>\n')
xml_file.write(' <xmin>' + str(xmin) + '</xmin>\n')
xml_file.write(' <ymin>' + str(ymin) + '</ymin>\n')
xml_file.write(' <xmax>' + str(xmax) + '</xmax>\n')
xml_file.write(' <ymax>' + str(ymax) + '</ymax>\n')
xml_file.write(' </bndbox>\n')
xml_file.write(' </object>\n')
xml_file.write('</annotation>')
xml_file.close()
#
#