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)
# print(vars)
# print(len(vars))
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]
filename = os.path.basename(xml_f)[:-4]
name = "image055"
json_dict['video_dir'].append(name)
tree = ET.parse(xml_f)
root = tree.getroot()
# print(root)
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):
#print("ai is ",ai)
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)
# print("name",name)
json_dict=convert(xml_list)
# print(json_dict)
otb['image055'] = json_dict
json_fp = open(json_file, 'a')
#json_str = json.dumps(json_dict)
json_str = json.dumps(otb)
json_fp.write(json_str)
json_fp.close()
print("------------create {} done--------------")
xml (pascal voc)格式转 otb格式
最新推荐文章于 2022-08-25 19:22:38 发布
该博客主要涉及XML文件的解析与处理,通过python的ElementTree库读取XML文件,提取特定元素并进行检查。内容包括获取XML中'object'和'bndbox'等元素的坐标信息,用于构建一个JSON字典,该字典包含视频目录、初始矩形、图像名称、目标矩形和属性等字段。最终将整理后的数据写入JSON文件。
摘要由CSDN通过智能技术生成