Created
October 31, 2016 23:01
-
-
Save monajalal/7cb8f660848421a38447160f7e9fefff to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
import skimage.io as io | |
import matplotlib.pyplot as plt | |
import pylab | |
import sys | |
from pprint import pprint as p | |
from time import sleep | |
import os | |
sys.path.append('/home/mona/mscoco/coco/PythonAPI') | |
p(sys.path) | |
from pycocotools.coco import COCO | |
pylab.rcParams['figure.figsize'] = (10.0, 8.0) | |
dataDir='.' | |
dataType='train2014' | |
annFile='%s/annotations/instances_%s.json'%(dataDir,dataType) | |
coco = COCO(annFile) | |
cats = coco.loadCats(coco.getCatIds()) | |
nms = [cat['name'] for cat in cats] | |
annFile = '%s/annotations/captions_%s.json'%(dataDir,dataType) | |
coco_caps=COCO(annFile) | |
categories = {"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"} | |
for category in categories: | |
category_path = "/home/mona/mscoco/all_categories_mscoco_caption/"+category | |
if not os.path.exists(category_path): | |
os.makedirs(category_path) | |
image_count = 0 | |
catIds = coco.getCatIds(category) | |
imgIds = coco.getImgIds(catIds=catIds ); | |
for imgId in imgIds: | |
print("{0}: {1}".format("image id is", imgId)) | |
if image_count < 1: | |
image_count += 1 | |
img = coco.loadImgs(imgId)[0] | |
annIds = coco.getAnnIds(imgId, catIds=catIds, iscrowd = None) | |
anns = coco.loadAnns(annIds) | |
print("{0}: {1}".format("length of annotation is", len(anns))) | |
for ann in range(len(anns)): | |
print("annotation is") | |
print("{0}: {1}".format("category_id is", anns[ann]['category_id'])) | |
print("{0}: {1}".format("id is", anns[ann]['id'])) | |
print(coco.loadCats(anns[ann]['category_id'])) | |
coco.showAnns(anns) | |
annIds = coco_caps.getAnnIds(imgId); | |
anns = coco_caps.loadAnns(annIds) | |
filename = "/home/mona/mscoco/all_categories_mscoco_caption/"+category+'/' +category+'_'+str(imgId) + ".txt" | |
caption_file = open(filename, 'wb') | |
for i in range(5): | |
caption_file.write((anns[i]['caption']) + os.linesep) | |
caption_file.close() | |
else: | |
break | |
imgId = 152360 | |
print(type(coco.loadImgs)) | |
print(dir(coco.loadImgs)) | |
img = coco.loadImgs(imgId)[0] | |
print(img) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment