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/ResultMerge.py
Views: 475
1
"""
2
To use the code, users should to config detpath, annopath and imagesetfile
3
detpath is the path for 15 result files, for the format, you can refer to "http://captain.whu.edu.cn/DOTAweb/tasks.html"
4
search for PATH_TO_BE_CONFIGURED to config the paths
5
Note, the evaluation is on the large scale images
6
"""
7
import os
8
import numpy as np
9
import dota_utils as util
10
import re
11
import time
12
import polyiou
13
14
# the thresh for nms when merge image
15
nms_thresh = 0.3
16
17
18
def py_cpu_nms_poly(dets, thresh):
19
scores = dets[:, 8]
20
polys = []
21
areas = []
22
for i in range(len(dets)):
23
tm_polygon = polyiou.VectorDouble([dets[i][0], dets[i][1],
24
dets[i][2], dets[i][3],
25
dets[i][4], dets[i][5],
26
dets[i][6], dets[i][7]])
27
polys.append(tm_polygon)
28
order = scores.argsort()[::-1]
29
30
keep = []
31
while order.size > 0:
32
ovr = []
33
i = order[0]
34
keep.append(i)
35
for j in range(order.size - 1):
36
iou = polyiou.iou_poly(polys[i], polys[order[j + 1]])
37
ovr.append(iou)
38
ovr = np.array(ovr)
39
inds = np.where(ovr <= thresh)[0]
40
order = order[inds + 1]
41
return keep
42
43
44
def py_cpu_nms(dets, thresh):
45
"""Pure Python NMS baseline."""
46
#print('dets:', dets)
47
x1 = dets[:, 0]
48
y1 = dets[:, 1]
49
x2 = dets[:, 2]
50
y2 = dets[:, 3]
51
scores = dets[:, 4]
52
53
areas = (x2 - x1 + 1) * (y2 - y1 + 1)
54
# index for dets
55
order = scores.argsort()[::-1]
56
57
keep = []
58
while order.size > 0:
59
i = order[0]
60
keep.append(i)
61
xx1 = np.maximum(x1[i], x1[order[1:]])
62
yy1 = np.maximum(y1[i], y1[order[1:]])
63
xx2 = np.minimum(x2[i], x2[order[1:]])
64
yy2 = np.minimum(y2[i], y2[order[1:]])
65
66
w = np.maximum(0.0, xx2 - xx1 + 1)
67
h = np.maximum(0.0, yy2 - yy1 + 1)
68
inter = w * h
69
ovr = inter / (areas[i] + areas[order[1:]] - inter)
70
71
inds = np.where(ovr <= thresh)[0]
72
order = order[inds + 1]
73
74
return keep
75
76
77
def nmsbynamedict(nameboxdict, nms, thresh):
78
nameboxnmsdict = {x: [] for x in nameboxdict}
79
for imgname in nameboxdict:
80
#print('imgname:', imgname)
81
#keep = py_cpu_nms(np.array(nameboxdict[imgname]), thresh)
82
#print('type nameboxdict:', type(nameboxnmsdict))
83
#print('type imgname:', type(imgname))
84
#print('type nms:', type(nms))
85
keep = nms(np.array(nameboxdict[imgname]), thresh)
86
#print('keep:', keep)
87
outdets = []
88
#print('nameboxdict[imgname]: ', nameboxnmsdict[imgname])
89
for index in keep:
90
# print('index:', index)
91
outdets.append(nameboxdict[imgname][index])
92
nameboxnmsdict[imgname] = outdets
93
return nameboxnmsdict
94
95
96
def poly2origpoly(poly, x, y, rate):
97
origpoly = []
98
for i in range(int(len(poly)/2)):
99
tmp_x = float(poly[i * 2] + x) / float(rate)
100
tmp_y = float(poly[i * 2 + 1] + y) / float(rate)
101
origpoly.append(tmp_x)
102
origpoly.append(tmp_y)
103
return origpoly
104
105
106
def mergebase(srcpath, dstpath, nms):
107
filelist = util.GetFileFromThisRootDir(srcpath)
108
for fullname in filelist:
109
name = util.custombasename(fullname)
110
#print('name:', name)
111
dstname = os.path.join(dstpath, name + '.txt')
112
with open(fullname, 'r') as f_in:
113
nameboxdict = {}
114
lines = f_in.readlines()
115
splitlines = [x.strip().split(' ') for x in lines]
116
for splitline in splitlines:
117
subname = splitline[0]
118
splitname = subname.split('__')
119
oriname = splitname[0]
120
pattern1 = re.compile(r'__\d+___\d+')
121
#print('subname:', subname)
122
x_y = re.findall(pattern1, subname)
123
x_y_2 = re.findall(r'\d+', x_y[0])
124
x, y = int(x_y_2[0]), int(x_y_2[1])
125
126
pattern2 = re.compile(r'__([\d+\.]+)__\d+___')
127
128
rate = re.findall(pattern2, subname)[0]
129
130
confidence = splitline[1]
131
poly = list(map(float, splitline[2:]))
132
origpoly = poly2origpoly(poly, x, y, rate)
133
det = origpoly
134
det.append(confidence)
135
det = list(map(float, det))
136
if (oriname not in nameboxdict):
137
nameboxdict[oriname] = []
138
nameboxdict[oriname].append(det)
139
nameboxnmsdict = nmsbynamedict(nameboxdict, nms, nms_thresh)
140
with open(dstname, 'w') as f_out:
141
for imgname in nameboxnmsdict:
142
for det in nameboxnmsdict[imgname]:
143
#print('det:', det)
144
confidence = det[-1]
145
bbox = det[0:-1]
146
outline = imgname + ' ' + \
147
str(confidence) + ' ' + ' '.join(map(str, bbox))
148
#print('outline:', outline)
149
f_out.write(outline + '\n')
150
151
152
def mergebyrec(srcpath, dstpath):
153
"""
154
srcpath: result files before merge and nms
155
dstpath: result files after merge and nms
156
"""
157
# srcpath = r'E:\bod-dataset\results\bod-v3_rfcn_2000000'
158
# dstpath = r'E:\bod-dataset\results\bod-v3_rfcn_2000000_nms'
159
160
mergebase(srcpath,
161
dstpath,
162
py_cpu_nms)
163
164
165
def mergebypoly(srcpath, dstpath):
166
"""
167
srcpath: result files before merge and nms
168
dstpath: result files after merge and nms
169
"""
170
mergebase(srcpath,
171
dstpath,
172
py_cpu_nms_poly)
173
174
175
if __name__ == '__main__':
176
# see demo for example
177
mergebypoly(r'work_dirs/temp/result_raw',\
178
r'work_dirs/temp/result_merge')
179
180