检测之COCO转VOC

文章目录

  • 1、COCO数据转VOC

检测系列相关文章参考如下链接:

  1. VOC数据的结构介绍及自定义生成,用labelimg自已标注
  2. VOC标准数据的生成及分析,VOC易用labelimg生成,做为基础的检测数据类型
  3. VOC转YOLO,方便YOLO系列模型使用
  4. VOC转COCO,方便用于COCO map评估
  5. YOLO转VOC,方便使用我们的VOC相关分析和处理代码
  6. COCO转VOC,方便使用我们的VOC相关分析和处理代码
    以上数据格式互转,方便我们处理各种收集的开源数据和自己标记的数据的整合。当然也不是特别的全面,但是工作中常用的主流的格式是包含的。
    觉的有价值的小伙伴可以给点个赞。

本次COCO转VOC数据主要是用来做检测所用,只用到instance.json的标注文件
COCO数据集中目前存在的问题有:

  • 有些图片没有标注信息
  • 有些图的标注框是一条线或点,
  • 有些图有标注框,但没有类名称;
  • 还有一些问题是图片是灰度图。
    以下代码实现了COCO转VOC,将这些错误有问题数据分类收集用于查看,同时也不会保存到我们的目标数据集中,其中,灰度图转换成RGB格式也存到目标数据中。
    转换成VOC数据集格式后,就可以参考VOC处理工具来进行操作。

1、COCO数据转VOC

from pycocotools.coco import COCO 
import  os, cv2, shutil
from lxml import etree, objectify
from tqdm import tqdm
from PIL import Image
import numpy as np
import time
import json

def cover_copy(src,dst):
    '''
    src和dst都必须是文件,该函数是执行覆盖操作
    '''
    if os.path.exists(dst):
        os.remove(dst)
        shutil.copy(src,dst)
    else:
        shutil.copy(src,dst)
