-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathinnotation_functions.py
More file actions
302 lines (223 loc) · 9.93 KB
/
innotation_functions.py
File metadata and controls
302 lines (223 loc) · 9.93 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
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
##### IMPORTS ############################
from jupyter_innotater import *
from cadastre_matching import *
#from scipy.ndimage import rotate
from skimage import io
import os
import imutils
import cv2
import matplotlib.pyplot as plt
##########################################
def initialize_match():
"""
Inquires the anchor and target labels
"""
input_anchor = input("Anchor cadaster label: ")
input_target = input("Target cadaster label: ")
return input_anchor, input_target
def innotater_init(a
, t
, path
, im_ext
):
selected_tifs = [path+a+im_ext
, path+t+im_ext
]
# Create permuted datasets
images = []
targets = np.zeros((len(selected_tifs), 1, 4), dtype='int')
for view in range(2):
images.append(selected_tifs[view:] + selected_tifs[:view])
image_ins = [ImageInnotation(images[i], None, name='Food '+str(i), width=400) for i in range(2)]
bb_ins = [BoundingBoxInnotation(targets[i], name='bbs '+str(i), source='Food '+str(i), desc='Food Type '+str(i)) for i in range(2)]
w = Innotater([GroupedInnotation(*image_ins[0:2])]
, indexes=[0]
, vertical=True
,targets=bb_ins)
return w, targets
def reset_click(uindex, repeat_index, **kwargs):
# uindex is the (underlying) index of the data sample where the button was clicked
# repeat_index will be the sub-index of the row in a RepeatInnotation, or -1 if at the top level
# kwargs will contain name and desc fields
if repeat_index == -1: # This was a top-level button (no sub-index within the RepeatInnotation)
# So reset everything
targets_type[uindex] = [1,0]
for i in range(repeats):
targets_bboxes[uindex, i, :] = 0
else:
# Only reset the row with repeat_index
targets_bboxes[uindex, repeat_index, :] = 0
return True # Tell Innotater the data at uindex was changed
def innotate_rectification(images
, flnms
, reorient = True
, rename = True
, rescale = False
, exclude_name="Exclude"
, ind_to_compute = np.array([])
):
if len(ind_to_compute)==0:
ind_to_compute = np.ones(len(images))
ind_to_compute = ind_to_compute == 1
#assert np.array([reorient, rename, rescale]).any()
repeats = 2
targets_exclude = np.zeros(len(images), dtype='int') # Binary flag to indicate want to exclude from dataset
targets = [targets_exclude]
displayed = [
TextInnotation(flnms, multiline=False), # Display the image filename
ImageInnotation(images, width=900, height=650)
]
interact = [BinaryClassInnotation(targets_exclude, name=exclude_name)]
if rename:
new_flnm = np.array(["" for _ in flnms], dtype='<U42') # update filename
interact += [TextInnotation(new_flnm, multiline=False)]
targets += [new_flnm]
if reorient:
orient_bboxes = np.zeros((len(images), repeats, 4), dtype='int') # (x,y,w,h)
interact += [RepeatInnotation(
(ButtonInnotation, None, {'desc': 'X', 'on_click': reset_click, 'layout': {'width': '40px'}}),
(BoundingBoxInnotation, orient_bboxes),
max_repeats=repeats, min_repeats=repeats
)]
targets += [orient_bboxes]
if rescale:
scale_bboxes = np.zeros((len(images), repeats, 4), dtype='int') # (x,y,w,h)
interact += [RepeatInnotation(
(ButtonInnotation, None, {'desc': 'X', 'on_click': reset_click, 'layout': {'width': '40px'}}),
(BoundingBoxInnotation, scale_bboxes),
max_repeats=repeats, min_repeats=repeats,
)]
targets += [scale_bboxes]
w = Innotater(
displayed, interact
, vertical=True, indexes = ind_to_compute
)
return w, targets
def perform_selection(targets
, images
, flnms
, rename = True
):
targets_exclude = targets[0]
mask_selected = np.array(targets_exclude==0)
selected_images = [im for im, select in zip(images, mask_selected) if select]
selected_targets = []
for targ in targets:
selected_targets += [targ[mask_selected]]
if rename and not (len(selected_targets[1]) == len(np.unique(selected_targets[1]))):
print("New files names are missing not unique, old ones will be used.")
selected_targets[1] = np.array(flnms)[mask_selected]
return selected_images, selected_targets
def orientation_angles(targets_orient):
"""
compute angle based on vector
"""
north_vectors = [np.array([tb[1][0]-tb[0][0], tb[1][1]-tb[0][1]]) for tb in targets_orient]
angles = [np.arctan(nv[1]/nv[0])*180/np.pi for nv in north_vectors]
rot_angles = [a+90 if nv[0]>=0 else a-90 for a, nv in zip(angles, north_vectors)]
return rot_angles
def rotate_images(images, orientation_angles):
"""
rotate a list of image according to given angles
"""
rotated_images = [imutils.rotate_bound(im, -alpha) for im, alpha in zip(images, orientation_angles)]
return rotated_images
def save_rectified_images(path
, rectified_images
, rectified_flnms
, new_filename_prefix
, img_ext
):
"""
save the rectified images
"""
assert len(rectified_images)==len(rectified_flnms)
if not os.path.exists(path):
os.makedirs(path)
for im, name in zip(rectified_images, rectified_flnms):
cv2.imwrite(os.path.join(path , new_filename_prefix+name+img_ext), im)
return
##### DEPRECATED ############################
def initialize_compositon(init_label
, path
, img_ext
, annot=True
):
init_im = io.imread(path+init_label+img_ext)#cv2.cvtColor(io.imread(path+init_label+img_ext), cv2.COLOR_BGR2GRAY)
h_init,w_init = init_im.shape[:2]
big_dict = {init_label: np.array([0,0])}
fig, ax = plt.subplots(figsize=(5,5))
if annot:
big_compo = cv2.putText(init_im.copy()
,text=init_label
,org=(w_init//2,h_init//2)
,fontFace=cv2.FONT_HERSHEY_PLAIN
,fontScale=40
,color=(255,0,0)
,thickness=80
)
big_compo = cv2.rectangle(big_compo, (0,0), (w_init, h_init), (255,0,0), 10)
else:
big_compo=init_im.copy()
plt_plot_cv2(big_compo)
return big_dict, big_compo
def test_match_grow(targets
, big_compo
, big_dict
, path
, anchor_label
, target_label
, img_ext
, path_compose
, annot=True
):
x_corner, y_corner, template_w, template_h = targets[0][0]
reduced_targ_x_corner, reduced_targ_y_corner, reduced_targ_w, reduced_targ_h = targets[1][0]
# Perform the matching
Lines_anchor = cv2.cvtColor(io.imread(path+anchor_label+img_ext), cv2.COLOR_BGR2GRAY)
Lines_target = cv2.cvtColor(io.imread(path+target_label+img_ext), cv2.COLOR_BGR2GRAY)
if reduced_targ_w < template_w or reduced_targ_h < template_h:
reduced_target=Lines_target
reduced_targ_x_corner, reduced_targ_y_corner = 0,0
else:
reduced_target = extract_single_template(Lines_target
, top_left_corner_coordinates=(reduced_targ_x_corner
, reduced_targ_y_corner)
, template_w=reduced_targ_w
, template_h=reduced_targ_h
)
template_provided = extract_single_template(Lines_anchor
, top_left_corner_coordinates=(x_corner, y_corner)
, template_w=template_w
, template_h=template_h
)
res = cv2.matchTemplate(reduced_target,template_provided,cv2.TM_CCOEFF)
min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(res)
homologous_target = max_loc[0]+reduced_targ_x_corner, max_loc[1]+reduced_targ_y_corner
single_compo, _ = image_composition(im1=Lines_anchor
, im2=Lines_target
, homologous_points=np.array([(x_corner, y_corner), homologous_target])
, label_pos = dict({})
, annot=False
, label='0'
)
plt.figure(figsize=(10,10))
plt_plot_cv2(single_compo)
plt.show()
# ask if satisfactory ?
print("Is it OK?")
ok = input()
if ok == "y":
img_target = io.imread(path_compose+target_label+img_ext)#, path_compose
big_compo, big_dict = image_composition(im1=big_compo
, im2=img_target
, homologous_points=np.array([np.array([x_corner, y_corner])+big_dict[anchor_label]
, np.array(homologous_target)])
, label_pos = big_dict
, annot=annot
, label=target_label
)
print("IMPLEMENT SAVING SCHEME")
else:
print("TO BE DISCARDED")
return big_compo, big_dict