del far
Browse files- handcrafted_solution.py +79 -16
handcrafted_solution.py
CHANGED
|
@@ -190,19 +190,6 @@ def get_SfM_depth(points3D, depth_np, gest_seg_np, K, R, t, dilate_r = 5):
|
|
| 190 |
if (sfm_depth_np[j, i]!=0 and z < sfm_depth_np[j, i]) or (sfm_depth_np[j, i]==0):
|
| 191 |
sfm_depth_np[j, i] = z
|
| 192 |
sfm_color_np[j, i] = c
|
| 193 |
-
'''
|
| 194 |
-
for pt, c in zip(xyz, rgb):
|
| 195 |
-
x, y, z = pt
|
| 196 |
-
u, v = x/z, y/z
|
| 197 |
-
u = u.astype(np.int32)
|
| 198 |
-
v = v.astype(np.int32)
|
| 199 |
-
for i in range(max(0, u - dilate_r), min(W, u + dilate_r)):
|
| 200 |
-
for j in range(max(0, v - dilate_r), min(H, v + dilate_r)):
|
| 201 |
-
if z > 0:
|
| 202 |
-
if (sfm_depth_np[j, i]!=0 and z < sfm_depth_np[j, i]) or (sfm_depth_np[j, i]==0):
|
| 203 |
-
sfm_depth_np[j, i] = z
|
| 204 |
-
sfm_color_np[j, i] = c
|
| 205 |
-
'''
|
| 206 |
|
| 207 |
if DUMP_IMG:
|
| 208 |
filename_sfm_depth = 'sfm_depth.png'
|
|
@@ -472,7 +459,75 @@ def uv_to_v3d(uv, depth_vert, K, R, t):
|
|
| 472 |
vertices_3d = cv2.convertPointsFromHomogeneous(vertices_3d).reshape(-1, 3)
|
| 473 |
return vertices_3d
|
| 474 |
|
| 475 |
-
def
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 476 |
good_entry = convert_entry_to_human_readable(entry)
|
| 477 |
points3D = good_entry['points3d']
|
| 478 |
vert_edge_per_image = {}
|
|
@@ -514,12 +569,20 @@ def predict(entry, visualize=False, depth_scale=2.5, ) -> Tuple[np.ndarray, List
|
|
| 514 |
|
| 515 |
vertices_3d = uv_to_v3d(uv, depth_vert, K, R, t)
|
| 516 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 517 |
vert_edge_per_image[i] = vertices, connections, vertices_3d
|
| 518 |
all_3d_vertices, connections_3d = merge_vertices_3d(vert_edge_per_image, 3.0) # TODO: 3cm looks too small
|
| 519 |
#print(f'after merge, {len(all_3d_vertices)} 3d vertices and {len(connections_3d)} 3d connections')
|
| 520 |
#all_3d_vertices_clean, connections_3d_clean = prune_not_connected(all_3d_vertices, connections_3d)
|
| 521 |
-
all_3d_vertices_clean, connections_3d_clean
|
| 522 |
-
#
|
|
|
|
| 523 |
if (len(all_3d_vertices_clean) < 2) or len(connections_3d_clean) < 1:
|
| 524 |
print (f'Not enough vertices or connections in the 3D vertices')
|
| 525 |
return (good_entry['__key__'], *empty_solution())
|
|
|
|
| 190 |
if (sfm_depth_np[j, i]!=0 and z < sfm_depth_np[j, i]) or (sfm_depth_np[j, i]==0):
|
| 191 |
sfm_depth_np[j, i] = z
|
| 192 |
sfm_color_np[j, i] = c
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 193 |
|
| 194 |
if DUMP_IMG:
|
| 195 |
filename_sfm_depth = 'sfm_depth.png'
|
|
|
|
| 459 |
vertices_3d = cv2.convertPointsFromHomogeneous(vertices_3d).reshape(-1, 3)
|
| 460 |
return vertices_3d
|
| 461 |
|
| 462 |
+
def delete_one_vert(vertices, vertices_3d, connections, vert_to_del):
|
| 463 |
+
i = np.where(np.all(abs(vertices_3d - vert_to_del) < 0.01, axis=1))
|
| 464 |
+
#print(i)
|
| 465 |
+
#print(len(i[0]))
|
| 466 |
+
if len(i[0])==0:
|
| 467 |
+
if vertices:
|
| 468 |
+
return vertices, vertices_3d, connections
|
| 469 |
+
else:
|
| 470 |
+
return vertices, vertices_3d, connections
|
| 471 |
+
|
| 472 |
+
#print('to del idx=', i[0])
|
| 473 |
+
idx = i[0]#[0]
|
| 474 |
+
if vertices:
|
| 475 |
+
vertices = np.delete(vertices, idx)
|
| 476 |
+
vertices_3d = np.delete(vertices_3d, idx, axis=0)
|
| 477 |
+
conn_to_del = []
|
| 478 |
+
for i, c in enumerate(connections):
|
| 479 |
+
if c[0] == idx or c[1] == idx:
|
| 480 |
+
conn_to_del.append(i)
|
| 481 |
+
if c[0] >= idx:
|
| 482 |
+
connections[i] = (connections[i][0]-1, connections[i][1])
|
| 483 |
+
if c[1] >= idx:
|
| 484 |
+
connections[i] = (connections[i][0], connections[i][1]-1)
|
| 485 |
+
|
| 486 |
+
#print(f'del {len(conn_to_del)} connections')
|
| 487 |
+
connections = np.delete(connections, (conn_to_del), axis=0)
|
| 488 |
+
#print(vertices, vertices_3d, connections)
|
| 489 |
+
if vertices:
|
| 490 |
+
return vertices, vertices_3d, connections
|
| 491 |
+
else:
|
| 492 |
+
return vertices_3d, connections
|
| 493 |
+
|
| 494 |
+
def prune_far(all_3d_vertices, connections_3d, prune_dist_thr=3000):
|
| 495 |
+
'''Prune vertices that are far away from any the other vertices'''
|
| 496 |
+
if (len(all_3d_vertices) < 2) or len(connections_3d) < 1:
|
| 497 |
+
return all_3d_vertices, connections_3d
|
| 498 |
+
|
| 499 |
+
isolated = []
|
| 500 |
+
distmat = cdist(all_3d_vertices, all_3d_vertices)
|
| 501 |
+
for i, v in enumerate(distmat):
|
| 502 |
+
exclude_self = np.array([x for idx,x in enumerate(v) if idx!=i])
|
| 503 |
+
#print('excluded:', exclude_self)
|
| 504 |
+
#if np.any(exclude_self > prune_dist_thr):
|
| 505 |
+
exclude_self = abs(exclude_self)
|
| 506 |
+
if min(exclude_self) > prune_dist_thr:
|
| 507 |
+
#print('del a pt w/ dist = ', min(exclude_self))
|
| 508 |
+
isolated.append(i)
|
| 509 |
+
break
|
| 510 |
+
|
| 511 |
+
while isolated:
|
| 512 |
+
isolated_pt = isolated.pop()
|
| 513 |
+
#print('isolated:', isolated_pt)
|
| 514 |
+
pt_to_del = all_3d_vertices[isolated_pt]
|
| 515 |
+
all_3d_vertices, connections_3d = delete_one_vert([], all_3d_vertices, connections_3d, pt_to_del)
|
| 516 |
+
|
| 517 |
+
distmat = cdist(all_3d_vertices, all_3d_vertices)
|
| 518 |
+
for i, v in enumerate(distmat):
|
| 519 |
+
exclude_self = np.array([x for idx,x in enumerate(v) if idx!=i])
|
| 520 |
+
#if np.any(exclude_self > prune_dist_thr):
|
| 521 |
+
exclude_self = abs(exclude_self)
|
| 522 |
+
if min(exclude_self) > prune_dist_thr:
|
| 523 |
+
#print('del a pt w/ dist = ', min(exclude_self))
|
| 524 |
+
isolated.append(i)
|
| 525 |
+
break
|
| 526 |
+
|
| 527 |
+
return all_3d_vertices, connections_3d
|
| 528 |
+
|
| 529 |
+
|
| 530 |
+
def predict(entry, visualize=False, prune_dist_thr=600, depth_scale=2.5, ) -> Tuple[np.ndarray, List[int]]:
|
| 531 |
good_entry = convert_entry_to_human_readable(entry)
|
| 532 |
points3D = good_entry['points3d']
|
| 533 |
vert_edge_per_image = {}
|
|
|
|
| 569 |
|
| 570 |
vertices_3d = uv_to_v3d(uv, depth_vert, K, R, t)
|
| 571 |
|
| 572 |
+
'''
|
| 573 |
+
print('before del, ', len(vertices), len(vertices_3d), len(connections))
|
| 574 |
+
vert_to_del = np.array([-2807.275, -4999.645, 1284.803])
|
| 575 |
+
vertices, vertices_3d, connections = delete_one_vert(vertices, vertices_3d, connections, vert_to_del)
|
| 576 |
+
print('after del, ', len(vertices), len(vertices_3d), len(connections))
|
| 577 |
+
'''
|
| 578 |
+
|
| 579 |
vert_edge_per_image[i] = vertices, connections, vertices_3d
|
| 580 |
all_3d_vertices, connections_3d = merge_vertices_3d(vert_edge_per_image, 3.0) # TODO: 3cm looks too small
|
| 581 |
#print(f'after merge, {len(all_3d_vertices)} 3d vertices and {len(connections_3d)} 3d connections')
|
| 582 |
#all_3d_vertices_clean, connections_3d_clean = prune_not_connected(all_3d_vertices, connections_3d)
|
| 583 |
+
all_3d_vertices_clean, connections_3d_clean = prune_far(all_3d_vertices, connections_3d, prune_dist_thr=prune_dist_thr)
|
| 584 |
+
#all_3d_vertices_clean, connections_3d_clean = all_3d_vertices, connections_3d # don't prune -> cost:2.0
|
| 585 |
+
#print(f'after pruning, {len(all_3d_vertices_clean)} 3d clean vertices and {len(connections_3d_clean)} 3d clean connections')
|
| 586 |
if (len(all_3d_vertices_clean) < 2) or len(connections_3d_clean) < 1:
|
| 587 |
print (f'Not enough vertices or connections in the 3D vertices')
|
| 588 |
return (good_entry['__key__'], *empty_solution())
|