def coco2voc(basedir='VOCdevkit/COCO_VOC',sourcedir='COCO'):
    """
    basedir:用来存放转换后数据和标注文件
    sourcedir:用来指定原始COCO数据集的存放位置
    """

    img_savepath= os.path.join(basedir,'JPEGImages')
    ann_savepath=os.path.join(basedir,'Annotations')
    main_path = os.path.join(basedir,"ImageSets/Main")
    for p in [basedir,img_savepath,ann_savepath,main_path]:
        if os.path.exists(p):
            shutil.rmtree(p)
            os.makedirs(p)
        else:
            os.makedirs(p)

    
    datasets = ['train2017','val2017']
    # datasets = ['val2017']

    for dataset in datasets:
        start = time.time()
        print(f"start {dataset}")
        no_ann=[] #用来存放没有标注数据的图片的id,并将这些图片复制到results文件夹中
        not_rgb=[] #是灰度图,同样将其保存

        annfile = 'instances_{}.json'.format(dataset)
        annpath=os.path.join(sourcedir,'annotations',annfile)
        
        print('loading annotations into memory...')
        tic = time.time()
        with open(annpath, 'r') as f:
            dataset_ann = json.load(f)
        assert type(
            dataset_ann
        ) == dict, 'annotation file format {} not supported'.format(
            type(dataset))
        print('Done (t={:0.2f}s)'.format(time.time() - tic))
        
        coco = COCO(annpath)
        classes = dict()
        for cat in coco.dataset['categories']:
            classes[cat['id']] = cat['name']
        imgIds = coco.getImgIds()
        # imgIds=imgIds[0:1000]#测试用,抽取10张图片,看下存储效果
        for imgId in tqdm(imgIds):
            img = coco.loadImgs(imgId)[0]
   
            filename = img['file_name']
            filepath=os.path.join(sourcedir,dataset,filename)

            annIds = coco.getAnnIds(imgIds=img['id'],  iscrowd=None)
            anns = coco.loadAnns(annIds)
            
            if not len(anns):
                # print(f"{dataset}:{imgId}该文件没有标注信息,将其复制到{dataset}_noann_result中,以使查看")
                no_ann.append(imgId)
                result_path = os.path.join(sourcedir,dataset+"_noann_result")
                dest_path = os.path.join(result_path,filename)
                if not os.path.exists(result_path):
                    os.makedirs(result_path)
                cover_copy(filepath,dest_path)
                continue #如果没有标注信息,则把没有标注信息的图片移动到相关结果文件 noann_result中,来进行查看 ,然后返回做下一张图
            #有标注信息,接着往下走,获取标注信息
            objs = []
            for ann in anns:
                name = classes[ann['category_id']]
                if 'bbox' in ann:
                    # print('bbox in ann',imgId)
                    bbox = ann['bbox']
                    xmin = (int)(bbox[0])
                    ymin = (int)(bbox[1])
                    xmax = (int)(bbox[2] + bbox[0])
                    ymax = (int)(bbox[3] + bbox[1])
                    obj = [name, 1.0, xmin, ymin, xmax, ymax]
                    #标错框在这里
                    if not(xmin-xmax==0 or ymin-ymax==0):
                        objs.append(obj)
 
                else:
                    print(f"{dataset}:{imgId}bbox在标注文件中不存在")# 单张图有多个标注框,某个类别没有框

                   
            annopath = os.path.join(ann_savepath,filename[:-3] + "xml") #生成的xml文件保存路径
            dst_path = os.path.join(img_savepath,filename)
           
            im = Image.open(filepath)
            image = np.array(im).astype(np.uint8)

            if im.mode != "RGB":
 
            # if img.shape[-1] != 3:
                
                

                # print(f"{dataset}:{imgId}该文件非rgb图,其复制到{dataset}_notrgb_result中,以使查看")
                # print(f"img.shape{image.shape} and img.mode{im.mode}")
                not_rgb.append(imgId)
                result_path = os.path.join(sourcedir,dataset+"_notrgb_result")
                dest_path = os.path.join(result_path,filename)
                if not os.path.exists(result_path):
                    os.makedirs(result_path)
                cover_copy(filepath,dest_path) #复制到notrgb_result来方便查看
                
                im=im.convert('RGB')
                image = np.array(im).astype(np.uint8)
                im.save(dst_path,quality=95)#图片经过转换后,放到我们需要的位置片
                im.close()

            else:
                
                cover_copy(filepath, dst_path)#把原始图像复制到目标文件夹
            E = objectify.ElementMaker(annotate=False)
            anno_tree = E.annotation(
                E.folder('VOC'),
                E.filename(filename),
                E.source(
                    E.database('COCO'),
                    E.annotation('VOC'),
                    E.image('COCO')
                ),
                E.size(
                    E.width(image.shape[1]),
                    E.height(image.shape[0]),
                    E.depth(image.shape[2])
                ),
                E.segmented(0)
            )

            for obj in objs:
                E2 = objectify.ElementMaker(annotate=False)
                anno_tree2 = E2.object(
                    E.name(obj[0]),
                    E.pose(),
                    E.truncated("0"),
                    E.difficult(0),
                    E.bndbox(
                        E.xmin(obj[2]),
                        E.ymin(obj[3]),
                        E.xmax(obj[4]),
                        E.ymax(obj[5])
                    )
                )
                anno_tree.append(anno_tree2)
            etree.ElementTree(anno_tree).write(annopath, pretty_print=True)
        print(f"{dataset}该数据集有{len(no_ann)}/{len(imgIds)}张图片没有instance标注信息,已经这些图片复制到{dataset}_noann_result中以使进行查看")
        print(f"{dataset}该数据集有{len(not_rgb)}/{len(imgIds)}张图片是非RGB图像,已经这些图片复制到{dataset}_notrgb_result中以使进行查看")
        duriation = time.time()-start
        print(f"数据集{dataset}处理完成用时{round(duriation/60,2)}分")
coco2voc()
start train2017
loading annotations into memory...
Done (t=33.31s)
loading annotations into memory...
Done (t=18.20s)
creating index...


  0%|          | 15/118287 [00:00<13:26, 146.57it/s]

