【数据集转换】VOC数据集转COCO数据集·代码实现+操作步骤

简介: 与VOC一个文件一个xml标注不同,COCO所有的目标框标注都是放在一个json文件中的。

在自己的数据集上实验时,往往需要将VOC数据集转化为coco数据集,因为这种需求所以才记录这篇文章,代码出处未知,感谢开源。


在远程服务器上测试目标检测算法需要用到测试集,最常用的是coco2014/2017和voc07/12数据集。


coco数据集的地址为http://cocodataset.org/#download


voc和coco的镜像为https://pjreddie.com/projects/pascal-voc-dataset-mirror/

一、数据集格式对比


1.1 VOC数据集


VOC_ROOT     #根目录
    ├── JPEGImages         # 存放源图,(当然图片并不一定要是**.jpg格式的,只是规定文件夹名字叫JPEGImages**);
    │     ├── aaaa.jpg     
    │     ├── bbbb.jpg  
    │     └── cccc.jpg
    ├── Annotations        # 存放xml文件,VOC的标注是xml格式,与JPEGImages中的图片一一对应
    │     ├── aaaa.xml 
    │     ├── bbbb.xml 
    │     └── cccc.xml 
    └── ImageSets          
        └── Main
          ├── train.txt    # txt文件中每一行包含一个图片的名称
          └── val.txt


1.2 COCO数据集


COCO_ROOT     #根目录
    ├── annotations        # 存放json格式的标注
    │     ├── instances_train2017.json   
    │     └── instances_val2017.json
    └── train2017         # 存放图片文件
    │     ├── 000000000001.jpg 
    │     ├── 000000000002.jpg 
    │     └── 000000000003.jpg 
    └── val2017        
          ├── 000000000004.jpg 
          └── 000000000005.jpg 


1.2.3 json标注格式


与VOC一个文件一个xml标注不同,COCO所有的目标框标注都是放在一个json文件中的。


这个json文件解析出来是一个字典,格式如下:


{
  "info": info, 
  "images": [image], 
  "annotations": [annotation], 
  "categories": [categories],
  "licenses": [license],
}


二、转换步骤


2.1 程序总体目录


60948e641b0d40ff8bc485a505a6b2e9.png


33376b544a2a40e7bb70a2a3e871de36.png


2.2  标签文件转换代码实现(xml文件转json格式)VOC_To_CoCo_01.py


这里需要运行三次,因为train.txt val.txt test.txt是三个文件,具体看注释


# VOC_To_CoCo_01.py
import os
import argparse
import json
import xml.etree.ElementTree as ET
from typing import Dict, List
import re
def get_label2id(labels_path: str) -> Dict[str, int]:
    """id is 1 start"""
    with open(labels_path, 'r') as f:
        labels_str = f.read().split()
    labels_ids = list(range(1, len(labels_str) + 1))
    return dict(zip(labels_str, labels_ids))
def get_annpaths(ann_dir_path: str = None,
                 ann_ids_path: str = None,
                 ext: str = '',
                 annpaths_list_path: str = None) -> List[str]:
    # If use annotation paths list
    if annpaths_list_path is not None:
        with open(annpaths_list_path, 'r') as f:
            ann_paths = f.read().split()
        return ann_paths
    # If use annotaion ids list
    ext_with_dot = '.' + ext if ext != '' else ''
    with open(ann_ids_path, 'r') as f:
        ann_ids = f.read().split()
    ann_paths = [os.path.join(ann_dir_path, aid + ext_with_dot) for aid in ann_ids]
    return ann_paths
def get_image_info(annotation_root, extract_num_from_imgid=True):
    path = annotation_root.findtext('path')
    if path is None:
        filename = annotation_root.findtext('filename')
    else:
        filename = os.path.basename(path)
    img_name = os.path.basename(filename)
    img_id = os.path.splitext(img_name)[0]
    if extract_num_from_imgid and isinstance(img_id, str):
        img_id = int(re.findall(r'\d+', img_id)[0])
    size = annotation_root.find('size')
    width = int(size.findtext('width'))
    height = int(size.findtext('height'))
    image_info = {
        'file_name': filename,
        'height': height,
        'width': width,
        'id': img_id
    }
    return image_info
