Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Path: blob/master/DOTA_devkit/DOTA.py
Views: 475
#The code is used for visulization, inspired from cocoapi1# Licensed under the Simplified BSD License [see bsd.txt]23import os4import matplotlib.pyplot as plt5from matplotlib.collections import PatchCollection6from matplotlib.patches import Polygon, Circle7import numpy as np8import dota_utils as util9from collections import defaultdict10import cv21112def _isArrayLike(obj):13if type(obj) == str:14return False15return hasattr(obj, '__iter__') and hasattr(obj, '__len__')1617class DOTA:18def __init__(self, basepath):19self.basepath = basepath20self.labelpath = os.path.join(basepath, 'labelTxt')21self.imagepath = os.path.join(basepath, 'images')22self.imgpaths = util.GetFileFromThisRootDir(self.labelpath)23self.imglist = [util.custombasename(x) for x in self.imgpaths]24self.catToImgs = defaultdict(list)25self.ImgToAnns = defaultdict(list)26self.createIndex()2728def createIndex(self):29for filename in self.imgpaths:30objects = util.parse_dota_poly(filename)31imgid = util.custombasename(filename)32self.ImgToAnns[imgid] = objects33for obj in objects:34cat = obj['name']35self.catToImgs[cat].append(imgid)3637def getImgIds(self, catNms=[]):38"""39:param catNms: category names40:return: all the image ids contain the categories41"""42catNms = catNms if _isArrayLike(catNms) else [catNms]43if len(catNms) == 0:44return self.imglist45else:46imgids = []47for i, cat in enumerate(catNms):48if i == 0:49imgids = set(self.catToImgs[cat])50else:51imgids &= set(self.catToImgs[cat])52return list(imgids)5354def loadAnns(self, catNms=[], imgId = None, difficult=None):55"""56:param catNms: category names57:param imgId: the img to load anns58:return: objects59"""60catNms = catNms if _isArrayLike(catNms) else [catNms]61objects = self.ImgToAnns[imgId]62if len(catNms) == 0:63return objects64outobjects = [obj for obj in objects if (obj['name'] in catNms)]65return outobjects66def showAnns(self, objects, imgId, range):67"""68:param catNms: category names69:param objects: objects to show70:param imgId: img to show71:param range: display range in the img72:return:73"""74img = self.loadImgs(imgId)[0]75plt.imshow(img)76plt.axis('off')7778ax = plt.gca()79ax.set_autoscale_on(False)80polygons = []81color = []82circles = []83r = 584for obj in objects:85c = (np.random.random((1, 3)) * 0.6 + 0.4).tolist()[0]86poly = obj['poly']87polygons.append(Polygon(poly))88color.append(c)89point = poly[0]90circle = Circle((point[0], point[1]), r)91circles.append(circle)92p = PatchCollection(polygons, facecolors=color, linewidths=0, alpha=0.4)93ax.add_collection(p)94p = PatchCollection(polygons, facecolors='none', edgecolors=color, linewidths=2)95ax.add_collection(p)96p = PatchCollection(circles, facecolors='red')97ax.add_collection(p)98def loadImgs(self, imgids=[]):99"""100:param imgids: integer ids specifying img101:return: loaded img objects102"""103print('isarralike:', _isArrayLike(imgids))104imgids = imgids if _isArrayLike(imgids) else [imgids]105print('imgids:', imgids)106imgs = []107for imgid in imgids:108filename = os.path.join(self.imagepath, imgid + '.png')109print('filename:', filename)110img = cv2.imread(filename)111imgs.append(img)112return imgs113114if __name__ == '__main__':115examplesplit = DOTA('/media/test/4d846cae-2315-4928-8d1b-ca6d3a61a3c6/DOTA/DOTAv2.0/trainval_split_1024')116imgids = examplesplit.getImgIds(catNms=['plane'])117img = examplesplit.loadImgs(imgids)118for imgid in imgids:119anns = examplesplit.loadAnns(imgId=imgid)120examplesplit.showAnns(anns, imgid, 2)121122