index created!


100%|██████████| 118287/118287 [36:04<00:00, 54.64it/s] 


train2017该数据集有1021/118287张图片没有instance标注信息,已经这些图片复制到train2017_noann_result中以使进行查看
train2017该数据集有226/118287张图片是非RGB图像,已经这些图片复制到train2017_notrgb_result中以使进行查看
数据集train2017处理完成用时36.96分
start val2017
loading annotations into memory...
Done (t=13.58s)
loading annotations into memory...
Done (t=0.49s)
creating index...
index created!


100%|██████████| 5000/5000 [01:23<00:00, 60.12it/s]

val2017该数据集有48/5000张图片没有instance标注信息,已经这些图片复制到val2017_noann_result中以使进行查看
val2017该数据集有10/5000张图片是非RGB图像,已经这些图片复制到val2017_notrgb_result中以使进行查看
数据集val2017处理完成用时1.68分

from pycocotools.coco import COCO
这个COCO类的内容是:

#关于COCO这个类如下,我们也可以使用以下代码而不使用pycocotools
__author__ = 'tylin'
__version__ = '2.0'
# Interface for accessing the Microsoft COCO dataset.

# Microsoft COCO is a large image dataset designed for object detection,
# segmentation, and caption generation. pycocotools is a Python API that
# assists in loading, parsing and visualizing the annotations in COCO.
# Please visit http://mscoco.org/ for more information on COCO, including
# for the data, paper, and tutorials. The exact format of the annotations
# is also described on the COCO website. For example usage of the pycocotools
# please see pycocotools_demo.ipynb. In addition to this API, please download
# both the COCO images and annotations in order to run the demo.

# An alternative to using the API is to load the annotations directly
# into Python dictionary
# Using the API provides additional utility functions. Note that this API
# supports both *instance* and *caption* annotations. In the case of
# captions not all functions are defined (e.g. categories are undefined).

# The following API functions are defined:
#  COCO       - COCO api class that loads COCO annotation file and prepare data
#               structures.
#  decodeMask - Decode binary mask M encoded via run-length encoding.
#  encodeMask - Encode binary mask M using run-length encoding.
#  getAnnIds  - Get ann ids that satisfy given filter conditions.
#  getCatIds  - Get cat ids that satisfy given filter conditions.
#  getImgIds  - Get img ids that satisfy given filter conditions.
#  loadAnns   - Load anns with the specified ids.
#  loadCats   - Load cats with the specified ids.
#  loadImgs   - Load imgs with the specified ids.
#  annToMask  - Convert segmentation in an annotation to binary mask.
#  showAnns   - Display the specified annotations.
#  loadRes    - Load algorithm results and create API for accessing them.
#  download   - Download COCO images from mscoco.org server.
# Throughout the API "ann"=annotation, "cat"=category, and "img"=image.
# Help on each functions can be accessed by: "help COCO>function".

# See also COCO>decodeMask,
# COCO>encodeMask, COCO>getAnnIds, COCO>getCatIds,
# COCO>getImgIds, COCO>loadAnns, COCO>loadCats,
# COCO>loadImgs, COCO>annToMask, COCO>showAnns

# Microsoft COCO Toolbox.      version 2.0
# Data, paper, and tutorials available at:  http://mscoco.org/
# Code written by Piotr Dollar and Tsung-Yi Lin, 2014.
# Licensed under the Simplified BSD License [see bsd.txt]

import copy
import itertools
import json
import os
import time
from collections import defaultdict
from urllib.request import urlretrieve

import matplotlib.pyplot as plt
import numpy as np
from matplotlib.collections import PatchCollection
from matplotlib.patches import Polygon

from . import mask as maskUtils


def _isArrayLike(obj):
    return hasattr(obj, '__iter__') and hasattr(obj, '__len__')