def get_coco_annotation_from_obj(obj, label2id):
    label = obj.findtext('name')
    assert label in label2id, f"Error: {label} is not in label2id !"
    category_id = label2id[label]
    bndbox = obj.find('bndbox')
    xmin = int(bndbox.findtext('xmin')) - 1
    ymin = int(bndbox.findtext('ymin')) - 1
    xmax = int(bndbox.findtext('xmax'))
    ymax = int(bndbox.findtext('ymax'))
    assert xmax > xmin and ymax > ymin, f"Box size error !: (xmin, ymin, xmax, ymax): {xmin, ymin, xmax, ymax}"
    o_width = xmax - xmin
    o_height = ymax - ymin
    ann = {
        'area': o_width * o_height,
        'iscrowd': 0,
        'bbox': [xmin, ymin, o_width, o_height],
        'category_id': category_id,
        'ignore': 0,
        'segmentation': []  # This script is not for segmentation
    }
    return ann
def convert_xmls_to_cocojson(annotation_paths: List[str],
                             label2id: Dict[str, int],
                             output_jsonpath: str,
                             extract_num_from_imgid: bool = True):
    output_json_dict = {
        "images": [],
        "type": "instances",
        "annotations": [],
        "categories": []
    }
    bnd_id = 1  # START_BOUNDING_BOX_ID, TODO input as args ?
    for a_path in annotation_paths:
        # Read annotation xml
        ann_tree = ET.parse(a_path)
        ann_root = ann_tree.getroot()
        img_info = get_image_info(annotation_root=ann_root,
                                  extract_num_from_imgid=extract_num_from_imgid)
        img_id = img_info['id']
        output_json_dict['images'].append(img_info)
        for obj in ann_root.findall('object'):
            ann = get_coco_annotation_from_obj(obj=obj, label2id=label2id)
            ann.update({'image_id': img_id, 'id': bnd_id})
            output_json_dict['annotations'].append(ann)
            bnd_id = bnd_id + 1
    for label, label_id in label2id.items():
        category_info = {'supercategory': 'none', 'id': label_id, 'name': label}
        output_json_dict['categories'].append(category_info)
    with open(output_jsonpath, 'w') as f:
        output_json = json.dumps(output_json_dict)
        f.write(output_json)
    print('Convert successfully !')
def main():
    parser = argparse.ArgumentParser(
        description='This script support converting voc format xmls to coco format json')
    parser.add_argument('--ann_dir', type=str, default='./VOCdevkit/Annotations')
    parser.add_argument('--ann_ids', type=str, default='./VOCdevkit/ImageSets/Main/val.txt') # 这里修改 train val test 一共修改三次
    #parser.add_argument('--ann_ids', type=str, default='./VOCdevkit/ImageSets/Main/train.txt')
    #parser.add_argument('--ann_ids', type=str, default='./VOCdevkit/ImageSets/Main/test.txt')
    parser.add_argument('--ann_paths_list', type=str, default=None)
    parser.add_argument('--labels', type=str, default='./VOCdevkit/labels.txt')
    parser.add_argument('--output', type=str, default='./output/annotations/val.json') # 这里修改 train val test 一共修改三次
    #parser.add_argument('--output', type=str, default='./output/annotations/train.json')
    #parser.add_argument('--output', type=str, default='./output/annotations/test.json')
    parser.add_argument('--ext', type=str, default='xml')
    args = parser.parse_args()
    label2id = get_label2id(labels_path=args.labels)
    ann_paths = get_annpaths(
        ann_dir_path=args.ann_dir,
        ann_ids_path=args.ann_ids,
        ext=args.ext,
        annpaths_list_path=args.ann_paths_list
    )
    convert_xmls_to_cocojson(
        annotation_paths=ann_paths,
        label2id=label2id,
        output_jsonpath=args.output,
        extract_num_from_imgid=True
    )
if __name__ == '__main__':
    if not os.path.exists('./output/annotations'):
        os.makedirs('./output/annotations')
    main()


2.3 数据集图像文件copy代码实现(复制图片数据集到coco中)VOC_To_CoCo_02.py


# VOC_To_CoCo_02.py
import os
import shutil
images_file_path = './VOCdevkit/JPEGImages/'
split_data_file_path = './VOCdevkit/ImageSets/Main/'
new_images_file_path = './output/'
if not os.path.exists(new_images_file_path + 'train'):
    os.makedirs(new_images_file_path + 'train')
if not os.path.exists(new_images_file_path + 'val'):
    os.makedirs(new_images_file_path + 'val')
if not os.path.exists(new_images_file_path + 'test'):
    os.makedirs(new_images_file_path + 'test')
