一部のCOCOデータセットをダウンロードし、新しいjson寸法ファイルを生成

15093 ワード

コンピュータは完全なCOCOデータセットを走ることができなくて(辛抱強くそれが終わるのを待つことができません)、だから部分のピクチャーをダウンロードして走りたいです(ただmask rcnnを走ってみたいだけです)、cococoAPIの中でダウンロードのピクチャーのインタフェースを提供して、それに対して部分的に修正して、もとのjsonファイルの中からランダムに指定の数量のピクチャーをダウンロードしてそしてそれらのjsonの表記の情報を保留して再び1つの新しい小さいjsonに保存します.
以下のコードをcocoapi/pythonAPI/pycocotoolsの下でmyccoと命名する.py
# coding:utf8
__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 json
import time
import matplotlib.pyplot as plt
from matplotlib.collections import PatchCollection
from matplotlib.patches import Polygon
import numpy as np
import copy
import itertools
import os
from collections import defaultdict
import sys
import json
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 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()# imgToAnns           (mask)           
		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
		"""
		print(type(ids))
		if _isArrayLike(ids):
			return [self.imgs[id] for id in ids]
		elif type(ids) == int:
			return [self.imgs[ids]]

	

	def download2(self, tarDir = None,tarFile = './new.json', N = 1000 ):
		'''
		Download COCO images from mscoco.org server.
		:param tarDir (str): COCO results directory name
			   imgIds (list): images to be downloaded
		:return:
		
		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)
		'''
		with open(tarFile,'w+') as f:
			load_json = {'images':[],'annotations':[],'categories':[],'type':'instances',"info": {"description": "This is stable 1.0 version of the 2014 MS COCO dataset.", "url": "http:\/\/mscoco.org", "version": "1.0", "year": 2014, "contributor": "Microsoft COCO group", "date_created": "2015-01-27 09:11:52.357475"}, "licenses": [{"url": "http:\/\/creativecommons.org\/licenses\/by-nc-sa\/2.0\/", "id": 1, "name": "Attribution-NonCommercial-ShareAlike License"}, {"url": "http:\/\/creativecommons.org\/licenses\/by-nc\/2.0\/", "id": 2, "name": "Attribution-NonCommercial License"}, {"url": "http:\/\/creativecommons.org\/licenses\/by-nc-nd\/2.0\/", "id": 3, "name": "Attribution-NonCommercial-NoDerivs License"}, {"url": "http:\/\/creativecommons.org\/licenses\/by\/2.0\/", "id": 4, "name": "Attribution License"}, {"url": "http:\/\/creativecommons.org\/licenses\/by-sa\/2.0\/", "id": 5, "name": "Attribution-ShareAlike License"}, {"url": "http:\/\/creativecommons.org\/licenses\/by-nd\/2.0\/", "id": 6, "name": "Attribution-NoDerivs License"}, {"url": "http:\/\/flickr.com\/commons\/usage\/", "id": 7, "name": "No known copyright restrictions"}, {"url": "http:\/\/www.usa.gov\/copyright.shtml", "id": 8, "name": "United States Government Work"}]}
						
			for i in self.imgs:
				if(N==0):
					break
				tic = time.time()
				img = self.imgs[i]
				#print (self.imgs[i])
				load_json['images'].append(img)
				fname = os.path.join(tarDir, img['file_name'])
				#print(img['id'])
				#print(self.imgToAnns[img['id']])
				anns = self.imgToAnns[img['id']]
				for ann in anns:
					load_json['annotations'].append(ann)
				if not os.path.exists(fname):
					urlretrieve(img['url'], fname)
				print('downloaded {}/{} images (t={:0.1f}s)'.format(i, N, time.time()- tic))
				N-=1
			
			for i in self.cats:
				load_json['categories'].append(self.cats[i])
			json.dump(load_json,f)

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


例呼び出しは次のとおりです.
# coding:utf8
from my_coco import COCO
coco = COCO('instances_minival2014.json')  #    json
coco.download2('./images','./new.json',100) #            json