-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlabel_dictionary.py
450 lines (379 loc) · 21 KB
/
label_dictionary.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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
import json
import os
import pandas as pd
from pathlib import Path
import math
from model import draw_box,load_image,face_location_encoding,knn_modelling,face_prediction,add_vector_location_img
from utils import special_layout,dict_5row_layout,col_layout,number_to_0000,write_table,create_annotated_dir,change_image_name
import matplotlib.pyplot as plt
import re
import matplotlib.pyplot as plt
from matplotlib.widgets import RectangleSelector
from matplotlib.widgets import Cursor, Button
import numpy as np
from sklearn.neighbors import KNeighborsClassifier
class Label_Dictionary():
def __init__(self,classname,save_individual_annotated=False):
global json_path
global df_path
global img_path
img_dir_path = f'./data/{classname}/image/individual'
json_path = f'./data/{classname}/{classname}.json'
df_path = f'./data/{classname}/{classname}.csv'
self.classname = classname
# read dictionary if exist
if Path(json_path).is_file():
with open(json_path,'r') as doc:
label_dict = json.load(doc)
# reminder
print(special_layout(f'Loaded label dictionary: {json_path}\n***You can type {self.classname}.dict_ to access dictionary'))
print(dict_5row_layout(label_dict,'name',blank = 15,each_row =5))
self.dict_ = label_dict
# make dictionary if not exist
else:
create_annotated_dir(self.classname)
try:
# 1. name
print(special_layout(f'1. Insert name on {classname}.json'))
dict_list = [ {'name': name, 'vector(individual)':[],'location(individual)':[],'img(individual)':[],'resize':[],
'vector(class)':[],'location(class)':[],'img(class)':[]} for name in pd.read_csv(df_path).columns[1:] ]
label_dict = dict(zip(range(len(dict_list)),dict_list))
print(dict_5row_layout(label_dict,'name',blank = 15,each_row =5))
# 2. img
print(special_layout(f'2. Insert img(individual) path on {classname}.json'))
for img_path in sorted(Path(img_dir_path).glob("*.jpg")):
label_num = int(img_path.name[:4])
label_dict[label_num]['img(individual)'].append(str(img_path))
print(f'Image ({img_path.name}) append to {dict_list[label_num]["name"]}')
# 3. location,vector
print(special_layout(f'Insert face location and vector (individual) on {classname}.json\n***face location: (row1,col2,row2,col1)'))
for each_dict in label_dict.values():
print(f'\nProcress image of {each_dict["name"]}:\n')
img_path_list = each_dict['img(individual)']
for img_path in img_path_list:
# load image
array = load_image(img_path)
# detect face and ecode
print(f'Detecting and Encoding face on image({img_path[-12:]})...')
location_list,vector_list = face_location_encoding(array)
print(f'{len(location_list)} Location append to {each_dict["name"]}')
print(f'{len(vector_list)} Vector append to {each_dict["name"]}')
vector_list = list(map(lambda array: list(array),vector_list))
if save_individual_annotated:
annotated_img = draw_box(array, location_list,show = False)
plt.imsave(img_path.replace('individual','annotated'),annotated_img)
# warning
if len(location_list)!=1:
words = f'Please check image({img_path[:-13]}) on {each_dict["name"]}!\n***amount of face on image (individual) is not one ({len(vector_list)})'
print(special_layout(special_layout(words)))
# location
each_dict['location(individual)']+=location_list
# vector
each_dict['vector(individual)']+=vector_list
# add img path on extra face
if len(location_list)>1:
for i in range(len(location_list)-1):
each_dict['img(individual)'].append(img_path)
i+=1
# Final: output to dir
with open(json_path,'w') as doc:
doc.write(json.dumps(label_dict))
# reminder
print(special_layout(f'Created label dictionary: {json_path}'))
with open(json_path,'r') as doc:
label_dict = json.load(doc)
self.dict_ = label_dict
print('\nName: \n')
print(dict_5row_layout(label_dict,'name',blank = 15,each_row =5))
print('\nLocation: \n')
print(dict_5row_layout(label_dict,'location(individual)',blank = 15,each_row =5,count_value = True))
except:
print(special_layout(f'Please creates attendence table: {df_path}\n\nPlease creates directory: {f"./{classname}"}'))
def face_visualize(self, number = None, img = None, save = False, show = True):
# create save dir
if save:
create_annotated_dir(self.classname)
def print_face(i,img_path_list,location_list,save = False):
array = load_image(img_path_list[i])
annotated_array = draw_box(array, [location_list[i]],show = True)
if save:
plt.imsave(img_path.replace('individual','annotated'),annotated_array)
'''
number,img = None -> input(class or indivdiual)
number != None -> all face of the individual
'''
# show
# number != None
if number != None:
target_dict = self.dict_[str(number)]
print(special_layout(f'Visualizing individual face on {target_dict["name"]}'))
# all img
for i in range(len(target_dict['img(individual)'])):
print_face(i,target_dict['img(individual)'],target_dict['location(individual)'],save)
# img == None
# specific face on one indiviual
elif img != None:
# indiviual
if re.match(r'\d{4}_',img[0:4]):
target_dict = self.dict_[str(int(img[0:4]))]
i = list(map(lambda path: bool(re.search(img,path)),target_dict['img(individual)'])).index(True)
print(special_layout(f'Visualizing individual face on {target_dict["name"]}'))
print_face(i,target_dict['img(individual)'],target_dict['location(individual)'],save)
if re.match(r'\d{4}-',img[0:4]):
class_face_location_dict = {}
for label,each_dict in self.dict_.items():
i = list(map(lambda path: bool(re.search(img,path)),each_dict['location(class)'])).index(True)
class_face_location_dict[label] = self.dict_[str(label)]['location(class)'][i]
array = load_image("./data/FTDS5/image/class/"+img)
draw_box(array, list(class_face_location_dict.values()), show = True
,label_test = list(class_face_location_dict.keys()), Dict = self.dict_)
else:
print(special_layout("Enter number or img"))
return None
def modelling(self,n_neighbors= 1):
vector_train=[]
label_train=[]
vector_individual_train = []
label_individual_train = []
print(special_layout(f"Vector amount summary:"))
print(col_layout('Label','Vector(individual) amount','Vector(class) amount','Total'))
for label,each_dict in self.dict_.items():
total = len(each_dict['vector(individual)'])+len(each_dict['vector(class)'])
print(col_layout(str(label)+'.'+each_dict['name'],len(each_dict['vector(individual)']),len(each_dict['vector(class)']),total))
vector_train = vector_train + each_dict['vector(individual)']+each_dict['vector(class)']
vector_individual_train += each_dict['vector(individual)']
label_train += [int(label) for i in range(total)]
label_individual_train += [int(label)]
knn_modelling(self.classname,vector_individual_train,label_individual_train,n_neighbors=1,only_individual = True)
knn_modelling(self.classname,vector_train,label_train,n_neighbors=n_neighbors)
def tick_attendence(self,img_name = None,save_annotated = True,add_vector = True,n_neighbors = 1):
global Label_test
global location_list
global vector_list
global mode
def line_select_callback(click,release):
global Label_test
global location_list
global target
row1,col2,row2,col1 = int(click.ydata),int(release.xdata),int(release.ydata),int(click.xdata)
Label_test.append(int(target))
location_list.append((row1,col2,row2,col1))
print(special_layout(f"Added {self.dict_[str(target)]['name']} to annotated image.\n\
Amount of targets: {len(location_list)}"))
plt.close()
def onclick(event):
global Label_test
global location_list
global vector_list
global mode
col, row = event.xdata, event.ydata
for i in range(len(location_list)):
row1,col2,row2,col1 = location_list[i]
if row > row1 and row < row2 and col > col1 and col < col2:
if mode == '2':
try:
correction = input(special_layout(f"You select {self.dict_[str(Label_test[i])]['name']} ({Label_test[i]})\n***correction -> 1 No correction -> 0"))
if int(correction):
correct_label = input(special_layout(f"Who is this? :\n\n\
{dict_5row_layout(self.dict_,'name',blank = 15,each_row =5,count_value = False)}\n***Please type number"))
print(special_layout(f"{self.dict_[str(Label_test[i])]['name']} -> {self.dict_[str(correct_label)]['name']}"))
Label_test[i] = correct_label
except:
pass
break
elif mode == '3':
try:
delete = input(special_layout(f"You confirm to delete {self.dict_[str(Label_test[i])]['name']} ({Label_test[i]}) on [{row1}:{row2},{col1}:{col2}]?\n***yes -> 1 No no -> 0"))
except:
pass
break
try:
if int(delete):
Label_test.pop(i)
location_list.pop(i)
vector_list.pop(i)
except:
pass
plt.close()
def toggle_selector(event):
toggle_selector.RS.set_active(True)
def object_mode_change(event):
global target
global mode
if event.key == '1':
mode = '1'
print(special_layout(f" Add label on annotated image."))
try:
target = input(special_layout(f"Select target label:\n\n\
{dict_5row_layout(self.dict_,'name',blank = 15,each_row =5,count_value = False)}"))
print(f"You will annotate {self.dict_[str(target)]['name']} ({target})")
plt.close()
except:
pass
elif event.key == '2':
mode = '2'
print(special_layout(f" Change label on annotated image."))
plt.close()
elif event.key == '3':
mode = '3'
print(special_layout(f" Delete label on annotated image."))
plt.close()
elif event.key == 'q':
mode = 'q'
print(special_layout(f"Finish correction..."))
plt.close()
else:
print(special_layout(f"Please press the followings key:\n\nAdd annotation -> 1\nClick show label and change label -> 2\nDelete annotation -> 3\n\
Exit correction -> q"))
# change name
if img_name == None:
try:
img_name = change_image_name(self.classname)
except:
img_name = [path for path in sorted(Path(f"./data/{self.classname}/image/class").glob("*.jpg"))][-1].name
img_path = f'./data/{self.classname}/image/class/{img_name}'
print(special_layout(f"Detect and encode faces on image({img_name})"))
array = load_image(img_path)
location_list, vector_list = face_location_encoding(array)
Label_test = list(face_prediction(self.classname, vector_list))
annotated = draw_box(load_image(img_path), location_list,False,Label_test ,self.dict_)
count = 0
while True:
print(special_layout(f"Show you the annotated image...\nEnter q"))
fig, ax = plt.subplots(1)
plt.imshow(annotated)
plt.show()
if count%2==0:
indivdual = input(special_layout(f"Try individual model?\n1:yes 0:no"))
if int(indivdual):
Label_test = list(face_prediction(self.classname, vector_list,only_individual=True))
annotated = draw_box(load_image(img_path), location_list,False,Label_test ,self.dict_)
else:
break
else:
back = input(special_layout(f"Try the previous model?\n1:yes 0:no"))
if int(back):
Label_test = list(face_prediction(self.classname, vector_list))
annotated = draw_box(load_image(img_path), location_list,False,Label_test ,self.dict_)
plt.close()
else:
print(123)
break
count += 1
print(special_layout(f"Show you the annotated image...\nPress H to watch instruction:)"))
mode = '2'
while True:
annotated = draw_box(load_image(img_path), location_list,False,Label_test ,self.dict_)
fig, ax = plt.subplots(1)
plt.imshow(annotated)
if mode == '1':
toggle_selector.RS = RectangleSelector(
ax,line_select_callback,
drawtype='box',useblit=True,
button=[1],minspanx=5,minspany=5,
spancoords='pixels',interactive=True
)
plt.connect('key_press_event', toggle_selector)
plt.connect('key_press_event',object_mode_change)
elif mode == '2' or mode == '3':
Cursor(ax,
horizOn=False, # Controls the visibility of the horizontal line
vertOn=False, # Controls the visibility of the vertical line
)
fig.canvas.mpl_connect('button_press_event', onclick)
plt.connect('key_press_event',object_mode_change)
plt.show()
if mode == 'q':
break
if save_annotated:
create_annotated_dir(self.classname)
plt.imsave(img_path.replace('class','annotated'),annotated)
# write table
write_table(self.dict_,self.classname,Label_test,img_name)
# add vector # Modelling
if add_vector:
vector_correct = input(special_layout(f"Add all face into our knn model?\n***yes -> 1 no -> 0"))
if int(vector_correct)-1:
mode = '2'
while True:
annotated = draw_box(load_image(img_path), location_list,False,Label_test ,self.dict_)
fig, ax = plt.subplots(1)
plt.imshow(annotated)
if mode == '1':
toggle_selector.RS = RectangleSelector(
ax,line_select_callback,
drawtype='box',useblit=True,
button=[1],minspanx=5,minspany=5,
spancoords='pixels',interactive=True
)
plt.connect('key_press_event', toggle_selector)
plt.connect('key_press_event',object_mode_change)
elif mode == '2' or mode == '3':
Cursor(ax,
horizOn=False, # Controls the visibility of the horizontal line
vertOn=False, # Controls the visibility of the vertical line
)
fig.canvas.mpl_connect('button_press_event', onclick)
plt.connect('key_press_event',object_mode_change)
plt.show()
if mode == 'q':
break
add_vector_location_img(self.dict_,self.classname,vector_list,Label_test,location_list,img_name)
vector_train=[]
label_train=[]
print(special_layout(f"Vector amount summary:"))
print(col_layout('Label','Vector(individual) amount','Vector(class) amount','Total'))
for label,each_dict in self.dict_.items():
total = len(each_dict['vector(individual)'])+len(each_dict['vector(class)'])
print(col_layout(str(label)+'.'+each_dict['name'],len(each_dict['vector(individual)']),len(each_dict['vector(class)']),total))
vector_train = vector_train + each_dict['vector(individual)']+each_dict['vector(class)']
label_train += [int(label) for i in range(total)]
knn_modelling(self.classname,vector_train,label_train,n_neighbors =n_neighbors)
# Final: output to dir
with open(json_path,'w') as doc:
doc.write(json.dumps(self.dict_))
# reminder
print(special_layout(f'Renew label dictionary: {json_path}'))
def print_all_info(self,key,blank = 15,each_row =5, count_value = False):
if count_value:
word = ' count'
else:
word = ''
print( f'Printing {key + word} of {self.classname}\n' )
print(dict_5row_layout(self.dict_,key,blank = 15,each_row =5,count_value = count_value))
def update(self,number,mode = 'individual'):
if mode == 'individual':
print(special_layout(f"Updating {self.dict_[str(number)]['name']} individual vector, location and img..."))
self.dict_[str(number)]['img(individual)'] = []
self.dict_[str(number)]['vector(individual)'] = []
self.dict_[str(number)]['location(individual)'] = []
for img_path in Path('./data/FTDS5/individual').glob(f'{number_to_0000(number)}*.jpg'):
self.dict_[str(number)]['img(individual)'].append(str(img_path))
for img_path in self.dict_[str(number)]['img(individual)']:
# load image
array = load_image(img_path)
# detect face and ecode
print(f'Detecting and Encoding face on image({img_path[:-13]})...')
location_list,vector_list = face_location_encoding(array)
print(f'{len(location_list)} Location append to {self.dict_[str(number)]["name"]}')
print(f'{len(vector_list)} Vector append to {self.dict_[str(number)]["name"]}')
vector_list = list(map(lambda array: list(array),vector_list))
# warning
if len(location_list)!=1:
words = f'Please check image({img_path[:-13]}) on {self.dict_[str(number)]["name"]}!\n***amount of face on image (individual) is not one ({len(vector_list)})'
print(special_layout(special_layout(words)))
# location
self.dict_[str(number)]['location(individual)']+=location_list
# vector
self.dict_[str(number)]['vector(individual)']+=vector_list
# add img path on extra face
if len(location_list)>1:
for i in range(len(location_list)-1):
self.dict_[str(number)]['img(individual)'].append(img_path)
i+=1
print(special_layout(f'Updated {self.classname} vector, location and img (individual)...'))
if __name__ == '__main__':
Avengers = Label_Dictionary('Avengers')
Avengers.modelling(n_neighbors=1)
for path in sorted(Path("/Users/15077693d/Desktop/FTDS/GitHub/Attendancv/data/Avengers/image/class").glob('*.jpg')):
Avengers.tick_attendence(img_name=path.name)