清华Tinghua100K交通标志数据集.json标签转.xml(python代码)

清华Tinghua100K交通标志数据集.json标签转.xml

数据集连接:Tinghua100K
转成与labelImg标注生成文件相同的XML格式(位置仅保留了bbox)

import json
import xml.dom.minidom
import cv2

img_path='F:/tt100k_dataset/data/train/' #train图片路径
annos = json.loads(open("F:/tt100k_dataset/data/annotations.json").read())
xml_path = 'F:/tt100k_dataset/data/xml_train/'#xml保存路径

for line in open(img_path+"ids.txt"):
    img_name=line.replace('\n','')
    img_file=img_name + '.jpg'
    img = cv2.imread(img_path + img_file)
    sp = img.shape
    img_height = str(sp[0])  # height(rows) of image
    img_width = str(sp[1])

    doc = xml.dom.minidom.Document()
    # creat a root node which name is annotation
    annotation = doc.createElement('annotation')
    # add the root node to the dom document object
    doc.appendChild(annotation)

    # add the folder subnode
    folder = doc.createElement('folder')
    folder_text = doc.createTextNode('JPEGImages')
    folder.appendChild(folder_text)
    annotation.appendChild(folder)

    # add the filename subnode
    filename = doc.createElement('filename')
    filename_text = doc.createTextNode(img_file)
    filename.appendChild(filename_text)
    annotation.appendChild(filename)

    # add the path subnode
    path = doc.createElement('path')
    path_text = doc.createTextNode(
        img_path + img_file)
    path.appendChild(path_text)
    annotation.appendChild(path)

    # add the source subnode
    source = doc.createElement('source')
    database = doc.createElement('database')
    database_text = doc.createTextNode('Unknown')
    source.appendChild(database)
    database.appendChild(database_text)
    annotation.appendChild(source)

    # add the size subnode
    size = doc.createElement('size')
    width = doc.createElement('width')
    width_text = doc.createTextNode(img_width)
    height = doc.createElement('height')
    height_text = doc.createTextNode(img_height)
    depth = doc.createElement('depth')
    depth_text = doc.createTextNode('3')
    size.appendChild(width)
    width.appendChild(width_text)
    size.appendChild(height)
    height.appendChild(height_text)
    size.appendChild(depth)
    depth.appendChild(depth_text)
    annotation.appendChild(size)

    segmented = doc.createElement('segmented')
    segmented_text = doc.createTextNode('0')
    segmented.appendChild(segmented_text)
    annotation.appendChild(segmented)

    img_objects = annos["imgs"][img_name]['objects']
    for i in range(0,len(img_objects)):
        obj_category=annos["imgs"][img_name]['objects'][i]['category']
        obj_bbox=annos["imgs"][img_name]['objects'][i]['bbox']
        bbox_ymin = int(annos["imgs"][img_name]['objects'][i]['bbox']['ymin'])
        bbox_xmin = int(annos["imgs"][img_name]['objects'][i]['bbox']['xmin'])
        bbox_ymax = int(annos["imgs"][img_name]['objects'][i]['bbox']['ymax'])
        bbox_xmax = int(annos["imgs"][img_name]['objects'][i]['bbox']['xmax'])
        print(obj_category,bbox_ymin,bbox_xmin,bbox_ymax,bbox_xmax)

        object = doc.createElement('object')
        name = doc.createElement('name')
        name_text = doc.createTextNode(obj_category)
        difficult = doc.createElement('difficult')
        difficult_text = doc.createTextNode('0')
        pose = doc.createElement('pose')
        pose_text = doc.createTextNode('Unspecified')
        truncated = doc.createElement('truncated')
        truncated_text = doc.createTextNode('0')
        bndbox = doc.createElement('bndbox')
        xmin = doc.createElement('xmin')
        xmin_text = doc.createTextNode(str(bbox_xmin))
        ymin = doc.createElement('ymin')
        ymin_text = doc.createTextNode(str(bbox_ymin))
        xmax = doc.createElement('xmax')
        xmax_text = doc.createTextNode(str(bbox_xmax))
        ymax = doc.createElement('ymax')
        ymax_text = doc.createTextNode(str(bbox_ymax))
        object.appendChild(name)
        name.appendChild(name_text)
        object.appendChild(pose)
        pose.appendChild(pose_text)
        object.appendChild(truncated)
        truncated.appendChild(truncated_text)
        object.appendChild(difficult)
        difficult.appendChild(difficult_text)
        object.appendChild(bndbox)
        bndbox.appendChild(xmin)
        xmin.appendChild(xmin_text)
        bndbox.appendChild(ymin)
        ymin.appendChild(ymin_text)
        bndbox.appendChild(xmax)
        xmax.appendChild(xmax_text)
        bndbox.appendChild(ymax)
        ymax.appendChild(ymax_text)
        annotation.appendChild(object)
    fp = open(xml_path + '%s.xml' % img_name, 'w+')
    doc.writexml(fp, indent='\t', addindent='\t', newl='\n', encoding='utf-8')
    #print(annos["imgs"][img_name]['objects'])
fp.close()
  • 4
    点赞
  • 31
    收藏
    觉得还不错? 一键收藏
  • 8
    评论
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值