class COCO:
    def __init__(self, annotation_file=None):
        """
        Constructor of Microsoft COCO helper class for reading and visualizing
        annotations.
        :param annotation_file (str): location of annotation file
        :param image_folder (str): location to the folder that hosts images.
        :return:
        """
        # load dataset
        self.dataset, self.anns, self.cats, self.imgs = dict(), dict(), dict(
        ), dict()
        self.imgToAnns, self.catToImgs = defaultdict(list), defaultdict(list)
        if annotation_file is not None:
            print('loading annotations into memory...')
            tic = time.time()
            with open(annotation_file, 'r') as f:
                dataset = json.load(f)
            assert type(
                dataset
            ) == dict, 'annotation file format {} not supported'.format(
                type(dataset))
            print('Done (t={:0.2f}s)'.format(time.time() - tic))
            self.dataset = dataset
            self.createIndex()
        self.img_ann_map = self.imgToAnns
        self.cat_img_map = self.catToImgs

    def createIndex(self):
        # create index
        print('creating index...')
        anns, cats, imgs = {}, {}, {}
        imgToAnns, catToImgs = defaultdict(list), defaultdict(list)
        if 'annotations' in self.dataset:
            for ann in self.dataset['annotations']:
                imgToAnns[ann['image_id']].append(ann)
                anns[ann['id']] = ann

        if 'images' in self.dataset:
            for img in self.dataset['images']:
                imgs[img['id']] = img

        if 'categories' in self.dataset:
            for cat in self.dataset['categories']:
                cats[cat['id']] = cat

        if 'annotations' in self.dataset and 'categories' in self.dataset:
            for ann in self.dataset['annotations']:
                catToImgs[ann['category_id']].append(ann['image_id'])

        print('index created!')

        # create class members
        self.anns = anns
        self.imgToAnns = imgToAnns
        self.catToImgs = catToImgs
        self.imgs = imgs
        self.cats = cats

    def info(self):
        """
        Print information about the annotation file.
        :return:
        """
        for key, value in self.dataset['info'].items():
            print('{}: {}'.format(key, value))

    def getAnnIds(self, imgIds=[], catIds=[], areaRng=[], iscrowd=None):
        """
        Get ann ids that satisfy given filter conditions. default skips that
        filter
        :param imgIds  (int array)     : get anns for given imgs
               catIds  (int array)     : get anns for given cats
               areaRng (float array)   : get anns for given area range
                                        (e.g. [0 inf])
               iscrowd (boolean)       : get anns for given crowd label
                                        (False or True)
        :return: ids (int array)       : integer array of ann ids
        """
        imgIds = imgIds if _isArrayLike(imgIds) else [imgIds]
        catIds = catIds if _isArrayLike(catIds) else [catIds]

        if len(imgIds) == len(catIds) == len(areaRng) == 0:
            anns = self.dataset['annotations']
        else:
            if not len(imgIds) == 0:
                lists = [
                    self.imgToAnns[imgId] for imgId in imgIds
                    if imgId in self.imgToAnns
                ]
                anns = list(itertools.chain.from_iterable(lists))
            else:
                anns = self.dataset['annotations']
            anns = anns if len(catIds) == 0 else [
                ann for ann in anns if ann['category_id'] in catIds
            ]
            anns = anns if len(areaRng) == 0 else [
                ann for ann in anns
                if ann['area'] > areaRng[0] and ann['area'] < areaRng[1]
            ]
        if iscrowd is not None:
            ids = [ann['id'] for ann in anns if ann['iscrowd'] == iscrowd]
        else:
            ids = [ann['id'] for ann in anns]
        return ids

    def get_ann_ids(self, img_ids=[], cat_ids=[], area_rng=[], iscrowd=None):
        return self.getAnnIds(img_ids, cat_ids, area_rng, iscrowd)

    def getCatIds(self, catNms=[], supNms=[], catIds=[]):
        """
        filtering parameters. default skips that filter.
        :param catNms (str array)  : get cats for given cat names
        :param supNms (str array)  : get cats for given supercategory names
        :param catIds (int array)  : get cats for given cat ids
        :return: ids (int array)   : integer array of cat ids
        """
        catNms = catNms if _isArrayLike(catNms) else [catNms]
        supNms = supNms if _isArrayLike(supNms) else [supNms]
        catIds = catIds if _isArrayLike(catIds) else [catIds]

        if len(catNms) == len(supNms) == len(catIds) == 0:
            cats = self.dataset['categories']
        else:
            cats = self.dataset['categories']
            cats = cats if len(catNms) == 0 else [
                cat for cat in cats if cat['name'] in catNms
            ]
            cats = cats if len(supNms) == 0 else [
                cat for cat in cats if cat['supercategory'] in supNms
            ]
            cats = cats if len(catIds) == 0 else [
                cat for cat in cats if cat['id'] in catIds
            ]
        ids = [cat['id'] for cat in cats]
        return ids

    def get_cat_ids(self, cat_names=[], sup_names=[], cat_ids=[]):
        return self.getCatIds(cat_names, sup_names, cat_ids)

    def getImgIds(self, imgIds=[], catIds=[]):
        '''
        Get img ids that satisfy given filter conditions.
        :param imgIds (int array) : get imgs for given ids
        :param catIds (int array) : get imgs with all given cats
        :return: ids (int array)  : integer array of img ids
        '''
        imgIds = imgIds if _isArrayLike(imgIds) else [imgIds]
        catIds = catIds if _isArrayLike(catIds) else [catIds]

        if len(imgIds) == len(catIds) == 0:
            ids = self.imgs.keys()
        else:
            ids = set(imgIds)
            for i, catId in enumerate(catIds):
                if i == 0 and len(ids) == 0:
                    ids = set(self.catToImgs[catId])
                else:
                    ids &= set(self.catToImgs[catId])
        return list(ids)

    def get_img_ids(self, img_ids=[], cat_ids=[]):
        return self.getImgIds(img_ids, cat_ids)

    def loadAnns(self, ids=[]):
        """
        Load anns with the specified ids.
        :param ids (int array)       : integer ids specifying anns
        :return: anns (object array) : loaded ann objects
        """
        if _isArrayLike(ids):
            return [self.anns[id] for id in ids]
        elif type(ids) == int:
            return [self.anns[ids]]

    load_anns = loadAnns

    def loadCats(self, ids=[]):
        """
        Load cats with the specified ids.
        :param ids (int array)       : integer ids specifying cats
        :return: cats (object array) : loaded cat objects
        """
        if _isArrayLike(ids):
            return [self.cats[id] for id in ids]
        elif type(ids) == int:
            return [self.cats[ids]]

    load_cats = loadCats

    def loadImgs(self, ids=[]):
        """
        Load anns with the specified ids.
        :param ids (int array)       : integer ids specifying img
        :return: imgs (object array) : loaded img objects
        """
        if _isArrayLike(ids):
            return [self.imgs[id] for id in ids]
        elif type(ids) == int:
            return [self.imgs[ids]]

    load_imgs = loadImgs

    def showAnns(self, anns, draw_bbox=False):
        """
        Display the specified annotations.
        :param anns (array of object): annotations to display
        :return: None
        """
        if len(anns) == 0:
            return 0
        if 'segmentation' in anns[0] or 'keypoints' in anns[0]:
            datasetType = 'instances'
        elif 'caption' in anns[0]:
            datasetType = 'captions'
        else:
            raise Exception('datasetType not supported')
        if datasetType == 'instances':
            ax = plt.gca()
            ax.set_autoscale_on(False)
            polygons = []
            color = []
            for ann in anns:
                c = (np.random.random((1, 3)) * 0.6 + 0.4).tolist()[0]
                if 'segmentation' in ann:
                    if type(ann['segmentation']) == list:
                        # polygon
                        for seg in ann['segmentation']:
                            poly = np.array(seg).reshape(
                                (int(len(seg) / 2), 2))
                            polygons.append(Polygon(poly))
                            color.append(c)
                    else:
                        # mask
                        t = self.imgs[ann['image_id']]
                        if type(ann['segmentation']['counts']) == list:
                            rle = maskUtils.frPyObjects([ann['segmentation']],
                                                        t['height'],
                                                        t['width'])
                        else:
                            rle = [ann['segmentation']]
                        m = maskUtils.decode(rle)
                        img = np.ones((m.shape[0], m.shape[1], 3))
                        if ann['iscrowd'] == 1:
                            color_mask = np.array([2.0, 166.0, 101.0]) / 255
                        if ann['iscrowd'] == 0:
                            color_mask = np.random.random((1, 3)).tolist()[0]
                        for i in range(3):
                            img[:, :, i] = color_mask[i]
                        ax.imshow(np.dstack((img, m * 0.5)))
                if 'keypoints' in ann and type(ann['keypoints']) == list:
                    # turn skeleton into zero-based index
                    sks = np.array(
                        self.loadCats(ann['category_id'])[0]['skeleton']) - 1
                    kp = np.array(ann['keypoints'])
                    x = kp[0::3]
                    
                    y = kp[1::3]
                    v = kp[2::3]
                    for sk in sks:
                        if np.all(v[sk] > 0):
                            plt.plot(x[sk], y[sk], linewidth=3, color=c)
                    plt.plot(x[v > 0],
                             y[v > 0],
                             'o',
                             markersize=8,
                             markerfacecolor=c,
                             markeredgecolor='k',
                             markeredgewidth=2)
                    plt.plot(x[v > 1],
                             y[v > 1],
                             'o',
                             markersize=8,
                             markerfacecolor=c,
                             markeredgecolor=c,
                             markeredgewidth=2)

                if draw_bbox:
                    [bbox_x, bbox_y, bbox_w, bbox_h] = ann['bbox']
                    poly = [[bbox_x, bbox_y], [bbox_x, bbox_y + bbox_h],
                            [bbox_x + bbox_w, bbox_y + bbox_h],
                            [bbox_x + bbox_w, bbox_y]]
                    np_poly = np.array(poly).reshape((4, 2))
                    polygons.append(Polygon(np_poly))
                    color.append(c)

            p = PatchCollection(polygons,
                                facecolor=color,
                                linewidths=0,
                                alpha=0.4)
            ax.add_collection(p)
            p = PatchCollection(polygons,
                                facecolor='none',
                                edgecolors=color,
                                linewidths=2)
            ax.add_collection(p)
        elif datasetType == 'captions':
            for ann in anns:
                print(ann['caption'])

    def loadRes(self, resFile):
        """
        Load result file and return a result api object.
        :param   resFile (str)     : file name of result file
        :return: res (obj)         : result api object
        """
        res = COCO()
        res.dataset['images'] = [img for img in self.dataset['images']]

        print('Loading and preparing results...')
        tic = time.time()
        if type(resFile) == str:
            with open(resFile) as f:
                anns = json.load(f)
        elif type(resFile) == np.ndarray:
            anns = self.loadNumpyAnnotations(resFile)
        else:
            anns = resFile
        assert type(anns) == list, 'results in not an array of objects'
        annsImgIds = [ann['image_id'] for ann in anns]
        assert set(annsImgIds) == (set(annsImgIds) & set(self.getImgIds())), \
               'Results do not correspond to current coco set'
        if 'caption' in anns[0]:
            imgIds = set([img['id'] for img in res.dataset['images']]) & set(
                [ann['image_id'] for ann in anns])
            res.dataset['images'] = [
                img for img in res.dataset['images'] if img['id'] in imgIds
            ]
            for id, ann in enumerate(anns):
                ann['id'] = id + 1
        elif 'bbox' in anns[0] and not anns[0]['bbox'] == []:
            res.dataset['categories'] = copy.deepcopy(
                self.dataset['categories'])
            for id, ann in enumerate(anns):
                bb = ann['bbox']
                x1, x2, y1, y2 = [bb[0], bb[0] + bb[2], bb[1], bb[1] + bb[3]]
                if 'segmentation' not in ann:
                    ann['segmentation'] = [[x1, y1, x1, y2, x2, y2, x2, y1]]
                ann['area'] = bb[2] * bb[3]
                ann['id'] = id + 1
                ann['iscrowd'] = 0
        elif 'segmentation' in anns[0]:
            res.dataset['categories'] = copy.deepcopy(
                self.dataset['categories'])
            for id, ann in enumerate(anns):
                # now only support compressed RLE format as segmentation
                # results
                ann['area'] = maskUtils.area(ann['segmentation'])
                if 'bbox' not in ann:
                    ann['bbox'] = maskUtils.toBbox(ann['segmentation'])
                ann['id'] = id + 1
                ann['iscrowd'] = 0
        elif 'keypoints' in anns[0]:
            res.dataset['categories'] = copy.deepcopy(
                self.dataset['categories'])
            for id, ann in enumerate(anns):
                s = ann['keypoints']
                x = s[0::3]
                y = s[1::3]
                x0, x1, y0, y1 = np.min(x), np.max(x), np.min(y), np.max(y)
                ann['area'] = (x1 - x0) * (y1 - y0)
                ann['id'] = id + 1
                ann['bbox'] = [x0, y0, x1 - x0, y1 - y0]
        print('DONE (t={:0.2f}s)'.format(time.time() - tic))

        res.dataset['annotations'] = anns
        res.createIndex()
        return res

    def download(self, tarDir=None, imgIds=[]):
        '''
        Download COCO images from mscoco.org server.
        :param tarDir (str): COCO results directory name
               imgIds (list): images to be downloaded
        :return:
        '''
        if tarDir is None:
            print('Please specify target directory')
            return -1
        if len(imgIds) == 0:
            imgs = self.imgs.values()
        else:
            imgs = self.loadImgs(imgIds)
        N = len(imgs)
        if not os.path.exists(tarDir):
            os.makedirs(tarDir)
        for i, img in enumerate(imgs):
            tic = time.time()
            fname = os.path.join(tarDir, img['file_name'])
            if not os.path.exists(fname):
                urlretrieve(img['coco_url'], fname)
            print('downloaded {}/{} images (t={:0.1f}s)'.format(
                i, N,
                time.time() - tic))

    def loadNumpyAnnotations(self, data):
        """
        Convert result data from a numpy array [Nx7] where each row contains
        {imageID,x1,y1,w,h,score,class}
        :param  data (numpy.ndarray)
        :return: annotations (python nested list)
        """
        print('Converting ndarray to lists...')
        assert (type(data) == np.ndarray)
        print(data.shape)
        assert (data.shape[1] == 7)
        N = data.shape[0]
        ann = []
        for i in range(N):
            if i % 1000000 == 0:
                print('{}/{}'.format(i, N))
            ann += [{
                'image_id': int(data[i, 0]),
                'bbox': [data[i, 1], data[i, 2], data[i, 3], data[i, 4]],
                'score': data[i, 5],
                'category_id': int(data[i, 6]),
            }]
        return ann

    def annToRLE(self, ann):
        """
        Convert annotation which can be polygons, uncompressed RLE to RLE.
        :return: binary mask (numpy 2D array)
        """
        t = self.imgs[ann['image_id']]
        h, w = t['height'], t['width']
        segm = ann['segmentation']
        if type(segm) == list:
            # polygon -- a single object might consist of multiple parts
            # we merge all parts into one mask rle code
            rles = maskUtils.frPyObjects(segm, h, w)
            rle = maskUtils.merge(rles)
        elif type(segm['counts']) == list:
            # uncompressed RLE
            rle = maskUtils.frPyObjects(segm, h, w)
        else:
            # rle
            rle = ann['segmentation']
        return rle

    ann_to_rle = annToRLE

    def annToMask(self, ann):
        """
        Convert annotation which can be polygons, uncompressed RLE, or RLE to
        binary mask.
        :return: binary mask (numpy 2D array)
        """
        rle = self.annToRLE(ann)
        m = maskUtils.decode(rle)
        return m

    ann_to_mask = annToMask

