xml (pascal voc)格式转 otb格式

该博客主要涉及XML文件的解析与处理,通过python的ElementTree库读取XML文件,提取特定元素并进行检查。内容包括获取XML中'object'和'bndbox'等元素的坐标信息,用于构建一个JSON字典,该字典包含视频目录、初始矩形、图像名称、目标矩形和属性等字段。最终将整理后的数据写入JSON文件。
摘要由CSDN通过智能技术生成
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--------------")
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

April leander

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值