【工具脚本】目标检测中VOC格式数据转COCO格式数据。亲测!无bug!不报错!
发布日期:2021-05-09 18:28:30 浏览次数:17 分类:精选文章

本文共 5534 字,大约阅读时间需要 18 分钟。

VOC���������������������COCO���������Python������������

������

������������������������VOC���������COCO������������������������������������������������������������Python������������������VOC���������������������COCO���������������������������������������������������

������������

���������������������������������������

  • voc_clses������������������������������������������������������������������������������������������������������������������

  • voc2007xmls���������VOC���������������������XML���������������������������

  • test_txt���������������XML���������������������������������������������������������������������������������������XML���������������������������������������������

  • json_name������������������JSON���������������

  • ������������������

    ���������������������������������������������

    # coding=utf-8
    import xml.etree.ElementTree as ET
    import os
    import json
    # ���������������������������������������
    voc_clses = [
    'none_of_the_above',
    'chepai',
    'chedeng',
    'chebiao',
    'person'
    ]
    categories = []
    for idx, cls in enumerate(voc_clses):
    categories.append({
    'supercategory': cls,
    'name': cls,
    'id': idx
    })
    def getimages(xmlname, index):
    """
    ������������XML���������������������������
    xmlname���XML���������
    index���������������������������������������������ID
    """
    tree = ET.parse(xmlname)
    root = tree.getroot()
    image_info = {
    'file_name': root.find('filename').text.strip(),
    'width': root.find('size').find('width').text.strip(),
    'height': root.find('size').find('height').text.strip(),
    'id': index
    }
    bboxes = []
    for obj in root.find('object'):
    # ������������������
    obj_name = obj.find('name').text.strip()
    # ������������ID
    obj_cls_idx = voc_clses.index(obj_name)
    # ���������������
    bbox = []
    xmin = int(obj.find('bndbox').find('xmin').text)
    ymin = int(obj.find('bndbox').find('ymin').text)
    xmax = int(obj.find('bndbox').find('xmax').text)
    ymax = int(obj.find('bndbox').find('ymax').text)
    bbox.append(xmin)
    bbox.append(ymin)
    bbox.append(xmax - xmin)
    bbox.append(ymax - ymin)
    bbox.append(obj_cls_idx)
    # ���������������������COCO���������������
    bbox.append((xmax - xmin) * (ymax - ymin) - 10.0)
    bboxes.append(bbox)
    return image_info, bboxes
    def txt2list(txtfile):
    """
    ������������������������������������
    txtfile���������������������
    ���������������������
    """
    with open(txtfile, 'r') as f:
    return [line.rstrip() for line in f]
    def main():
    # ������������������������XML������
    txtfile = os.path.join(voc2007xmls, 'Main/test.txt')
    xml_list = txt2list(txtfile)
    # ������������������������
    for f in os.listdir(os.path.join(json_name)):
    os.remove(os.path.join(json_name, f))
    # ���������������������JSON������
    anno_dict = {
    'images': [],
    'categories': categories,
    'annotations': []
    }
    # ������������XML������
    for idx, xml_name in enumerate/xml_list:
    xml_path = os.path.join(voc2007xmls, xml_name + '.xml')
    image_info, bboxes = getimages(xml_path, idx)
    anno_dict['images'].append(image_info)
    anno_dict['annotations'].extend([
    {
    'image_id': box[3],
    'category_id': box[2],
    'bbox': box[:4],
    'area': box[-1],
    'iscrowd': 0
    }
    for box in bboxes
    ])
    # ���������JSON������
    with open(os.path.join(json_name, 'instances_voc2007val.json'), 'w') as f:
    json.dump(anno_dict, f, indent=4)
    if __name__ == '__main__':
    main()

    ������������������

  • ���������������������������������������������voc_clses���������������������������������������������������������
  • ���������������������������������VOC������XML������������������������������������������������������������������������������������������
  • COCO������������������VOC���������������������COCO������������������������������������ID������������������������������������
  • ���������������������������������������XML������������������������������������������������������JSON���������
  • ���������������������������������������������������������������������10���������������COCO���������������
  • ������������������

    ������������������������VOC������������������/data_1/script_file/model_test_script-master/mAp/input/Annotations������XML���������������COCO���������������������������instances_voc2007val.json���������������������������������������

    {
    "images": [
    {
    "file_name": "0001.jpg",
    "width": "1280",
    "height": "720",
    "id": 0
    },
    ...
    ],
    "categories": [
    {
    "supercategory": "person",
    "name": "person",
    "id": 1
    },
    ...
    ],
    "annotations": [
    {
    "image_id": 0,
    "category_id": 1,
    "bbox": [100, 100, 200, 200],
    "area": 10000,
    "iscrowd": 0
    },
    ...
    ]
    }

    ������������������

    ���������������������������������

  • VOC���������������������������
  • Test TXT������������������
  • JSON������������������������
  • ���������������������������������������������
  • ������

    ���������������������������������������������������������VOC���������������COCO������������������������������������������������������������������������������������������������������������

    上一篇:【Linux】Cat指令用法
    下一篇:【Linux】VIM的基本使用

    发表评论

    最新留言

    逛到本站,mark一下
    [***.202.152.39]2025年04月19日 00时28分38秒