-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpuzzle.py
More file actions
165 lines (142 loc) · 6.42 KB
/
Copy pathpuzzle.py
File metadata and controls
165 lines (142 loc) · 6.42 KB
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
import matplotlib.pyplot as plt
import numpy as np
import skimage
from facet import Facet, compatibility_func
from piece import Piece, pieces_from_masks
from puzzle_piece_detector.inference_callable import Inference
from utils import image_in_scale, masks_in_scale
def evaluate_edge_compatibility(pieces, comparison_method):
n_side_pieces = n_middle_pieces = n_corner_pieces = 0
for piece in pieces:
if piece.type is Piece.Type.SIDE:
n_side_pieces = n_side_pieces + 1
elif piece.type is Piece.Type.MIDDLE:
n_middle_pieces = n_middle_pieces + 1
elif piece.type is Piece.Type.CORNER:
n_corner_pieces = n_corner_pieces + 1
else:
raise
n_pieces = len(pieces)
n_facets = 4
arr = np.zeros((n_pieces, n_pieces, n_facets, n_facets))
for p1idx, p1 in enumerate(pieces):
for p2idx, p2 in enumerate(pieces):
if p1idx < p2idx:
for f1idx, f1 in enumerate(p1.facets):
if f1.type is Facet.Type.FLAT:
continue
for f2idx, f2 in enumerate(p2.facets):
if f2.type is Facet.Type.FLAT:
continue
arr[p1idx, p2idx, f1idx, f2idx] = comparison_method(f1, f2)
return arr, n_facets, n_pieces, n_side_pieces, n_middle_pieces
def segment_to_masks_and_extract_pieces(weights_path, image_path, segmenting_method):
if segmenting_method[0]:
inference = Inference(weights_path)
masks = segmenting_method[1](inference, image_path)
else:
masks = segmenting_method[1](image_path)
image = skimage.io.imread(image_path)
scale = 1
masks = masks_in_scale(masks, scale)
image = image_in_scale(image, scale)
pieces = pieces_from_masks(masks, image)
return pieces, masks
def print_pieces(pieces, name):
fig = plt.figure()
for i in range(len(pieces)):
factor = int(np.ceil(np.sqrt(len(pieces))))
ax = fig.add_subplot(factor, factor, i + 1)
plt.imshow(pieces[i].cropped_image)
ax.set_title(f'{i}')
ax.axis('off')
plt.savefig(f'plots/{name}.png')
plt.close(fig)
def print_facets(pieces, name):
fig = plt.figure()
for i in range(len(pieces)):
factor = int(np.ceil(np.sqrt(len(pieces))))
ax = fig.add_subplot(factor, factor, i + 1)
img = np.zeros_like(pieces[i].cropped_image)
facet_colors = np.array([[255, 0, 0], [0, 255, 0], [0, 0, 255], [255, 255, 255]]).astype(np.uint8)
for fi in range(len(pieces[i].facets)):
mask = pieces[i].facets[fi].facet_mask
img[mask, :] = facet_colors[fi, :]
plt.imshow(img)
ax.set_title(f'{i}')
ax.axis('off')
plt.savefig(f'plots/{name}.png')
plt.close(fig)
def print_figures_with_weights_to_folder(edges, arr, pieces, output_dir):
for edge in edges:
p1, p2, f1, f2 = edge[0], edge[1], edge[2], edge[3]
fig = plt.figure()
ax = fig.add_subplot(2, 2, 1)
plt.imshow(pieces[p1].cropped_image)
ax.set_title(f'{p1}')
ax = fig.add_subplot(2, 2, 2)
plt.imshow(pieces[p2].cropped_image)
ax.set_title(f'{p2}')
ax = fig.add_subplot(2, 2, 3)
plt.imshow(pieces[p1].facets[f1].facet_mask)
ax.set_title(f'{f1}')
ax = fig.add_subplot(2, 2, 4)
plt.imshow(pieces[p2].facets[f2].facet_mask)
ax.set_title(f'{f2}')
plt.savefig(f'plots/{output_dir}/{format(arr[p1, p2, f1, f2], ".3f")}_{p1}_{p2}_{f1}_{f2}.png')
plt.close()
def sort_and_filter(n_pieces, n_facets, filter_val, weights, descending=True):
sort_idx = np.argsort(weights, axis=None)
if descending:
sort_idx = np.flip(sort_idx) # descending order using flat
mask = weights.flat > filter_val # filter mask,
filtered_idx = sort_idx[mask[sort_idx]] # cutting according to filter
# get indices
edges = np.transpose(np.vstack(np.unravel_index(filtered_idx, (n_pieces, n_pieces, n_facets, n_facets))))
return edges
def paint_facets_by_type(masks, pieces):
width, height, _ = masks.shape
masks_with_facets = np.ones((width, height, 3), dtype=np.uint8) * 255
for piece in pieces:
img = np.ones_like(piece.cropped_image) * 255
facet_colors = dict()
facet_colors[Facet.Type.FLAT] = np.array([0, 0, 0], dtype=np.uint8)
facet_colors[Facet.Type.TAB] = np.array([0, 255, 0], dtype=np.uint8)
facet_colors[Facet.Type.BLANK] = np.array([255, 0, 0], dtype=np.uint8)
for fi in range(len(piece.facets)):
facet = piece.facets[fi]
mask = facet.facet_mask_custom_contour_size(3)
img[mask, :] = facet_colors[facet.type]
height, width, _ = piece.cropped_image.shape
left, top = piece.left, piece.top
left_width, top_height = left + width, top + height
masks_with_facets[top:top_height, left:left_width] = img
return masks_with_facets
def paint_facets_distinct(masks, pieces):
width, height, _ = masks.shape
masks_with_facets = np.ones((width, height, 3), dtype=np.uint8) * 255
for piece in pieces:
img = np.ones_like(piece.cropped_image) * 255
facet_colors = np.array([[255, 0, 0], [0, 255, 0], [0, 0, 255], [0, 0, 0]]).astype(np.uint8)
for fi in range(len(piece.facets)):
mask = piece.facets[fi].facet_mask_custom_contour_size(3)
img[mask, :] = facet_colors[fi, :]
height, width, _ = piece.cropped_image.shape
left, top = piece.left, piece.top
left_width, top_height = left + width, top + height
masks_with_facets[top:top_height, left:left_width] = img
return masks_with_facets
def calc_cmp_from_iou_and_mgc(iou, mgc, n_facets, n_pieces, pieces):
cmp = np.zeros((n_pieces, n_pieces, n_facets, n_facets))
for p1idx, p1 in enumerate(pieces):
for p2idx, p2 in enumerate(pieces):
if p1idx < p2idx:
for f1idx, f1 in enumerate(p1.facets):
if f1.type is Facet.Type.FLAT:
continue
for f2idx, f2 in enumerate(p2.facets):
if f2.type is Facet.Type.FLAT:
continue
cmp[p1idx, p2idx, f1idx, f2idx] = compatibility_func()(mgc[p1idx, p2idx, f1idx, f2idx],
iou[p1idx, p2idx, f1idx, f2idx])
return cmp