import sys
import os
import json
import glob
import numpy as np
import xml.etree.ElementTree as ET
def get(root,name):
return root.findall(name)
def get_and_check(root,name,length):
vars = root.findall(name)
if len(vars) == 0:
raise NotImplementedError('Can not find %s in %s.'%(name, root.tag))
if length > 0 and len(vars) != length:
raise NotImplementedError('The size of %s is supposed to be %d, but is %d.'%(name, length, len(vars)))
if length == 1:
vars = vars[0]
return vars
def convert(xml_list):
json_dict = {"video_dir":[], "init_rect":[], "image_names": [], "gt_rect": [], "attr": []}
line=xml_list[0]
xml_f = line
filename = os.path.basename(xml_f)[:-4]
name = "image055"
json_dict['video_dir'].append(name)
tree = ET.parse(xml_f)
root = tree.getroot()
obj=get_and_check(root, 'object', 1)
bndbox=get_and_check(obj, 'bndbox', 1)
xmin=int(get_and_check(bndbox, 'xmin', 1).text)
ymin=int(get_and_check(bndbox, 'ymin', 1).text)
xmax=int(get_and_check(bndbox, 'xmax', 1).text)
ymax=int(get_and_check(bndbox, 'ymax', 1).text)
init_bbox=[xmin,ymin,xmax,ymax]
json_dict['init_rect'].append(init_bbox)
ai=0
for index, line in enumerate(xml_list):
ai+=1
xml_f = line
tree = ET.parse(xml_f)
root = tree.getroot()
filename = "image055/img/"+os.path.basename(xml_f)[:-4]+ ".jpg"
json_dict['image_names'].append(filename)
obj=get_and_check(root, 'object', 1)
bndbox=get_and_check(obj, 'bndbox', 1)
xmin=int(get_and_check(bndbox, 'xmin', 1).text)
ymin=int(get_and_check(bndbox, 'ymin', 1).text)
xmax=int(get_and_check(bndbox, 'xmax', 1).text)
ymax=int(get_and_check(bndbox, 'ymax', 1).text)
init_bbox=[xmin,ymin,xmax,ymax]
json_dict['gt_rect'].append(init_bbox)
return json_dict
if __name__ == '__main__':
json_file = './train.json'
num = 55
i = 0
otb= {"image047":{},"image048":{},"image049":{},"image050":{},"image051":{},"image052":{},"image053":{},"image054":{},"image055":{}}
for i in range(1):
xml_dir='./image0%s/Annotations'%str(num)
xml_list = glob.glob(xml_dir + "/*.xml")
xml_list = np.sort(xml_list)
name = "image0"+str(num)
json_dict=convert(xml_list)
otb['image055'] = json_dict
json_fp = open(json_file, 'a')
json_str = json.dumps(otb)
json_fp.write(json_str)
json_fp.close()
print("------------create {} done--------------")