CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
hukaixuan19970627

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.

GitHub Repository: hukaixuan19970627/yolov5_obb
Path: blob/master/DOTA_devkit/DOTA.py
Views: 475
1
#The code is used for visulization, inspired from cocoapi
2
# Licensed under the Simplified BSD License [see bsd.txt]
3
4
import os
5
import matplotlib.pyplot as plt
6
from matplotlib.collections import PatchCollection
7
from matplotlib.patches import Polygon, Circle
8
import numpy as np
9
import dota_utils as util
10
from collections import defaultdict
11
import cv2
12
13
def _isArrayLike(obj):
14
if type(obj) == str:
15
return False
16
return hasattr(obj, '__iter__') and hasattr(obj, '__len__')
17
18
class DOTA:
19
def __init__(self, basepath):
20
self.basepath = basepath
21
self.labelpath = os.path.join(basepath, 'labelTxt')
22
self.imagepath = os.path.join(basepath, 'images')
23
self.imgpaths = util.GetFileFromThisRootDir(self.labelpath)
24
self.imglist = [util.custombasename(x) for x in self.imgpaths]
25
self.catToImgs = defaultdict(list)
26
self.ImgToAnns = defaultdict(list)
27
self.createIndex()
28
29
def createIndex(self):
30
for filename in self.imgpaths:
31
objects = util.parse_dota_poly(filename)
32
imgid = util.custombasename(filename)
33
self.ImgToAnns[imgid] = objects
34
for obj in objects:
35
cat = obj['name']
36
self.catToImgs[cat].append(imgid)
37
38
def getImgIds(self, catNms=[]):
39
"""
40
:param catNms: category names
41
:return: all the image ids contain the categories
42
"""
43
catNms = catNms if _isArrayLike(catNms) else [catNms]
44
if len(catNms) == 0:
45
return self.imglist
46
else:
47
imgids = []
48
for i, cat in enumerate(catNms):
49
if i == 0:
50
imgids = set(self.catToImgs[cat])
51
else:
52
imgids &= set(self.catToImgs[cat])
53
return list(imgids)
54
55
def loadAnns(self, catNms=[], imgId = None, difficult=None):
56
"""
57
:param catNms: category names
58
:param imgId: the img to load anns
59
:return: objects
60
"""
61
catNms = catNms if _isArrayLike(catNms) else [catNms]
62
objects = self.ImgToAnns[imgId]
63
if len(catNms) == 0:
64
return objects
65
outobjects = [obj for obj in objects if (obj['name'] in catNms)]
66
return outobjects
67
def showAnns(self, objects, imgId, range):
68
"""
69
:param catNms: category names
70
:param objects: objects to show
71
:param imgId: img to show
72
:param range: display range in the img
73
:return:
74
"""
75
img = self.loadImgs(imgId)[0]
76
plt.imshow(img)
77
plt.axis('off')
78
79
ax = plt.gca()
80
ax.set_autoscale_on(False)
81
polygons = []
82
color = []
83
circles = []
84
r = 5
85
for obj in objects:
86
c = (np.random.random((1, 3)) * 0.6 + 0.4).tolist()[0]
87
poly = obj['poly']
88
polygons.append(Polygon(poly))
89
color.append(c)
90
point = poly[0]
91
circle = Circle((point[0], point[1]), r)
92
circles.append(circle)
93
p = PatchCollection(polygons, facecolors=color, linewidths=0, alpha=0.4)
94
ax.add_collection(p)
95
p = PatchCollection(polygons, facecolors='none', edgecolors=color, linewidths=2)
96
ax.add_collection(p)
97
p = PatchCollection(circles, facecolors='red')
98
ax.add_collection(p)
99
def loadImgs(self, imgids=[]):
100
"""
101
:param imgids: integer ids specifying img
102
:return: loaded img objects
103
"""
104
print('isarralike:', _isArrayLike(imgids))
105
imgids = imgids if _isArrayLike(imgids) else [imgids]
106
print('imgids:', imgids)
107
imgs = []
108
for imgid in imgids:
109
filename = os.path.join(self.imagepath, imgid + '.png')
110
print('filename:', filename)
111
img = cv2.imread(filename)
112
imgs.append(img)
113
return imgs
114
115
if __name__ == '__main__':
116
examplesplit = DOTA('/media/test/4d846cae-2315-4928-8d1b-ca6d3a61a3c6/DOTA/DOTAv2.0/trainval_split_1024')
117
imgids = examplesplit.getImgIds(catNms=['plane'])
118
img = examplesplit.loadImgs(imgids)
119
for imgid in imgids:
120
anns = examplesplit.loadAnns(imgId=imgid)
121
examplesplit.showAnns(anns, imgid, 2)
122