coco数据集转voc格式(附pycocotools下载方法)

1、coco数据集高速下载
我下载的是train2017、val2017和annotations_trainval2017,即coco2017的训练集(118287张图片)、测试集(5000张图片)和他们的标签。
感谢这位大佬的贡献,提供了反代下载方式,直接将下载地址http://us5.funcs.xyz/zips/train2017.zip,http://us5.funcs.xyz/zips/val2017.zip,http://us5.funcs.xyz/annotations/annotations_trainval2017.zip 三个链接复制粘贴到迅雷下载里,可以飞速下载,我在没有任何会员加速的情况下达到了8M/s的速度。

2、coco数据集格式转voc格式
1)下载scikit-image、pycocotools工具、opencv、tqdm

为了防止破坏我自己的系统环境,我新建了一个conda环境:

conda create -n coco python=3.6.4

然后激活环境:

source activate coco

陆续下载所需的库,根据个人网速、服务器配置不同下载速度有所区别,我的服务器有时候快,有时候慢得一匹,也不知道啥原因

conda install scikit-image

conda install tqdm

conda install opencv=3.4.4 # 根据个人需要安装响应版本,我这里因为之前一直用的3.4.4,所以为了防止版本更换带来的bug我还是安装的这个

# pycocotools的下载依据官网有四种方式
# 网上有建议用pip下载,虽然可以下载成功,但是下载到了系统环境里,建议还是用conda
conda install -c conda-forge pycocotools
conda install -c conda-forge/label/gcc7 pycocotools
conda install -c conda-forge/label/cf201901 pycocotools
conda install -c conda-forge/label/cf202003 pycocotools

# 上面四种方式我试了第一种和第四种,第一种安装成功,第四种安装成功后使用出bug,并且都贼慢,等到砸电脑,最后我想到conda search一下,发现可以直接搜索到这个库,直接
conda install pycocotools

#速度快很多,而且后续使用没出错,难道是大道至简?

2)开始转换
首先需要把解压出来的train2017、val2017、annotations三个文件夹放到一个文件夹coco里,然后新建一个目标文件夹coco2017用于存放转换后的xml文件,参考网上的代码并亲测下面代码可用,只需要改一下savepath为coco2017文件夹的绝对路径,datadir为coco文件夹的绝对路径即可:

# -*- coding: utf-8 -*-
from pycocotools.coco import COCO
import os
import shutil
from tqdm import tqdm
import skimage.io as io
import matplotlib.pyplot as plt
import cv2
from PIL import Image, ImageDraw

savepath = "../coco2017/"
datasets_list = ['val2017']  ##运行完之后再改为train2017再运行一次
img_dir = savepath + 'images/'  #####这个路径会把你处理的图片拷贝进来,这里我们只处理了val2017文件夹下的数据,所以处理好之后需要修改生成image文件夹的名称为val2017
anno_dir = savepath + 'annotations/'  # 当前目录下会生成annotations文件夹存放xml,结束后修改名称
classes_names = ['person', 'bicycle', 'car', 'motorcycle', 'airplane', 'bus', 'train', 'truck', 'boat', 'traffic light',
                 'fire hydrant', 'stop sign', 'parking meter', 'bench', 'bird', 'cat', 'dog', 'horse', 'sheep', 'cow',
                 'elephant', 'bear', 'zebra', 'giraffe', 'backpack', 'umbrella', 'handbag', 'tie', 'suitcase',
                 'frisbee', 'skis', 'snowboard', 'sports ball', 'kite', 'baseball bat', 'baseball glove', 'skateboard',
                 'surfboard', 'tennis racket', 'bottle', 'wine glass', 'cup', 'fork', 'knife', 'spoon', 'bowl',
                 'banana', 'apple', 'sandwich', 'orange', 'broccoli', 'carrot', 'hot dog', 'pizza', 'donut', 'cake',
                 'chair', 'couch', 'potted plant', 'bed', 'dining table', 'toilet', 'tv', 'laptop', 'mouse', 'remote',
                 'keyboard', 'cell phone', 'microwave', 'oven', 'toaster', 'sink', 'refrigerator', 'book', 'clock',
                 'vase', 'scissors', 'teddy bear', 'hair drier', 'toothbrush']
