把xml数据集 转为 BDD100K 的json数据格式

注意:本文所表示的json格式并非coco数据集格式,而是自动驾驶数据集BDD100K的标注。

请先确认再看全文!!!!!

一、修改数据集读取位置

5~9行

 二、修改类别

33~34行,若有标注类别名称想改变,可修改类别名称,不用可注释掉。

 

三、运行程序

python xml_bddjson.py

全部可运行代码如下:

import os
import json
import xml.etree.ElementTree as ET

Root = "/home/wtj/Data/red_mount/6979_detect_annotation/val/"  # 数据集读取位置
xml_dir = Root + "labels/"       # xml标注位置
img_dir = Root + "images/"       # 图片读取位置

annotation_dir = Root + "det_annotaions/"   # json 输出位置

def mkdir(path):
    if not os.path.exists(path):
        os.mkdir(path)

def write_json(xml_path, anno_path):
    objects = []
    tree = ET.parse(xml_path)
    root = tree.getroot()
    if root.tag != "annotation":
        raise Exception("xml should be like voc ...")
    file_name = root.findtext("filename")[:-4]
    
    # attributes (交通灯时修改,其他默认)
    attributes = dict()
    attributes["occluded"] = True
    attributes["truncated"] = False
    attributes["trafficLightColor"] = "none"
    objs = root.findall("object")
    
    id = 0
    for obj in objs:
        category = obj[0].text
        if category == "truck":
            category = "car"

        bbox_j = dict()
        box = [int(obj[4][i].text) for i in range(4)]
        bbox_j["x1"] = box[0]
        bbox_j["y1"] = box[1]
        bbox_j["x2"] = box[2]
        bbox_j["y2"] = box[3]

        # print(box)
        obj_j = dict()
        obj_j["category"]   = category
        obj_j["id"]         = id 
        obj_j["attributes"] = attributes
        obj_j["box2d"]      = bbox_j

        objects.append(obj_j)
        id = id + 1
        # print(id)
    objects_j = objects
    label_json = {
        "name": file_name,
        "frames": [{
            "timestamp": 1000,
            "objects": objects_j
        }],
        "attributes":{
                    "weather": "undefined",
                    "scene": "city street",
                    "timeofday": "daytime"
        }
    }
    json_path = os.path.join(anno_path, file_name+ ".json" )
    with open(json_path,"w") as file:
        file.write(json.dumps(label_json,indent=4))


def main():
    mkdir(annotation_dir)
    img_list = os.listdir(img_dir)
    for img_name in img_list:
        name = img_name[0:-4]
        xml_path = os.path.join(xml_dir, name+".xml")
        print("reading     {}   ...".format(xml_path))
        write_json(xml_path, annotation_dir)



if __name__ == "__main__":
    main()

最后请看,知识就是力量,请放心使用~~~

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值