python将json数据集转成voc xml文件

代码

# --------------------------------------------------------
# Written by YuTing Zhang, based on python
# json file transform to xml file automatically
# --------------------------------------------------------
import json
import os

headstr = """\

    VOC
    %s
    zhangyt
    
        My Database
    
    
        %d
        %d
        %d
    
    0
"""
objstr = """\
    
        %s
        Unspecified
        0
        0
        
            %d
            %d
            %d
            %d
        
    
"""
 
tailstr = '''\

'''

def write_xml(anno_path,head, objs, tail):
    f = open(anno_path, "w")
    f.write(head)
    for obj in objs:
        f.write(objstr%(obj[0],obj[1],obj[2],obj[3],obj[4]))
    f.write(tail)


def json_to_xml(json_path,xml_path):
    if not os.path.exists(xml_path):
        os.makedirs(xml_path)
    dir = os.listdir(json_path)
    for file in dir:
        file_list=file.split(".")
        with open(os.path.join(json_path,file), 'r') as load_f:
            load_dict = json.load(load_f)

        asset = load_dict["asset"]
        regions = load_dict["regions"]


        #headstr
        filename = asset["name"]
        width = asset["size"]["width"]
        height = asset["size"]["height"]
        depth =  3

        if "png" in filename:
            xml_name = filename.replace("png", "xml")
        elif "jpg" in filename:
            xml_name = filename.replace("jpg", "xml")

        head=headstr % (filename, width, height, depth)
        
        #objstr
        dataset = []
        for region in regions:
            print(region)
            name = region["tags"][0]
            # print(name)
            boundingBox = region["boundingBox"]
            print(boundingBox)
            xmin = boundingBox["left"]
            ymin = boundingBox['top']
            xmax = xmin + boundingBox["width"]
            ymax = ymin + boundingBox["height"]

            dataset.append([name, xmin, ymin, xmax, ymax])
        tail = tailstr

        write_xml(os.path.join(xml_path, xml_name),head, dataset, tail)


if __name__ == '__main__':

    json_path="Helm500_res/"  #该目录为存放json文件的路径  
    xml_path="Helm500_res/voc_annotation"   #该目录为放xml文件的路径
    json_to_xml(json_path,xml_path)

 

json文件

{
    "asset": {
        "format": "png",
        "id": "00a232ba6c87727cccd3466624f2f26f",
        "name": "vlcsnap-2020-06-16-14h28m31s653.png",
        "path": "file:D:/%E7%AE%97%E6%B3%95%E6%95%B0%E6%8D%AE/tasks/Helm500/vlcsnap-2020-06-16-14h28m31s653.png",
        "size": {
            "width": 640,
            "height": 360
        },
        "state": 2,
        "type": 1
    },
    "regions": [
        {
            "id": "Ue8qq4rhM",
            "type": "RECTANGLE",
            "tags": [
                "helmet_on"
            ],
            "boundingBox": {
                "height": 20.95432959232234,
                "width": 18.024723477640343,
                "left": 548.902012963844,
                "top": 317.2385805391418
            },
            "points": [
                {
                    "x": 548.902012963844,
                    "y": 317.2385805391418
                },
                {
                    "x": 566.9267364414843,
                    "y": 317.2385805391418
                },
                {
                    "x": 566.9267364414843,
                    "y": 338.1929101314642
                },
                {
                    "x": 548.902012963844,
                    "y": 338.1929101314642
                }
            ]
        },
        {
            "id": "0DlBLBCWC",
            "type": "RECTANGLE",
            "tags": [
                "person"
            ],
            "boundingBox": {
                "height": 46.65991148972875,
                "width": 54.074244766888675,
                "left": 516.74974726451,
                "top": 313.3400885102713
            },
            "points": [
                {
                    "x": 516.74974726451,
                    "y": 313.3400885102713
                },
                {
                    "x": 570.8239920313987,
                    "y": 313.3400885102713
                },
                {
                    "x": 570.8239920313987,
                    "y": 360.00000000000006
                },
                {
                    "x": 516.74974726451,
                    "y": 360.00000000000006
                }
            ]
        },
        {
            "id": "gtACiHR0v",
            "type": "RECTANGLE",
            "tags": [
                "person"
            ],
            "boundingBox": {
                "height": 90.81534249441964,
                "width": 27.580803134182176,
                "left": 354.03721076150833,
                "top": 214.97299460168497
            },
            "points": [
                {
                    "x": 354.03721076150833,
                    "y": 214.97299460168497
                },
                {
                    "x": 381.6180138956905,
                    "y": 214.97299460168497
                },
                {
                    "x": 381.6180138956905,
                    "y": 305.7883370961046
                },
                {
                    "x": 354.03721076150833,
                    "y": 305.7883370961046
                }
            ]
        }
    ],
    "version": "2.1.0"
}

xml文件


    VOC
    vlcsnap-2020-06-16-14h28m31s653.png
    zhangyt
    
        My Database
    
    
        640
        360
        3
    
    0
    
        helmet_on
        Unspecified
        0
        0
        
            548
            317
            566
            338
        
    
    
        person
        Unspecified
        0
        0
        
            516
            313
            570
            360
        
    
    
        person
        Unspecified
        0
        0
        
            354
            214
            381
            305
        
    

 

你可能感兴趣的:(2020非专栏)