事实上有些COCO中有些图是的标注是一个点或一条线,可视化看一下:

import cv2
import matplotlib.pyplot as plt
import os 
import numpy as np
import xml.etree.ElementTree as ET
#只画有问题的框
def draw_single_image(ann_path,img_path,save_path=None):
    """
    ann_path:指定xml的绝对路径
    img_path:指定xml的绝对路径
    save_path:如果不是None,那么将是结果图的保存路径;反之则画出来
    """
    img = cv2.imdecode(np.fromfile(img_path,dtype=np.uint8),-1)
    if img is None or not img.any():
            raise '有空图'
    tree = ET.parse(ann_path)
    root = tree.getroot()
    result = root.findall("object")
    for obj in result:
        name = obj.find("name").text
        x1=int(obj.find('bndbox').find('xmin').text)
        y1=int(obj.find('bndbox').find('ymin').text)
        x2=int(obj.find('bndbox').find('xmax').text)
        y2=int(obj.find('bndbox').find('ymax').text)
        #这部分要吧注释掉,从而把所有框都画出来
        if x1==x2 or y1==y2:
            print('x,y',x1,x2,y1,y2)
            cv2.rectangle(img,(x1,y1),(x2,y2),(0,0,255),2)
            cv2.putText(img,name,(max(x1,15),max(y1,15)),cv2.FONT_ITALIC,1,(0,255,0,2))
    if save_path is None:
        plt.figure(figsize=(25,10))
        # imgrgb = cv2.cvtColor(img,cv2.COLOR_BGR2BGR)
        imgrgb=img[...,::-1]
        plt.imshow(imgrgb)
    else:
        cv2.imencode('.jpg',img)[1].tofile(save_path)
 #画出所有框
