coco数据集

一、coco数据集字段
1、info字段:包括一下字段
在这里插入图片描述
2、licenses:里面集合了不同类型的licenses,并在images中按照id号被应用,基本不参与数据解析的过程中。
在这里插入图片描述
3、images:对应每张图片的详细信息,其中的id号是分配的唯一id
在这里插入图片描述
4、categories:其中supercategory是父类,name是子类,id是类别id(按照子类统计)
在这里插入图片描述
5:annotation:
category_id:该注释的类别id
id:当前注释的id
image_id:该注释所在的图片的id号
area:区域面积
bbox:目标的矩形标注框
iscrowd:0或1。0表示标注的单个对象,此时segmentation使用polygon表示,
1表示标注的是一组对象,此时segmentation使用RLE格式
segmentation:若使用polygon标注时,则记录的是多边形的坐标点,连续两个数值表示一个点的坐标位置,因此此时点的数量为偶数
若使用RLE格式(Run Length Encoding(行程长度压缩算法))

二、代码生成

"""
version:v2
time:2020/11/06
author:huangxiaohuang
"""

import json
import os
import cv2
import shutil
import xml.etree.ElementTree as ET

dataset = {}


# 读取xml文件
def readxml(dataset, xml, count):
    tree = ET.parse(xml)
    root = tree.getroot()
    for child in root:
        # 读取图片的宽高
        if child.tag == "size":
            for s_ch in child:
                if s_ch.tag == "width":
                    w = s_ch.text
                if s_ch.tag == 'height':
                    h = s_ch.text
        elif child.tag == "object":
            # 读取bbox并将信息按照格式保存
            for s_ch in child:
                if s_ch.tag == "bndbox":
                    for ss_ch in s_ch:
                        if ss_ch.tag == "xmin":
                            xmin = ss_ch.text
                        elif ss_ch.tag == "ymin":
                            ymin = ss_ch.text
                        elif ss_ch.tag == "xmax":
                            xmax = ss_ch.text
                        elif ss_ch.tag == "ymax":
                            ymax = ss_ch.text
                    #   保存annotations字段
                    dataset.setdefault("annotations", []).append({
                        'image_id': int(count),
                        'bbox': [int(xmin), int(ymin), int(xmax) - int(xmin), int(ymax) - int(ymin)],
                        'category_id': 6,
                        'area': (int(xmax) - int(xmin)) * (int(ymax) - int(ymin)),
                        'iscrowd': 0,
                        'id': int(count),
                        'segmentation': []
                    })
                else:
                    ca_name = s_ch.text
            #   保存images字段
            dataset.setdefault("images", []).append({
                'file_name': str(count) + '.jpg',
                'id': int(count),
                'width': int(w),
                'height': int(h)
            })


# 原始图片路径
im_path = "./images/train2017/"
# 新产生图片路径
trainimg = "./data/"

cmax = 0
dirpath = os.listdir(im_path)
f1 = os.listdir(im_path)
for file in f1:
    cmax = max(cmax, int(file.split(".")[0]))
    print(cmax)
for imgdir in dirpath:
    count = 1

    for file in os.listdir(im_path):

        if file.split(".")[1] == "jpg":
            oldname = os.path.join(im_path, file)
            jpgname = os.path.join(trainimg, str(count + cmax) + ".jpg")
            # 复制图片到新产生图片的路径文件夹
            shutil.copyfile(oldname, jpgname)
            # 读取相应图片的xml文件
            readxml(dataset, os.path.join('./xml', file.split(".")[0] + ".xml"), count + cmax)
            count += 1

    break
print('end')
# 保存categories字段
for i in range(1, 3):
    dataset.setdefault("categories", []).append({
        'id': i - 1,
        'name': 1,
        'supercategory': 'No'
    })
# 产生instances_minival2014.json文件的保存路径
folder = os.path.join('./coco/')
if not os.path.exists(folder):
    os.makedirs(folder)
json_name = os.path.join(folder + 'instances_minival2014.json')
with open(json_name, 'w') as f:
    json.dump(dataset, f)

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值