-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPAF.py
372 lines (302 loc) · 13 KB
/
PAF.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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
import torch
import numpy as np
from utils import calculateAffinityBtwCorners, corners2Vector, getCornersFromGaussMap, getCandidateSides, deleteCandidates
# FUNCTIONS:
# makeVecMaps
# generatePAF
# getSidesFromCorners
# connectGatesFromSides
# checkGates
# getGates
# Make PAF map
def makeVecMaps(image_dims, corners, v_points, vector_1, th_dist):
vx_map = np.zeros((image_dims[0],image_dims[1]))
vy_map = np.zeros((image_dims[0],image_dims[1]))
for i in range(len(vx_map)):
for j in range(len(vx_map[i])):
# Limited by the corner points
if (i < (min(corners[:,0] - th_dist)) or i > (max(corners[:,0] + th_dist))):
continue
if (j < (min(corners[:,1] - th_dist)) or j > (max(corners[:,1] + th_dist))):
continue
for x,y in v_points:
dist = np.sqrt((i-x)**2+(j-y)**2)
if dist < th_dist:
if dist < 1:
value = 1
else:
value = 1/dist
vx_map[i,j] = max(value,vx_map[i,j])
vy_map[i,j] = max(value,vy_map[i,j])
vx_map = vx_map * vector_1[0]
vy_map = vy_map * vector_1[1]
return vx_map, vy_map
def generatePAF(image, side_gates, scale_factor = 2, th_dist = 1):
if type(image) == torch.Tensor:
height = image.shape[1]
width = image.shape[2]
else:
height = image.shape[0]
width = image.shape[1]
height = height // scale_factor
width = width // scale_factor
image_dims = (width,height)
vx_map_sum = np.zeros(image_dims)
vy_map_sum = np.zeros(image_dims)
# If there is no corner detected, we return empty maps
if len(side_gates) > 0:
# v_points_plot = []
for side_gate in side_gates:
for corner in side_gate:
corner[0] = int(round(corner[0] * width,0))
corner[1] = int(round(corner[1] * height,0))
_, vector_1, v_points = corners2Vector(side_gate[0],side_gate[1])
vx_map, vy_map = makeVecMaps(image_dims, side_gate, v_points, vector_1, th_dist)
vx_map_sum += vx_map
vy_map_sum += vy_map
vx_map_sum = vx_map_sum.transpose()
vy_map_sum = vy_map_sum.transpose()
return vx_map_sum, vy_map_sum
def getSidesFromCorners(corners, vx_maps, vy_maps, conf_th = 0.91):
# Check points of the next corner
connected_sides_list = []
# Each 4 possible corners
for c_i in range(4):
connected_side_list = []
# next corner
c_j = (c_i + 1) % 4
candidate_sides = getCandidateSides(corners,c_i,c_j)
score_list = []
for side in candidate_sides:
# score_int = integratePathBtwCorners(side[0],side[1],vx_maps[c_i],vy_maps[c_i])
score = calculateAffinityBtwCorners(side[0],side[1],vx_maps[c_i],vy_maps[c_i])
score_list.append(score)
# Select the best candidate
while candidate_sides != []:
# idx_selected = np.argmin(score_list) # Integrate
idx_selected = np.argmax(score_list) # Affinity
# connected_side_list.append(candidate_sides[idx_selected])
if score_list[idx_selected] > conf_th:
# print(score_list[idx_selected])
side_dict = {'side':candidate_sides[idx_selected], 'score': score_list[idx_selected]}
connected_side_list.append(side_dict)
candidate_sides, score_list = deleteCandidates(candidate_sides, score_list, idx_selected)
connected_sides_list.append(connected_side_list)
# connected_sides_array = np.array(connected_sides_list)
return connected_sides_list
# if np.array_equal(sides[0][0][1],sides[1][0][0]):
# print(sides[0][0][1] == sides[1][0][0])
# else:
# print('False')
# corner_connected_list = []
# # Each point detected of this corner
# for p_i in range(len(corners[c_i])):
# # score_corner_list = []
# # Next corner
# def connectGatesFromSides(side_list):
# # side_list = np.array(side_list)
# n_points = []
# for side in side_list:
# n_points.append(len(side))
# n_gates = max(n_points)
# gate_list = []
# # print(side_list)
# for i in range(n_gates):
# print(i)
# gate = {'id':i,'c0':None,'c1':None,'c2':None,'c3':None,'c4':None,'n_corners':0,'score':0}
# # gate = []
# for j in range(4):
# # print(i,j,side_list)
# if j == 0:
# if len(side_list[j]) > i:
# gate['c0'] = side_list[j][i]['side'][0]
# gate['c1'] = side_list[j][i]['side'][1]
# # side_list[j].pop(i)
# # print('gate',gate)
# # print('aferr',side_list)
# gate['n_corners'] += 2
# else:
# continue
# else:
# for k in range(len(side_list[j])):
# c_prev = 'c'+str(j)
# if np.array_equal(gate[c_prev],side_list[j][k]['side'][0]):
# c_name = 'c'+str(j+1)
# gate[c_name] = side_list[j][k]['side'][1]
# # side_list[j].pop(k)
# # print('gate',gate)
# # print('aferr',side_list)
# gate['n_corners'] += 1
# # gate.append(side_list[j][k][1])
# elif (j == 3) & (np.array_equal(gate['c0'],side_list[j][k]['side'][1])):
# gate['c3'] = side_list[j][i]['side'][0]
# gate['c4'] = side_list[j][i]['side'][1]
# # side_list[j].pop(i)
# # print('gate',gate)
# # print('aferr',side_list)
# gate['n_corners'] += 2
# # gate = gate[:-1] # Last point is useful just to check if the gate is correct. It may be deleted.
# print(gate)
# gate_list.append(gate)
# return gate_list
def connectGatesFromSides(side_list):
gate_list = []
# new_gate = {'id':0,'c0':None,'c1':None,'c2':None,'c3':None,'c4':None,'n_corners':0,'score':0}
# gate_list.append(new_gate)
for i in range(4):
for j in range(len(side_list[i])):
# for side in side_list[i]:
connected = False
c_curr = 'c'+str(i)
c_next = 'c'+str((i+1) % 4)
for gate in gate_list:
if np.array_equal(side_list[i][j]['side'][0],gate[c_curr]):
if gate[c_next] is None:
gate[c_next] = side_list[i][j]['side'][1]
gate['n_corners'] += 1
else:
if np.array_equal(side_list[i][j]['side'][1],gate[c_next]):
print("Connection secure")
gate['score']=1
else:
print("Connection error")
gate['score']=-1
connected = True
break
if np.array_equal(side_list[i][j]['side'][1],gate[c_next]):
if gate[c_curr] is None:
gate[c_curr] = side_list[i][j]['side'][0]
gate['n_corners'] += 1
else:
if np.array_equal(side_list[i][j]['side'][0],gate[c_curr]):
print("Connection secure")
gate['score']=1
else:
print("Connection error")
gate['score']=-1
connected = True
break
if connected == False:
new_gate = {'id':0,'c0':None,'c1':None,'c2':None,'c3':None, 'n_corners':0,'score':0}
new_gate[c_curr] = side_list[i][j]['side'][0]
new_gate[c_next] = side_list[i][j]['side'][1]
new_gate['n_corners'] = 2
gate_list.append(new_gate)
side_list[i][j]=None
return gate_list
# def connectGatesFromSides(side_list):
# # side_list = np.array(side_list)
# n_points = []
# for side in side_list:
# n_points.append(len(side))
# n_gates = max(n_points)
# gate_list = []
# # print(side_list)
# for i in range(n_gates):
# print(i)
# gate = {'id':i,'c0':None,'c1':None,'c2':None,'c3':None,'c4':None,'n_corners':0,'score':0}
# # gate = []
# for j in range(4):
# # print(i,j,side_list)
# if j == 0:
# if len(side_list[j]) > i:
# gate['c0'] = side_list[j][i]['side'][0]
# gate['c1'] = side_list[j][i]['side'][1]
# # side_list[j].pop(i)
# # print('gate',gate)
# # print('aferr',side_list)
# gate['n_corners'] += 2
# else:
# continue
# else:
# for k in range(len(side_list[j])):
# c_prev = 'c'+str(j)
# if np.array_equal(gate[c_prev],side_list[j][k]['side'][0]):
# c_name = 'c'+str(j+1)
# gate[c_name] = side_list[j][k]['side'][1]
# # side_list[j].pop(k)
# # print('gate',gate)
# # print('aferr',side_list)
# gate['n_corners'] += 1
# # gate.append(side_list[j][k][1])
# elif (j == 3) & (np.array_equal(gate['c0'],side_list[j][k]['side'][1])):
# gate['c3'] = side_list[j][i]['side'][0]
# gate['c4'] = side_list[j][i]['side'][1]
# # side_list[j].pop(i)
# # print('gate',gate)
# # print('aferr',side_list)
# gate['n_corners'] += 2
# # gate = gate[:-1] # Last point is useful just to check if the gate is correct. It may be deleted.
# print(gate)
# gate_list.append(gate)
# return gate_list
def checkGates(gates):
# Check for union corner between gates
for i in range(len(gates)):
for j in range(len(gates)):
if i == j:
continue
if gates[i] is None or gates[j] is None:
continue
for k in range(4):
c_curr = 'c'+str(k)
if np.array_equal(gates[i][c_curr],gates[j][c_curr]) & (gates[i][c_curr] is not None):
# Join gates
for p in range(4):
c_join = 'c'+str(p)
if gates[i][c_join] is None:
gates[i][c_join] = gates[j][c_join]
gates[i]['n_corners'] += gates[j]['n_corners'] - 1
gates[j]=None
break
gates_checked = []
for gate in gates:
print(gate)
if gate is None:
continue
elif gate['score'] < 1:
continue
elif gate['n_corners'] < 4:
continue
else:
gates_checked.append(gate)
return gates_checked
def getSides(labels):
gauss_maps = labels[:4]
vx_maps = labels[4:8]
vy_maps = labels[8:]
corners = getCornersFromGaussMap(gauss_maps)
connected_sides = getSidesFromCorners(corners, vx_maps, vy_maps)
return connected_sides
def getGates(connected_sides):
connected_gates_dict = connectGatesFromSides(connected_sides)
checked_gates = checkGates(connected_gates_dict)
return checked_gates
def detectGates(labels):
detected_sides = getSides(labels)
detected_gates = getGates(detected_sides)
return detected_gates
# def connectSides(corners, vx_maps, vy_maps):
# # Check points of the next corner
# connected_sides_list = []
# # Each 4 possible corners
# for c_i in range(4):
# corner_connected_list = []
# # Each point detected of this corner
# for p_i in range(len(corners[c_i])):
# # Next corner
# c_j = (c_i + 1) % 4
# score_point_list = []
# # Each point detected of this corner
# for p_j in range(len(corners[c_j])):
# vx_map = vx_maps[c_i]
# vy_map = vy_maps[c_i]
# corner_0 = corners[c_i][p_i]
# corner_1 = corners[c_j][p_j]
# score_point = integratePathBtwCorners(corner_0,corner_1,vx_map,vy_map)
# score_point_list.append(score_point)
# idx_point_selected = np.argmin(score_point_list)
# corner_connected_list.append([corners[c_i][p_i],corners[c_j][idx_point_selected]])
# connected_sides_list.append(corner_connected_list)
# connected_sides_array = np.array(connected_sides_list)
# return connected_sides_array