-
Notifications
You must be signed in to change notification settings - Fork 1
/
validation.py
151 lines (123 loc) · 3.53 KB
/
validation.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
import json
import PIL.Image as Image
from PIL import ImageDraw, ImageFont
import MaskExam as Mask
import numpy as np
import math
import os
import common
path = "dataset/trainval/"
jsonName = "val.json"
ret1 = []
ret2 = []
jsonPath = path + "/" + jsonName
b = json.load(open(jsonPath))
immNum = 0
vector = []
iter = 0
idx = 0
tot = len(b)
idx_2 = 0
success = 0
total = 0
classes = {}
def coordToMatrix(coord, w, h):
img_size = (w, h)
poly = Image.new("RGB", img_size)
pdraw = ImageDraw.Draw(poly)
pdraw.polygon(coord,
fill=(255,255,255), outline=(255,255,255))
#poly = poly.transpose(Image.FLIP_LEFT_RIGHT)
#poly = poly.rotate(180)
#pix = np.array(poly.getdata()).reshape(w, h)
return poly
def find_centroid(im):
width, height = im.size
XX, YY, count = 0, 0, 0
for x in range(0, width, 1):
for y in range(0, height, 1):
if im.getpixel((x, y)) == (255,255,255):
XX += x
YY += y
count += 1
return XX/count, YY/count
def compute_area(im):
width, height = im.size
area = 0
for x in range(0, width, 1):
for y in range(0, height, 1):
if im.getpixel((x, y)) == (255,255,255):
area += 1
return area
def find_max_coord(x, y):
x_max = 0
x_min = 10000000
y_max = 0
y_min = 10000000
for indice in range(len(x)):
if x[indice] < x_min:
x_min = x[indice]
if y[indice] < y_min:
y_min = y[indice]
if x[indice] > x_max:
x_max = x[indice]
if y[indice] > y_max:
y_max = y[indice]
return [x_max, x_min, y_max, y_min]
def cade_internamente(max, centroide):
if centroide[0]< max[0] and centroide[0] > max[1]:
if centroide[1]< max[2] and centroide[1] > max[3]:
return True
return False
iter= 0
for json_elem in b:
# read image for sizes
iter+= 1
try:
im = Image.open(json_elem).convert('RGB')
except:
print("%d/%d" % (iter,tot), "[MISSING]", json_elem)
continue
print("%d/%d" % (iter,tot), "[CHECKING]", json_elem)
w, h = im.size
im = np.array(im)
seg = b[json_elem]
for m in seg:
mask = seg[m]
name = seg[m]['class']
maskMat = []
idClassi = []
idss = []
centroidi_lista = []
aree = []
max_coord = []
class_id_name = common.all_classes.index(name)
idClassi.append(classes.get(name))
x_coord = []
y_coord = []
for k in range(len(seg[m]['polygon'][0]['all_points_x'])):
y_coord.append(seg[m]['polygon'][0]['all_points_y'][k])
x_coord.append(seg[m]['polygon'][0]['all_points_x'][k])
coord = []
for ind in range(len(x_coord)):
coord.append(x_coord[ind])
coord.append(y_coord[ind])
immagine = coordToMatrix(coord, w, h)
# not used. In any case can generate an exception because of mistakes in json file (mask coordinate)
#centroidi_lista.append(find_centroid(immagine))
aree.append(compute_area(immagine))
idss.append(class_id_name)
max_coord.append(find_max_coord(x_coord, y_coord))
centroidi_lista_mask, idss_mask, aree_mask = Mask.centreAnalisi(im, w, h)
for indice in range(len(idss)):
total += 1
for indice_mask in range(len(idss_mask)):
# considering only the network areas in a range of 50%-150% of the mask in the original image
if (aree[indice] * 0.5) < aree_mask[indice_mask] and aree_mask[indice_mask] < (aree[indice] * 1.5):
# check if network centroids fall inside the bounding box of in the the mask original image
if cade_internamente(max_coord[indice], centroidi_lista_mask[indice_mask]):
if idss_mask[indice_mask] == idss[indice]:
success += 1
print("Numero di successi: " + str(success))
print("Numero totale label: " + str(total))
print("Percentuale di successo: "+ str(float(success) / float(total) ) + "%")