dataDir = '../coco'  ####### 连接到coco的数据集
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 mkr(path):
    if os.path.exists(path):
        shutil.rmtree(path)
        os.mkdir(path)
    else:
        os.mkdir(path)


mkr(img_dir)
mkr(anno_dir)


def id2name(coco):
    classes = dict()
    for cls in coco.dataset['categories']:
        classes[cls['id']] = cls['name']
    return classes


def write_xml(anno_path, head, objs, tail):
    f = open(anno_path, "w")
    f.write(head)
    for obj in objs:
        f.write(objstr % (obj[0], obj[1], obj[2], obj[3], obj[4]))
    f.write(tail)


def save_annotations_and_imgs(coco, dataset, filename, objs):
    anno_path = anno_dir + filename[:-3] + 'xml'
    print('anno_path:%s' % anno_path)
    # img_path=dataDir+'/'+'images'+'/'+dataset+'/'+filename
    img_path = dataDir + '/' + dataset + '/' + filename
    print('img_path:%s' % img_path)
    print('step3-image-path-OK')
    dst_imgpath = img_dir + filename

    img = cv2.imread(img_path)
    '''if (img.shape[2] == 1):
        print(filename + " not a RGB image")     
        return'''
    print('img_path:%s' % img_path)
    print('dst_imgpath:%s' % dst_imgpath)
    shutil.copy(img_path, dst_imgpath)

    head = headstr % (filename, img.shape[1], img.shape[0], img.shape[2])
    tail = tailstr
    write_xml(anno_path, head, objs, tail)


def showimg(coco, dataset, img, classes, cls_id, show=True):
    global dataDir
    # I=Image.open('%s/%s/%s/%s'%(dataDir,'images',dataset,img['file_name']))
    I = Image.open('%s/%s/%s' % (dataDir, dataset, img['file_name']))  ########may be you can changed
    annIds = coco.getAnnIds(imgIds=img['id'], catIds=cls_id, iscrowd=None)
    anns = coco.loadAnns(annIds)
    objs = []
    for ann in anns:
        class_name = classes[ann['category_id']]
        if class_name in classes_names:
            print(class_name)
            if 'bbox' in ann:
                bbox = ann['bbox']
                xmin = int(bbox[0])
                ymin = int(bbox[1])
                xmax = int(bbox[2] + bbox[0])
                ymax = int(bbox[3] + bbox[1])
                obj = [class_name, xmin, ymin, xmax, ymax]
                objs.append(obj)
                # draw = ImageDraw.Draw(I)
                # draw.rectangle([xmin, ymin, xmax, ymax])
    # if show:
    # plt.figure()
    # plt.axis('off')
    # plt.imshow(I)
    # plt.show()
    return objs


for dataset in datasets_list:
    annFile = '{}/annotations/instances_{}.json'.format(dataDir, dataset)  # 你放json文件的路径
    print('annFile:%s' % annFile)
    coco = COCO(annFile)
    '''
    loading annotations into memory...
    Done (t=0.81s)
    creating index...
    index created!
    '''
    classes = id2name(coco)
    print("classes:%s" % classes)
    classes_ids = coco.getCatIds(catNms=classes_names)
    print(classes_ids)
    for cls in classes_names:
        cls_id = coco.getCatIds(catNms=[cls])
        img_ids = coco.getImgIds(catIds=cls_id)
        print(cls, len(img_ids))
        # imgIds=img_ids[0:10]
        for imgId in tqdm(img_ids):
            img = coco.loadImgs(imgId)[0]
            filename = img['file_name']
            # print(filename)
            objs = showimg(coco, dataset, img, classes, classes_ids, show=False)
            # print(objs)
            save_annotations_and_imgs(coco, dataset, filename, objs)

  • 1
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 7
    评论
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值