-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdetEva.py
169 lines (154 loc) · 5.46 KB
/
detEva.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
import numpy as np
import os
import shapely
from shapely.geometry import Polygon,MultiPoint
def line(x1, y1, x2, y2):
return ((float(x1) - float(x2))**2 + (float(y1) - float(y2))**2)**0.5
# for clockwise 8 point coordinates
def area(box, isrectangle=True):
if isrectangle:
box = [(box[0]+box[6])/2, (box[1]+box[3])/2, (box[2]+box[4])/2, (box[5]+box[7])/2]
return (box[2] - box[0]) * (box[3] - box[1])
# width = (line(box[0],box[1],box[2],box[3]) + line(box[4],box[5],box[6],box[7]))/2
# length = (line(box[0],box[1],box[6],box[7]) + line(box[2],box[3],box[4],box[5]))/2
# return width * length
# for quadrilateral
else:
box = np.array(box).reshape(4,2)
poly = Polygon(box).convex_hull
return poly.area
def union(gt, bbox, isrectangle=True):
return area(gt)+area(bbox) - intersection(gt, bbox)
def intersection(gt, bbox, isrectangle=True):
if isrectangle:
gt = [(gt[0]+gt[6])/2, (gt[1]+gt[3])/2, (gt[2]+gt[4])/2, (gt[5]+gt[7])/2]
bbox = [(bbox[0]+bbox[6])/2, (bbox[1]+bbox[3])/2, (bbox[2]+bbox[4])/2, (bbox[5]+bbox[7])/2]
# determine the (x, y)-coordinates of the intersection rectangle
xA = max(gt[0], bbox[0])
yA = max(gt[1], bbox[1])
xB = min(gt[2], bbox[2])
yB = min(gt[3], bbox[3])
# compute the area of intersection rectangle
interArea = max(0, xB - xA) * max(0, yB - yA)
else:
gt = np.array(gt).reshape(4,2)
bbox = np.array(bbox).reshape(4,2)
poly1 = Polygon(gt).convex_hull
poly2 = Polygon(bbox).convex_hull
interArea = poly1.intersection(poly2).area #intersection area
return interArea
def iou(gt, bbox):
return intersection(gt, bbox) / union(gt, bbox)
def recallMat(gt, bbox):
if area(gt) == 0:
return 0.
return intersection(gt, bbox) / area(gt)
def precisionMat(gt, bbox):
if area(bbox) == 0:
return 0.
return intersection(gt, bbox) / area(bbox)
def findIndex(num, mat, threshold, axis):
if axis == 0:
index = np.where(mat[num,:] >= threshold)[0]
elif axis == 1:
index = np.where(mat[:, num] >= threshold)[0]
return index
def detEva(gt, bbox, r=0.8, p=0.4):
rnum = 0.
pnum = 0.
n = len(gt) # gt size is n*8
m = len(bbox) # bbox size is m*8
# calculate RM and PM
RM = np.zeros([n, m])
PM = np.zeros([n, m])
# flagr[i]/flagp[j] is used to record whether the gt[i]/bbox[j] is matched
flagr = np.zeros(n)
flagp = np.zeros(m)
# ====================================================
# one to many
for i in range(n):
for j in range(m):
RM[i][j] = recallMat(gt[i], bbox[j])
PM[i][j] = precisionMat(gt[i], bbox[j])
# calculate rnum and pnum
for i in range(n):
index = findIndex(i, PM, p, axis=0)
if len(index) > 1:
sum = 0
for j in index:
if flagp[j] == 0:
sum += RM[i][j]
if sum >= r:
pnum += len(index)
rnum += 1
flagr[i] = 1
for j in index:
flagp[j] = 1
# ====================================================
# many to one
for j in range(m):
if flagp[j] == 1:
continue
index = findIndex(j, RM, r, axis=1)
if len(index) > 1:
sum = 0
for i in index:
if flagr[i] == 0:
sum += PM[i][j]
if sum >= p:
pnum += 1
rnum += len(index)
flagp[j] = 1
for i in index:
flagr[i] = 1
# ====================================================
# one to one
for i in range(n):
for j in range(m):
if (flagr[i]==0) and (flagp[j]==0) and (RM[i][j]>=r)and(PM[i][j]>= p): # (iou(gt[i], bbox[j])>=0.5):
rnum += 1
pnum += 1
flagr[i]=1
flagp[j]=1
break # to Simplify step
recall = rnum / n
precision = pnum / m
hmean = (recall + precision) / 2
return recall, precision, hmean
def load_box(file):
with open(file, 'r') as f:
lines = f.readlines()
box = []
for line in lines:
if line.strip().strip('\n'):
l = line.strip('\n').split(',')
box.append([float(l[i]) for i in range(8)])
return box
# the name of text in label and result is same
def evaluation(label='./data/test_label/text_task1&2/', result='./result/result_label/'):
accuracy = []
for file in os.listdir(result):
if file.split('.')[-1] != 'txt':
continue
gt = load_box(label+file)
bbox = load_box(result+file)
precision, recall, hmean = detEva(gt, bbox)
accuracy.append([file, precision, recall, hmean])
accuracy = sorted(accuracy, key=lambda x: x[3], reverse=True)
hmean = 0
# write a text to record accuracy
with open('./accuracy.txt', 'w') as f:
for a in accuracy:
f.write(a[0].split('.')[0]+',')
hmean = hmean + a[3]
# print hmean
for i in range(1, 4):
f.write('%.3f%%' % (a[i]*100))
if i < 3:
f.write(',')
f.write('\n')
print('==================================================')
print('The accuracy of hmean is: ',)
print ('%.3f%%' % (float(hmean*100)/float(len(accuracy))))
if __name__ == '__main__':
evaluation()