VOC数据集格式转化成COCO数据集格式

本文详细介绍了如何将VOC数据集格式转换为COCO数据集格式,主要涉及标注文件的转化,包括理解VOC的.xml标注文件和COCO的json文件结构。作者提供了两个关键函数,分别用于通过txt文件和直接处理Annotations文件夹来生成COCO格式的json文件,便于深度学习模型如CenterNet的训练。
摘要由CSDN通过智能技术生成

                  VOC数据集格式转化成COCO数据集格式
一、唠叨
     之前写过一篇关于coco数据集转化成VOC格式的博客COCO2VOC,最近读到CenterNet的官方代码,实现上则是将voc转化成coco数据格式,这样的操作我个人感觉很不习惯,也觉得有些奇葩,可能是每个人习惯不一样吧,我们知道有时候我们会采用labelImg标注数据,标注出来的格式就是voc,如果直接训练就可以用来训练是不是更加友好。

     为了不大规模修改原始训练代码(虽然自己修改了一个版本的voc数据集就能直接训练centernet 😊),同时也看到网上很多大佬也做了将VOC数据格式转化成COCO用于其训练,这里我自己做一个精细一点的,作为笔记。

二、转化过程
   数据格式的转换实际是annotation标注文件的转化,voc的数据标注文件为以.xml结尾的文件,而且每张图片均有一个对应的同名标注文件;COCO则是将所有的标注信息写在一个json文件中。VOC数据集目录如下:

    在之前的coco2voc博客中做了详细的介绍,现在直接开始转化,目标就是将Annotations中的所有标注文件中的bbox标注信息转化为json文件,根据训练集和测试集,则主要转化为四个json文件,分别是test.json、train.json、val.json和trainval.json .这里我根据ImageSets中的train.txt val.txt trainval.txt生成后三个json文件,当然也可以直接从Annotations文件夹生成。

VOC2COCO.py

import xml.etree.ElementTree as ET
import os
import json
 
coco = dict()
coco['images'] = []
coco['type'] = 'instances'
coco['annotations'] = []
coco['categories'] = []
 
category_set = dict()
image_set = set()
 
category_item_id = -1
image_id = 20180000000
annotation_id = 0
 
def addCatItem(name):
    global category_item_id
    category_item = dict()
    category_item['supercategory'] = 'none'
    category_item_id += 1
    category_item['id'] = category_item_id
    category_item['name'] = name
    coco['categories'].append(category_item)
    category_set[name] = category_item_id
    return category_item_id
 
def addImgItem(file_name, size):
    global image_id
    if file_name is None:
        raise Exception('Could not find filename tag in xml file.')
    if size['width'] is None:
        raise Exception('Could not find width tag in xml file.')
    if size['height'] is None:
        raise Exception('Could not find height tag in xml file.')
    image_id += 1
    image_item = dict()
    image_item['id'] = image_id
    image_item['file_name'] = file_name
    image_item['width'] = size['width']
    image_item['height'] = size['height']
    coco['images'].append(image_item)
    image_set.add(file_name)
    return image_id
 
def addAnnoItem(object_name, image_id, category_id, bbox):
    global annotation_id
    annotation_item = dict()
    annotation_item['segmentation'] = []
    seg = []
    # bbox[] is x,y,w,h
    # left_top
    seg.append(bbo
  • 8
    点赞
  • 64
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 4
    评论
要将VOC数据集格式换为COCO数据集格式,可以按照以下步骤进行操作: 1. 安装Python COCO API库 COCO API库可以帮助我们处理COCO数据集格式。可以使用以下命令安装COCO API库: ``` pip install pycocotools ``` 2. 编写换脚本 可以编写Python脚本来将VOC数据集格式换为COCO数据集格式。下面是一个简单的脚本示例: ``` import os import json from xml.etree import ElementTree as ET from collections import defaultdict def parse_xml(xml_file): tree = ET.parse(xml_file) root = tree.getroot() size = root.find('size') width = int(size.find('width').text) height = int(size.find('height').text) objects = [] for obj in root.findall('object'): name = obj.find('name').text bbox = obj.find('bndbox') xmin = int(bbox.find('xmin').text) ymin = int(bbox.find('ymin').text) xmax = int(bbox.find('xmax').text) ymax = int(bbox.find('ymax').text) objects.append({'name': name, 'bbox': [xmin, ymin, xmax-xmin, ymax-ymin]}) return {'width': width, 'height': height, 'objects': objects} def convert_voc_to_coco(voc_dir, coco_file): images = [] annotations = [] categories = defaultdict(dict) category_id = 1 annotation_id = 1 for root, _, files in os.walk(voc_dir): for file in files: if file.endswith('.xml'): xml_file = os.path.join(root, file) image_file = os.path.splitext(xml_file)[0] + '.jpg' image_id = len(images) + 1 image = {'id': image_id, 'file_name': image_file} images.append(image) data = parse_xml(xml_file) for obj in data['objects']: category_name = obj['name'] if category_name not in categories: categories[category_name] = {'id': category_id, 'name': category_name} category_id += 1 category = categories[category_name] annotation = {'id': annotation_id, 'image_id': image_id, 'category_id': category['id'], 'bbox': obj['bbox']} annotations.append(annotation) annotation_id += 1 coco_data = {'images': images, 'annotations': annotations, 'categories': list(categories.values())} with open(coco_file, 'w') as f: json.dump(coco_data, f) if __name__ == '__main__': voc_dir = 'path/to/voc' coco_file = 'path/to/coco.json' convert_voc_to_coco(voc_dir, coco_file) ``` 在这个脚本中,我们使用`parse_xml`函数从VOC格式的XML文件中解析出图像和目标位置信息,然后使用`convert_voc_to_coco`函数将整个VOC数据集换为COCO格式的JSON文件。 3. 运行脚本 将VOC格式数据集放置在指定的目录下,然后运行上面的Python脚本即可将VOC数据集格式换为COCO数据集格式。 ``` python voc_to_coco.py ``` 换后的COCO格式的JSON文件将保存在指定的位置。
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

青年夏日科技

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值