IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 人工智能 -> 逐一显示landmark dataset所有图片的标注 -> 正文阅读

[人工智能]逐一显示landmark dataset所有图片的标注

本文解释如何显示WIDER数据集及显示相关标注。

如何显示coco数据集的图片及查看标注的质量请参考前面的文章《保存coco dataset注释为单一文件,并逐一显示所有图片的mask》。

根据libfacedetection.train(https://github.com/ShiqiYu/libfacedetection.train)的数据结构,我们看一下其中的标注trainset.json的效果,

$ tree data/widerface
data/widerface
├── eval_tools
├── wider_face_split
├── WIDER_test
├── WIDER_train
├── WIDER_val
└── trainset.json ?

文件trainset.json?比较大,直接打开要好久,我摘录几条看一下格式

{"images": 
[
{"file_name": "./44--Aerobics/44_Aerobics_Aerobics_44_53.jpg", "height": 683, "width": 1024, "id": 0},
{"file_name": "./44--Aerobics/44_Aerobics_Aerobics_44_28.jpg", "height": 736, "width": 1024, "id": 1}, 
{"file_name": "./44--Aerobics/44_Aerobics_Aerobics_44_591.jpg", "height": 936, "width": 1024, "id": 2}, 
{"file_name": "./44--Aerobics/44_Aerobics_Aerobics_44_46.jpg", "height": 520, "width": 1024, "id": 3}, 
{"file_name": "./44--Aerobics/44_Aerobics_Aerobics_44_321.jpg", "height": 1392, "width": 1024, "id": 4}, 
{"file_name": "./44--Aerobics/44_Aerobics_Aerobics_44_946.jpg", "height": 1001, "width": 1024, "id": 5}, 
{"file_name": "./44--Aerobics/44_Aerobics_Aerobics_44_307.jpg", "height": 1139, "width": 1024, "id": 6}, 
{"file_name": "./44--Aerobics/44_Aerobics_Aerobics_44_943.jpg", "height": 1156, "width": 1024, "id": 7},
{"file_name": "./44--Aerobics/44_Aerobics_Aerobics_44_711.jpg", "height": 1656, "width": 1024, "id": 8},
...
{"file_name": "./27--Spa/27_Spa_Spa_27_110.jpg", "height": 307, "width": 1024, "id": 12860}, 
{"file_name": "./27--Spa/27_Spa_Spa_27_879.jpg", "height": 1024, "width": 1024, "id": 12861}, 
{"file_name": "./27--Spa/27_Spa_Spa_27_219.jpg", "height": 682, "width": 1024, "id": 12862}], 
"annotations": 
[
{"segmentation": [[421.1, 133.2, 445.5, 125.5, 432.6, 145.4, 432.3, 159.9, 450.6, 153.4]], "area": 4356.76, "iscrowd": 0, "image_id": 0, "bbox": [411.5, 100.1, 59.6, 73.1], "category_id": 1, "id": 0, "ignore": 0}, 
{"segmentation": [[537.6, 120.5, 546.8, 119.1, 541.2, 125.2, 539.9, 130.7, 547.1, 129.5]], "area": 585.66, "iscrowd": 0, "image_id": 0, "bbox": [534.4, 110.9, 22.7, 25.8], "category_id": 1, "id": 1, "ignore": 0}, 
{"segmentation": [[104.5, 152.8, 116.5, 150.9, 109.9, 158.4, 107.3, 164.1, 117.7, 162.5]], "area": 953.2800000000001, "iscrowd": 0, "image_id": 0, "bbox": [99.3, 139.2, 28.8, 33.1], "category_id": 1, "id": 2, "ignore": 0},
{"segmentation": [[823.0, 102.6, 832.1, 102.6, 826.2, 107.1, 823.6, 112.3, 830.9, 112.2]], "area": 497.96000000000004, "iscrowd": 0, "image_id": 0, "bbox": [819.0, 95.6, 21.1, 23.6], "category_id": 1, "id": 3, "ignore": 0}, 
{"segmentation": [[955.1, 94.6, 971.3, 96.7, 961.0, 106.2, 954.8, 111.8, 966.8, 113.3]], "area": 1760.5900000000001, "iscrowd": 0, "image_id": 0, "bbox": [945.4, 76.2, 37.7, 46.7], "category_id": 1, "id": 4, "ignore": 0}, 
{"segmentation": [[597.0, 121.9, 603.2, 121.7, 600.3, 125.3, 598.1, 127.7, 602.5, 127.5]], "area": 195.35999999999999, "iscrowd": 0, "image_id": 0, "bbox": [593.3, 115.6, 13.2, 14.8], "category_id": 1, "id": 5, "ignore": 0}, 
{"segmentation": [[756.2, 115.3, 762.5, 115.1, 759.5, 118.2, 757.2, 121.5, 762.1, 121.3]], "area": 221.1, "iscrowd": 0, "image_id": 0, "bbox": [752.5, 108.7, 13.4, 16.5], "category_id": 1, "id": 6, "ignore": 0}, 
{"segmentation": [[358.1, 117.4, 372.8, 116.2, 370.0, 123.7, 361.8, 134.2, 373.6, 133.4]], "area": 1874.5200000000002, "iscrowd": 0, "image_id": 0, "bbox": [340.3, 96.7, 38.1, 49.2], "category_id": 1, "id": 7, "ignore": 0}, 
{"segmentation": [[379.5, 129.0, 389.0, 129.0, 384.7, 133.6, 380.9, 138.7, 387.5, 138.7]], "area": 552.16, "iscrowd": 0, "image_id": 0, "bbox": [373.1, 116.6, 20.3, 27.2], "category_id": 1, "id": 8, "ignore": 0}, 
{"segmentation": [[709.0, 126.4, 713.6, 126.1, 711.4, 128.5, 709.7, 130.8, 713.6, 130.6]], "area": 124.23, "iscrowd": 0, "image_id": 0, "bbox": [706.5, 121.5, 10.1, 12.3], "category_id": 1, "id": 9, "ignore": 0},
{"segmentation": [[572.8, 126.0, 581.4, 126.0, 576.7, 131.5, 574.6, 134.5, 580.3, 134.4]], "area": 371.05, "iscrowd": 0, "image_id": 0, "bbox": [569.0, 116.7, 18.1, 20.5], "category_id": 1, "id": 10, "ignore": 0}, 

{"segmentation": [[931.5, 271.4, 943.9, 269.6, 936.0, 278.6, 935.2, 286.0, 944.5, 284.4]], "area": 1200.3700000000001, "iscrowd": 0, "image_id": 1, "bbox": [927.0, 255.3, 30.7, 39.1], "category_id": 1, "id": 11, "ignore": 0},
{"segmentation": [[473.7, 339.6, 481.5, 339.6, 474.1, 345.6, 475.3, 351.7, 481.4, 351.4]], "area": 818.4, "iscrowd": 0, "image_id": 1, "bbox": [471.2, 326.5, 24.8, 33.0], "category_id": 1, "id": 12, "ignore": 0},
{"segmentation": [[28.5, 326.5, 34.1, 323.5, 31.7, 333.3, 39.8, 337.5, 42.5, 334.7]], "area": 912.0300000000001, "iscrowd": 0, "image_id": 1, "bbox": [23.9, 310.9, 30.1, 30.3], "category_id": 1, "id": 13, "ignore": 0}, 
{"segmentation": [[413.6, 314.8, 418.6, 315.9, 412.2, 319.4, 412.7, 324.3, 416.4, 324.8]], "area": 477.52, "iscrowd": 0, "image_id": 1, "bbox": [410.3, 305.7, 18.8, 25.4], "category_id": 1, "id": 14, "ignore": 0}, 
{"segmentation": [[599.5, 263.3, 614.7, 263.0, 603.4, 272.6, 601.1, 283.4, 612.3, 283.2]], "area": 1978.4599999999998, "iscrowd": 0, "image_id": 1, "bbox": [594.8, 242.8, 37.4, 52.9], "category_id": 1, "id": 15, "ignore": 0}, 
{"segmentation": [[828.2, 290.6, 839.6, 290.0, 832.2, 297.0, 830.0, 303.8, 838.8, 303.3]], "area": 865.8, "iscrowd": 0, "image_id": 1, "bbox": [824.6, 278.5, 26.0, 33.3], "category_id": 1, "id": 16, "ignore": 0},
...
{"segmentation": [[738.1, 329.4, 747.3, 329.3, 741.2, 334.4, 738.8, 340.2, 746.2, 340.1]], "area": 607.7599999999999, "iscrowd": 0, "image_id": 1, "bbox": [734.6, 319.3, 21.4, 28.4], "category_id": 1, "id": 17, "ignore": 0}, 
{"segmentation": [[876.7, 210.8, 877.4, 211.2, 874.3, 218.4, 878.8, 225.6, 879.2, 225.7]], "area": 847.5, "iscrowd": 0, "image_id": 12862, "bbox": [872.6, 197.0, 22.6, 37.5], "category_id": 1, "id": 113613, "ignore": 0}], 
"categories":
[
{"name": "background", "id": 0}, 
{"name": "face", "id": 1}]}

典型的coco数据结构,我基本参考了原来的cocoapi,但也做了不小的改动,主要包括:

  1. 显示方式发生了变化,这次简单地显示成多边形吧(其实也可以用数据点或数字)
  2. mask(RLE)不需要了,因此没有maskutil,无需编译安装,直接python搞定

源码分成两个文件,一个是主文件,随便取个名吧:unknown.py

# @MxTan from SpaceVision SZ Co.Ltd 
#
# @brief  for display landmark annotations piece by piece
#
# ref. windows version cocoapi if you need a mask version
# https://github.com/philferriere/cocoapi
#
#

from CoLandMark import LandMark
import numpy as np
import skimage.io as io #conda install scikit-image
import json
import os
 
import matplotlib as mpl
mpl.use('TkAgg')
import pylab
import matplotlib.rcsetup as rcsetup
pylab.rcParams['figure.figsize'] = (8.0, 10.0)
 
dataDir='D:/vsAI/libfacedetectiontrain/data/widerface/WIDER_train/images'
annFile= 'trainset.json'
 
# initialize COCO api for instance annotations
coco=LandMark(annFile)
 
# display COCO categories
catIds = coco.getCatIds()
cats = coco.loadCats(catIds)
nms=[cat['name'] for cat in cats] 
print('COCO format categories: \n{}\n'.format(' '.join(nms)))
 
# recursively display all images and its masks
imgIds = coco.getImgIds()
for id in imgIds: 
    mpl.pyplot.clf()  #put a stop breakpoint here, each cycle you will see a marked image
    annIds = coco.getAnnIds([id], catIds=catIds, iscrowd=None)
    anns = coco.loadAnns(annIds)
    imgIds = coco.getImgIds(imgIds = [id])
    img = coco.loadImgs(imgIds[0])[0]

    #----- save seperate image ----
    #file_name_ext='./WIDER_train/images/' + img['file_name']
    #(filename,extension) = os.path.splitext(file_name_ext)
    #file_path = "coco/" + filename + ".json"
    #data = {"annotations":anns}
    #with open(file_path, 'w') as result_file:
    #    json.dump(data, result_file)
    
    #----display image----
    file_path = '{}/{}'.format(dataDir,img['file_name'])
    I = io.imread(file_path) 
    #NOTE: the above method is equivalent to the following format
    #I = io.imread('%s/%s'%(dataDir,img['file_name']))  

    mpl.pyplot.imshow(I)
    mpl.pyplot.axis('off')
    coco.showAnns(anns)

标注工具文件取名叫做CoLandMark.py(原来叫COCO.py),里面那个类改名为LandMark,以避免同时使用原来的CoCo时冲突。

__author__ = 'tylin'
__version__ = '2.0'

# A copy from CocoApi, but some modifications are made to cope withe the landmark display
#
# AN alternative import lib for landmark points (NO area rle code required, so we removed the mask part)

# 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 json
import time
import numpy as np
import copy
import itertools

import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
from matplotlib.collections import PatchCollection
from matplotlib.patches import Polygon

import os
from collections import defaultdict
import sys
PYTHON_VERSION = sys.version_info[0]
if PYTHON_VERSION == 2:
    from urllib import urlretrieve
elif PYTHON_VERSION == 3:
    from urllib.request import urlretrieve


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


class LandMark:
    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 not annotation_file == None:
            print('loading annotations into memory...')
            tic = time.time()
            dataset = json.load(open(annotation_file, 'r'))
            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()

    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 not iscrowd == None:
            ids = [ann['id'] for ann in anns if ann['iscrowd'] == iscrowd]
        else:
            ids = [ann['id'] for ann in anns]
        return ids

    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 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 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]]

    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]]

    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]]

    def showAnns(self, anns):
        """
        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':
            #plt.clf()  #clear the foreground image
            #plt.cla()  # clear the axis
            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)
            #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 = LandMark()
        res.dataset['images'] = [img for img in self.dataset['images']]

        print('Loading and preparing results...')
        tic = time.time()

        # Check result type in a way compatible with Python 2 and 3.
        if PYTHON_VERSION == 2:
            is_string =  isinstance(resFile, basestring)  # Python 2
        elif PYTHON_VERSION == 3:
            is_string = isinstance(resFile, str)  # Python 3
        if is_string:
            anns = json.load(open(resFile))
        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 not 'segmentation' 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 not 'bbox' 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

    # 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

注释掉的部分我没有删除,大家可以和原文比较。

  人工智能 最新文章
2022吴恩达机器学习课程——第二课(神经网
第十五章 规则学习
FixMatch: Simplifying Semi-Supervised Le
数据挖掘Java——Kmeans算法的实现
大脑皮层的分割方法
【翻译】GPT-3是如何工作的
论文笔记:TEACHTEXT: CrossModal Generaliz
python从零学(六)
详解Python 3.x 导入(import)
【答读者问27】backtrader不支持最新版本的
上一篇文章      下一篇文章      查看所有文章
加:2021-09-23 11:28:02  更:2021-09-23 11:28:38 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年11日历 -2024/11/27 12:57:15-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码