【cocoAPI】 cocoAPI teardown reports

Reference

  1. https://github.com/cocodataset/cocoapi/blob/master/PythonAPI/pycocoDemo.ipynb
  2. https://github.com/cocodataset/cocoapi/issues/36
  3. https://cocodataset.org/#detection-eval

본 파일은 아래에 저장되어 있습니다. 그리고 docker container 내부에서 구동해야, directory name 설정에 문제가 없습니다.

  • docker 내부에서는 /workspace/cocoapi
  • Ubuntu 내부에서는 /home/junha/docker/cocoapi
# =========================================== What is COCO API ===========================================


# In[4]:
get_ipython().run_line_magic('matplotlib', 'inline')
from pycocotools.coco import COCO
import numpy as np
import skimage.io as io
import matplotlib.pyplot as plt
import pylab
pylab.rcParams['figure.figsize'] = (8.0, 10.0)


# In[5]:
dataDir='/dataset/coco'
dataType='val2017'
annFile='{}/annotations/instances_{}.json'.format(dataDir,dataType)


# In[6]:


# initialize COCO api for instance annotations
coco=COCO(annFile)

"""
dir(coco)
[ 'annToMask',
 'annToRLE',
 'anns',
 'catToImgs',
 'cats',
 'createIndex',
 'dataset',
 'download',
 'getAnnIds',
 'getCatIds',
 'getImgIds',
 'imgToAnns',
 'imgs',
 'info',
 'loadAnns',
 'loadCats',
 'loadImgs',
 'loadNumpyAnnotations',
 'loadRes',
 'showAnns']
"""


# In[4]:


# display COCO categories and supercategories
cats = coco.loadCats(coco.getCatIds())
nms=[cat['name'] for cat in cats]
print('COCO categories: \n{}\n'.format(' '.join(nms))) # 80개의 class

nms1 = set([cat['supercategory'] for cat in cats])
print('COCO supercategories: \n{}'.format(' '.join(nms1))) # 12개의 class


# In[5]:


# get all images containing given categories, select one at random
catIds = coco.getCatIds(catNms=['person','dog','skateboard']); # catIds = [1, 18, 41]
imgIds = coco.getImgIds(catIds=catIds ); # imgIds = [549220, 324158, 279278] # 위 카테고리를 모두 포함하는 이미지 Id를 찾는다! (3개뿐)
# imgIds = coco.getImgIds(imgIds = [324158]);  # imgIds = [324158] 이런 방법도 있단다
imgs = coco.loadImgs(imgIds) # imgIDs에 있는 모든 id의 이미지를, coco.imgs 내용에서 추출해 온다.
imgId, img = imgIds[0], imgs[0]


# In[6]:


# load and display image
I = io.imread('%s/%s/%s'%(dataDir,dataType,img['file_name']))
#I = io.imread(img['coco_url'])
plt.axis('off')
plt.imshow(I)
plt.show()


# In[7]:

"""
1. anns['area']: segmentation area
2. anns['bbox']: [top left x position, top left y position, width, height]
3. small object: anns['area'] < 32**2 = 1024

reference
1. https://github.com/cocodataset/cocoapi/issues/36
2. https://cocodataset.org/#detection-eval
"""

# load and display instance annotations
plt.imshow(I); plt.axis('off')
annIds = coco.getAnnIds(imgIds=img['id'], catIds=catIds, areaRng=[0, 1024], iscrowd=None)
anns = coco.loadAnns(annIds)
coco.showAnns(anns)


# =========================================== Small object ===========================================

# In[4]:
get_ipython().run_line_magic('matplotlib', 'inline')
from pycocotools.coco import COCO
import numpy as np
import skimage.io as io
import matplotlib.pyplot as plt
import pylab
pylab.rcParams['figure.figsize'] = (8.0, 10.0)


# In[5]:
dataDir='/dataset/coco'
dataType='train2017' # 'val2017'
annFile='{}/annotations/instances_{}.json'.format(dataDir,dataType)


# In[6]:


# initialize COCO api for instance annotations
coco=COCO(annFile)



# In[4]:


# display COCO categories and supercategories
cats = coco.loadCats(coco.getCatIds())
nms=[cat['name'] for cat in cats] # 80개의 객체
print('COCO categories: \n{}\n'.format(' '.join(nms)))


# In[5]:
# image ID가 있을 때, 이미지를 load 해오는 방법

imgIds = coco.getImgIds(); # imgIds = [549220, 324158, 279278] # 위 카테고리를 모두 포함하는 이미지 Id를 찾는다! (3개뿐)
# imgIds = coco.getImgIds(imgIds = [324158]);  # imgIds = [324158] 이런 방법도 있단다
imgs = coco.loadImgs(imgIds) # imgIDs에 있는 모든 id의 이미지를, coco.imgs 내용에서 추출해 온다.
print('len(imgIds) = {}, len(imgs) = {}'.format(len(imgIds), len(imgs)))
# I = io.imread('%s/%s/%s'%(dataDir,dataType,img['file_name']))


# In[7]:

# 하나의 annotation에는 image ID가 존재한다. image ID로 위의 방법을 사용해서 이미지를 load한다. 

annIds = coco.getAnnIds(imgIds=imgIds, areaRng=[0, 1024], iscrowd=None)
anns = coco.loadAnns(annIds)
print('len(annIds) = {}, len(anns) = {}'.format(len(annIds), len(anns)))

#%%
dataDir='/dataset/coco'
dataType='train2017'
nmb_anns = len(anns)
for i, ann in enumerate(anns):
    imageName = 'img'+str(i).zfill(5) + '.jpg'
    save_path = '/workspace/cocoapi/train_small_object/class{}/'.format(str(ann['category_id']).zfill(2))
    I = io.imread('%s/%s/%s'%(dataDir,dataType,coco.loadImgs([ann['image_id']])[0]['file_name']))
    bb = np.round(ann['bbox']).astype(np.int32) 
    small_image = I[bb[1]:bb[1]+bb[3], bb[0]:bb[0]+bb[2]]
    if 0 not in small_image.shape:
        io.imsave(save_path+imageName, small_image)
    if i%1000 == 0 : print('{}%'.format(i/nmb_anns*100))
    

# ===========================================  Key Points ============================================
# In[8]:


# initialize COCO api for person keypoints annotations
annFile = '{}/annotations/person_keypoints_{}.json'.format(dataDir,dataType)
coco_kps=COCO(annFile)


# In[9]:


# load and display keypoints annotations
plt.imshow(I); plt.axis('off')
ax = plt.gca()
annIds = coco_kps.getAnnIds(imgIds=img['id'], catIds=catIds, iscrowd=None)
anns = coco_kps.loadAnns(annIds)
coco_kps.showAnns(anns)


# In[10]:


# initialize COCO api for caption annotations
annFile = '{}/annotations/captions_{}.json'.format(dataDir,dataType)
coco_caps=COCO(annFile)


# In[11]:


# load and display caption annotations
annIds = coco_caps.getAnnIds(imgIds=img['id']);
anns = coco_caps.loadAnns(annIds)
coco_caps.showAnns(anns)
plt.imshow(I); plt.axis('off'); plt.show()



© All rights reserved By Junha Song.