• imglab .xml 标签格式转coco .json格式


    用imglab做物体特征点标注,将标注数据转为coco keypoints数据集转换代码:

    import xml.dom.minidom as xmldom
    import os
    import csv
    import json
    
    image_w = 640
    image_h = 640
    image_start_index = 0
    instance_start_index  = 10000
    
    info = {"description": "COCO 2017 Dataset","url": "http://cocodataset.org","version": "1.0","year": 2017,"contributor": "COCO Consortium","date_created": "2017/09/01"}
    licenses = [{"url": "http://creativecommons.org/licenses/by-nc-sa/2.0/","id": 1,"name": "Attribution-NonCommercial-ShareAlike License"},
    {"url": "http://creativecommons.org/licenses/by-nc/2.0/","id": 2,"name": "Attribution-NonCommercial License"},
    {"url": "http://creativecommons.org/licenses/by-nc-nd/2.0/","id": 3,"name": "Attribution-NonCommercial-NoDerivs License"},
    {"url": "http://creativecommons.org/licenses/by/2.0/","id": 4,"name": "Attribution License"},
    {"url": "http://creativecommons.org/licenses/by-sa/2.0/","id": 5,"name": "Attribution-ShareAlike License"},
    {"url": "http://creativecommons.org/licenses/by-nd/2.0/","id": 6,"name": "Attribution-NoDerivs License"},
    {"url": "http://flickr.com/commons/usage/","id": 7,"name": "No known copyright restrictions"},
    {"url": "http://www.usa.gov/copyright.shtml","id": 8,"name": "United States Government Work"}]
    images = []
    annotations = []
    categories = [{"supercategory": "person","id": 1,"name": "person","keypoints": ["nose","left_eye","right_eye","left_ear","right_ear","left_shoulder","right_shoulder","left_elbow","right_elbow","left_wrist","right_wrist","left_hip","right_hip","left_knee","right_knee","left_ankle","right_ankle"],
    "skeleton": []}]
    
     
    xml_filepath = os.path.abspath("/home/jiajie/pytorch/detectron2/datasets/imglab_data/mydata.xml")
    
    ############################################
    # 得到文件对象
    dom_obj = xmldom.parse(xml_filepath)
     
    # 得到元素对象
    element_obj = dom_obj.documentElement
     
    # 获得子标签
     
    sub_element_obj = element_obj.getElementsByTagName("image")
     
    for i in range(len(sub_element_obj)):
        image_start_index = image_start_index + 1
        #images
        image_info = {"license": 1} 
        image_info['file_name'] = str(sub_element_obj[i].getAttribute("file")).split("\")[0]+"_"+str(sub_element_obj[i].getAttribute("file")).split("\")[1]
        #print(image_info['file_name'])
        image_info['coco_url'] = ""
        image_info['height'] = int(image_h)
        image_info['width'] = int(image_w)
        image_info['date_captured'] = ""
        image_info['flickr_url'] = ""
        image_info['id'] = int(image_start_index)
        images.append(image_info) 
        sub_element_obj_box = sub_element_obj[i].getElementsByTagName("box")
        for j in range(len(sub_element_obj_box)):
            instance_start_index = instance_start_index  + 1
            sub_element_obj_keypoints = sub_element_obj_box[j].getElementsByTagName("part")
            # annotations
            annotations_info = {"segmentation":[[]]}
            annotations_info['num_keypoints'] = int(len(sub_element_obj_keypoints))
            annotations_info['area'] = 0.0
            annotations_info['iscrowd'] = 0
            keypoints_dict = {}
            
            for k in range(len(sub_element_obj_keypoints)):
                #print(sub_element_obj_keypoints[k].getAttribute("x"),sub_element_obj_keypoints[k].getAttribute("y"))
                kp_name = str(sub_element_obj_keypoints[k].getAttribute("name"))+"_"+str(sub_element_obj_keypoints[k].getAttribute("x"))+"_"+str(sub_element_obj_keypoints[k].getAttribute("y"))
                keypoints_dict[kp_name] = pow(int(sub_element_obj_keypoints[k].getAttribute("x")),2)+pow(int(sub_element_obj_keypoints[k].getAttribute("y")),2)            
            sort_list = sorted(keypoints_dict.items(),  key=lambda d: d[1], reverse=False)
            #print(sort_list)
            keypoints_list = []  
            #print(int(str(sort_list[0][0]).split("_")[1]))   
            keypoints_list.append(int(str(sort_list[0][0]).split("_")[1]))
            
            keypoints_list.append(int(str(sort_list[0][0]).split("_")[2]))
            keypoints_list.append(int(2))
            keypoints_list.append(int(str(sort_list[1][0]).split("_")[1]))
            keypoints_list.append(int(str(sort_list[1][0]).split("_")[2]))
            keypoints_list.append(int(2))
            keypoints_list.append(int(str(sort_list[2][0]).split("_")[1]))
            keypoints_list.append(int(str(sort_list[2][0]).split("_")[2]))
            keypoints_list.append(int(2))
            keypoints_list.append(int(str(sort_list[3][0]).split("_")[1]))
            keypoints_list.append(int(str(sort_list[3][0]).split("_")[2]))
            keypoints_list.append(int(2) )
            annotations_info['keypoints'] = keypoints_list
            annotations_info['image_id'] = int(image_start_index)
            #print(keypoints_list)
            #bbox
            annotations_info['bbox'] = [int(sub_element_obj_box[j].getAttribute("left")),int(sub_element_obj_box[j].getAttribute("top")),int(sub_element_obj_box[j].getAttribute("width")),int(sub_element_obj_box[j].getAttribute("height"))]
            annotations_info['category_id'] = 1
            annotations_info['id'] = instance_start_index
            annotations.append(annotations_info)
    
    image = {
        'info': info,
        'licenses': licenses,
        'images': images,
        'annotations': annotations,
        'categories': categories
    }
    
    
    with open('/home/jiajie/pytorch/detectron2/datasets/imglab_data/person_keypoints_train2017.json', 'w') as f:
        json.dump(image,f)
    
    
  • 相关阅读:
    Bootstrap入门(二十五)JS插件2:过渡效果
    Bootstrap入门(二十四)data属性
    Bootstrap入门(二十三)JS插件1:模态框
    Bootstrap入门(二十二)组件16:列表组
    Bootstrap入门(二十一)组件15:警告框
    Bootstrap入门(二十)组件14:警告框
    Bootstrap入门(十九)组件13:页头与缩略图
    git 上传本地代码到github 服务器
    Dropzone.js实现文件拖拽上传
    将复杂form表单序列化serialize-object.js
  • 原文地址:https://www.cnblogs.com/jiajiewu/p/12937831.html
Copyright © 2020-2023  润新知