利用检测结果实现半自动标注

1. 将目标检测结果保存为xml格式

#-----------------------------------------------------------------------------------#
#   下面定义了xml里面的组成模块,无需改动。
#-----------------------------------------------------------------------------------#
headstr = """\

    VOC
    %s
    
        My Database
        COCO
        flickr
        NULL
    
    
        NULL
        company
    
    
        %d
        %d
        %d
    
    0
"""
objstr = """\
    
        %s
        Unspecified
        0
        0
        
            %d
            %d
            %d
            %d
        
    
"""
    
tailstr = '''\

'''

def write_xml(anno_path, jpg_pth, head, input_shape, boxes, unique_labels, tail):
       f = open(anno_path, "w")
       f.write(head%(jpg_pth, input_shape[0], input_shape[1], 3))
       for i, box in enumerate(boxes):
           f.write(objstr%(str(unique_labels[int(box[4])]), box[0], box[1], box[2], box[3]))
       f.write(tail)

2. 将目标检测结果保存为json格式

def write_json (img_path, pred):
    """Plot parking slots on the image."""
    
    cur_json_dict = {
    "version": "5.1.1",
    "flags": {},
    "shapes": [
    ],
    }

    if(len(pred)==0):
        print("img  pred none box",img_path)
        
    image=cv.imread(img_path)
    # marking_points = list(list(zip(*pred_points))[1])
    height = image.shape[0]  # 320 
    width =  image.shape[1]  # 320

        
    for poly in range(len(pred_points)):
                         
        cur_json_dict['shapes'].append(
                {"label": isOccupied, "points": poly, "group_id": None,
                "shape_type": "polygon", "flags": {}})
    
   
    new_json_path = (img_path.split('.')[0]+'.json').replace("image","annotated_label")
    start =os.path.dirname(new_json_path)
    create_folder(start)
    relative_path = "..//img//" + os.path.basename(img_path)
    cur_json_dict["imagePath"] = relative_path
    # cur_json_dict["imageData"] = str(base64.b64encode(open(img_path, "rb").read()))
    # # delete 'b and '
    # cur_json_dict["imageData"] = cur_json_dict["imageData"][2:-1]
    cur_json_dict["imageData"] = None
    cur_json_dict["imageHeight"] = image.shape[0]
    cur_json_dict["imageWidth"] = image.shape[1]

你可能感兴趣的:(深度学习,目标跟踪,人工智能,计算机视觉)