1. 将目标检测结果保存为xml格式
headstr = """\
<annotation><folder>VOC</folder><filename>%s</filename><source><database>My Database</database><annotation>COCO</annotation><image>flickr</image><flickrid>NULL</flickrid></source><owner><flickrid>NULL</flickrid><name>company</name></owner><size><width>%d</width><height>%d</height><depth>%d</depth></size><segmented>0</segmented>
"""
objstr = """\<object><name>%s</name><pose>Unspecified</pose><truncated>0</truncated><difficult>0</difficult><bndbox><xmin>%d</xmin><ymin>%d</ymin><xmax>%d</xmax><ymax>%d</ymax></bndbox></object>
"""tailstr = '''\
</annotation>
'''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)height = image.shape[0] width = image.shape[1] 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_pathcur_json_dict["imageData"] = Nonecur_json_dict["imageHeight"] = image.shape[0]cur_json_dict["imageWidth"] = image.shape[1]