-
Notifications
You must be signed in to change notification settings - Fork 9
/
test.py
237 lines (166 loc) · 8.36 KB
/
test.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
import time
import json
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.ticker import FuncFormatter
import matConverter
import plyConverter
from distances import averageDistance, hausdorffDistance
from alignment_rigid_3D import alignment_rigid, max_iterations
def classification_test(testers, distance=None):
start = time.time()
with open("classificationTestResults.txt", "w+") as tr:
tr.write("Parameters: " + "\n")
tr.write("-max_iterations = " + str(max_iterations) + "\n")
tr.write("-compressionLevel = " + str(plyConverter.compressionLevel) + ", " + str(matConverter.compressionLevel)
+ "\n")
tr.write("-radius from nose = " + str(plyConverter.radius) + "\n")
if distance == "hausdorff":
tr.write("-hausdorff" + "\n")
truePositives = []
for tester in testers:
source = "data/Tester_" + str(tester) + "_pose_0_final_frontal.txt"
results = dict()
tr.write('\n')
tr.write("Reference: " + source[5:15] + '\n')
for k in testers:
print("Alignment with " + str(k))
groundtruth = "groundtruth/Tester_" + str(k) + "/Tester_" + str(k) + "_pose_0.txt"
alignment_rigid(target=groundtruth, source=source)
print("Calculating distance")
if distance == "hausdorff":
distTot = hausdorffDistance(target=groundtruth, source="output.txt")
else:
distTot = averageDistance(target=groundtruth, source="output.txt")[1]
results[k] = distTot
line = "Distance from tester " + str(k) + " --> " + str(distTot)
print(line)
tr.write(line + '\n')
argMin = np.infty
minDist = np.infty
for key in results.keys():
if results[key] < minDist:
minDist = results[key]
argMin = key
if argMin == tester:
tr.write("Corrected classification of tester " + str(tester) + "\n")
truePositives.append(argMin)
else:
tr.write("Wrong classification of tester " + str(tester) + " with tester " + str(argMin) + "\n")
tr.write("\n")
tr.write("Success rate --> " + str((len(truePositives) / len(testers)) * 100) + "\n")
end = time.time()
print(str(end - start))
tr.write("Elapsed time: " + str(end - start))
def distancesTest(testers, poses):
start = time.time()
with open("distancesTestResult.txt", "w+") as tr:
tr.write("Parameters: " + "\n")
tr.write("-max_iterations = " + str(max_iterations) + "\n")
tr.write("-compressionLevel = " + str(plyConverter.compressionLevel) + ", " + str(matConverter.compressionLevel)
+ "\n")
tr.write("-radius from nose = " + str(plyConverter.radius) + "\n")
analytics = {}
with open("analytics.json", "w+") as aj:
for tester in testers:
for pose in poses:
source = "data/Tester_" + str(tester) + "_pose_" + str(pose) + "_final_frontal.txt"
target = "groundtruth/Tester_" + str(tester) + "/Tester_" + str(tester) + "_pose_" + str(pose) + ".txt"
tr.write("\n")
tr.write("\n")
tr.write("Tester " + str(tester) + " with pose " + str(pose) + "\n")
tr.write("\n")
tr.write("Distances from groundtruth to source:" + "\n")
print("Alignment " + str(tester) + " with pose " + str(pose))
alignment_rigid(target, source)
distancesDict = {}
print("Distance calculation of Tester " + str(tester) + " with pose " + str(pose)
+ " from groundtruth to source")
mn, avg, mx, median = averageDistance(target, "output.txt")
hausdorff = hausdorffDistance(target, "output.txt")
tr.write("Min --> " + str(mn) + "\n")
tr.write("Average --> " + str(avg) + "\n")
tr.write("Max --> " + str(mx) + "\n")
tr.write("Median --> " + str(median) + "\n")
tr.write("Hausdorff distance --> " + str(hausdorff) + "\n")
distancesDict["min_sg"] = mn
distancesDict["avg_sg"] = avg
distancesDict["max_sg"] = mx
distancesDict["median_sg"] = median
distancesDict["hausdorff_sg"] = hausdorff
tr.write("\n")
tr.write("Distances from source to groundtruth:" + "\n")
print("Distance calculation of Tester " + str(tester) + " with pose " + str(pose)
+ " from source to groundtruth")
mn, avg, mx, median = averageDistance("output.txt", target)
hausdorff = hausdorffDistance("output.txt", target)
tr.write("Min --> " + str(mn) + "\n")
tr.write("Average --> " + str(avg) + "\n")
tr.write("Max --> " + str(mx) + "\n")
tr.write("Median --> " + str(median) + "\n")
tr.write("Hausdorff distance --> " + str(hausdorff) + "\n")
distancesDict["min_gs"] = mn
distancesDict["avg_gs"] = avg
distancesDict["max_gs"] = mx
distancesDict["median_gs"] = median
distancesDict["hausdorff_gs"] = hausdorff
analytics["Tester_" + str(tester) + "_pose_" + str(pose)] = distancesDict
json.dump(analytics, aj, indent=4)
analytics.clear()
end = time.time()
print(str(end - start))
tr.write("\n")
tr.write("\n")
tr.write("Time elapsed: " + str(end - start))
def posesPrecision(testers, poses):
with open("analytics.json", "r") as an:
analytics = json.load(an)
# Dizionario che contiene tutti i valori di riferimento della media delle distanze, in particolare viene preso
# come refernce la pose 0 (espressione neutra)
references = {}
for tester in testers:
avg_gs = analytics["Tester_" + str(tester) + "_pose_0"]["avg_gs"]
avg_sg = analytics["Tester_" + str(tester) + "_pose_0"]["avg_sg"]
references[tester] = (avg_gs + avg_sg)/2
sum = 0
for key in references.keys():
sum += references[key]
print(sum/(len(references.keys())))
# Per ogni posa calcola il rapporto con la media delle distanze medie di ciascun tester e rende una percentuale
# la quale rappresenta quanto rappresenta bene la posa rispetto al valore di riferimento (espressione neutra)
posesPercentage = []
for pose in poses[1:]:
poseAverage = 0
for tester in testers:
avg_gs = analytics["Tester_" + str(tester) + "_pose_" + str(pose)]["avg_gs"]
avg_sg = analytics["Tester_" + str(tester) + "_pose_" + str(pose)]["avg_sg"]
poseAverage += (((avg_gs + avg_sg)/2)/references[tester]) - 1
posesPercentage.append((poseAverage/len(testers)) * 100)
colors = []
for pose in posesPercentage:
if pose <= 5:
colors.append("green")
elif 5 < pose < 10:
colors.append("limegreen")
elif 10 < pose < 20:
colors.append("yellow")
elif 20 <= pose < 30:
colors.append("orange")
elif pose >= 30:
colors.append("red")
fig, ax = plt.subplots()
bars = plt.bar(x=np.asarray(poses[1:]), height=np.asarray(posesPercentage))
plt.xticks(np.asarray(poses[1:]))
plt.ylabel("Deviation from reference")
plt.xlabel("Poses")
for i in range(len(colors)):
bars[i].set_color(colors[i])
formatter = FuncFormatter(lambda y, pos: "%d%%" % (y))
ax.yaxis.set_major_formatter(formatter)
plt.title("Poses percentage precision")
plt.show()
caucasians = range(5, 6)
poses = range(20)
#classification_test(caucasians)
distancesTest(caucasians, poses)
posesPrecision(caucasians, poses)