dst_train_Image = new_images_file_path + 'train/'
dst_val_Image = new_images_file_path + 'val/'
dst_test_Image = new_images_file_path + 'test/'
total_txt = os.listdir(split_data_file_path)
for i in total_txt:
    name = i[:-4]
    if name == 'train':
        txt_file = open(split_data_file_path + i, 'r')
        for line in txt_file:
            line = line.strip('\n')
            line = line.strip('\r')
            srcImage = images_file_path + line + '.jpg'
            dstImage = dst_train_Image + line + '.jpg'
            shutil.copyfile(srcImage, dstImage)
        txt_file.close()
    elif name == 'val':
        txt_file = open(split_data_file_path + i, 'r')
        for line in txt_file:
            line = line.strip('\n')
            line = line.strip('\r')
            srcImage = images_file_path + line + '.jpg'
            dstImage = dst_val_Image + line + '.jpg'
            shutil.copyfile(srcImage, dstImage)
        txt_file.close()
    elif name == 'test':
        txt_file = open(split_data_file_path + i, 'r')
        for line in txt_file:
            line = line.strip('\n')
            line = line.strip('\r')
            srcImage = images_file_path + line + '.jpg'
            dstImage = dst_test_Image + line + '.jpg'
            shutil.copyfile(srcImage, dstImage)
        txt_file.close()
    else:
        print("Error, Please check the file name of folder")


三、效果展示


7da88fc7b4014973b8f4c0d87c4ad6b9.png


目录
相关文章
sklearn中分类模型评估指标(一):准确率、Top准确率、平衡准确率
accuracy_score函数计算准确率分数,即预测正确的分数(默认)或计数(当normalize=False时)。 在多标签分类中,该函数返回子集准确率(subset accuracy)。 如果样本的整个预测标签集与真实标签集严格匹配,则子集准确率为 1.0; 否则为 0.0。
|
XML JSON 数据可视化
数据集学习笔记(二): 转换不同类型的数据集用于模型训练(XML、VOC、YOLO、COCO、JSON、PNG)
本文详细介绍了不同数据集格式之间的转换方法,包括YOLO、VOC、COCO、JSON、TXT和PNG等格式,以及如何可视化验证数据集。
5601 1
数据集学习笔记(二): 转换不同类型的数据集用于模型训练(XML、VOC、YOLO、COCO、JSON、PNG)
|
12月前
|
人工智能 监控 物联网
2025年优测平台:接口最大并发量测试工具与实践方法
文章聚焦2025年接口最大并发量测试,阐述其是保障系统稳定性与性能的关键,在高并发成主流背景下愈发重要。指出行业面临并发模型复杂等痛点,主流方案朝SaaS化等方向发展。介绍优测平台在接口并发测试的优势、应用场景及成功案例,还给出最佳实践与决策问答。
|
虚拟化 数据安全/隐私保护
VMware Workstation Pro - 最新版
VMware是一款强大的虚拟机软件,可在单台计算机上模拟完整硬件系统,实现多系统运行。2024年5月推出最新版Workstation Pro 17.5.2,个人用户可免费使用。用户可通过官网下载并注册账户,按指引完成安装,适用于开发、测试及部署应用,具备高效灵活的虚拟化技术。
47578 1
|
IDE 开发工具 Python
【Python】已完美解决:SyntaxError: Non-UTF-8 code starting with ‘æ‘ in file E:/Python/3.py on line 4, but no
【Python】已完美解决:SyntaxError: Non-UTF-8 code starting with ‘æ‘ in file E:/Python/3.py on line 4, but no
1269 0
|
存储 缓存 分布式数据库
【赵渝强老师】HBase的体系架构
HBase是一种基于BigTable思想的列式存储NoSQL数据库,适合数据分析与处理。其主从架构包含HBase HMaster、Region Server和ZooKeeper。HMaster负责Region分配及表管理;Region Server执行数据读写操作,并包含WAL预写日志、Block Cache读缓存和MemStore写缓存;ZooKeeper维护集群状态并协调分布式系统工作。通过视频讲解与架构图示,详细解析各组件功能与协作机制。
1106 11
|
测试技术 API 开发者
持续上新!Qwen2.5-Max正式发布
持续上新!Qwen2.5-Max正式发布
持续上新!Qwen2.5-Max正式发布
|
XML 数据格式 Python
将xml标签转换为txt(voc格式转换为yolo方便进行训练)
该文章提供了一个Python脚本,用于将VOC格式的XML标签文件转换为YOLO训练所需的TXT格式,包括修改数据集类别、输入图像与标注文件夹地址、转换过程和结果展示。
将xml标签转换为txt(voc格式转换为yolo方便进行训练)
惊呆了、老铁。CSDN竟然有GitHub的加速功能????
这篇文章介绍了几种加速访问GitHub的方法,包括使用镜像网站、代理网站下载、利用CDN加速以及转入Gitee平台进行加速。作者建议,对于较大的项目推荐使用代理网站或Gitee下载,而对于较小的项目,使用CDN加速即可满足需求。
惊呆了、老铁。CSDN竟然有GitHub的加速功能????

热门文章

最新文章