def draw_single_image_all(ann_path,img_path,save_path=None):
    """
    ann_path:指定xml的绝对路径
    img_path:指定xml的绝对路径
    save_path:如果不是None,那么将是结果图的保存路径;反之则画出来
    """
    img = cv2.imdecode(np.fromfile(img_path,dtype=np.uint8),-1)
    if img is None or not img.any():
            raise '有空图'
    tree = ET.parse(ann_path)
    root = tree.getroot()
    result = root.findall("object")
    for obj in result:
        name = obj.find("name").text
        x1=int(obj.find('bndbox').find('xmin').text)
        y1=int(obj.find('bndbox').find('ymin').text)
        x2=int(obj.find('bndbox').find('xmax').text)
        y2=int(obj.find('bndbox').find('ymax').text)

        cv2.rectangle(img,(x1,y1),(x2,y2),(0,0,255),2)
        cv2.putText(img,name,(max(x1,15),max(y1,15)),cv2.FONT_ITALIC,1,(0,255,0,2))
    if save_path is None:
        plt.figure(figsize=(25,10))
        # imgrgb = cv2.cvtColor(img,cv2.COLOR_BGR2BGR)
        imgrgb=img[...,::-1]
        plt.imshow(imgrgb)
    else:
        cv2.imencode('.jpg',img)[1].tofile(save_path)
#draw_single_image('VOCdevkit/COCO_VOC/Annotations/000000200365.xml','VOCdevkit/COCO_VOC/JPEGImages/000000200365.jpg')
#draw_single_image_all('VOCdevkit/COCO_VOC/Annotations/000000200365.xml','VOCdevkit/COCO_VOC/JPEGImages/000000200365.jpg')
draw_single_image('VOCdevkit/COCO_VOC/Annotations/000000060054.xml','VOCdevkit/COCO_VOC/JPEGImages/000000060054.jpg')
draw_single_image_all('VOCdevkit/COCO_VOC/Annotations/000000060054.xml','VOCdevkit/COCO_VOC/JPEGImages/000000060054.jpg')
x,y 402 407 369 369

检测之COCO转VOC_第1张图片

检测之COCO转VOC_第2张图片
上述代码以经把各种问题数据经过处理。

后续处理方式可以参考VOC数据的处理方式来进行,如提取某几分类数据等。

你可能感兴趣的:(数据标注及转换,COCO转VOC,深度学习,图像识别,目标检测)