/
labeler.py
177 lines (147 loc) · 5.75 KB
/
labeler.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
from __future__ import print_function
import cv2
import os
import sys
import shutil
import time
from collections import defaultdict
import json
import utils
def run(directory):
tree_model = utils.TreeModel(utils.load_model("modelv2_testing.pkl"))
match_data = utils.read_match_data()
names, face_encodings = utils.get_names_faces_lists(match_data)
people_list = utils.get_people_list()
existing_people = set([x['name'] for x in people_list])
todo = []
for dir_name in os.listdir(directory):
fullpath = os.path.join(directory, dir_name)
if not os.path.isdir(fullpath) or dir_name == "__pycache__":
continue
filenames = os.listdir(fullpath)
if "name.txt" in filenames:
with open(os.path.join(directory, dir_name, "name.txt")) as f:
name = f.read().strip()
if name not in [p['name'] for p in people_list] and name != 'nevermatch':
print("Unknown label", name)
continue
todo.append(dir_name)
for i, dir_name in enumerate(sorted(todo)):
print("%s/%s" % (i+1, len(todo)), dir_name)
#for dir_name in os.listdir(directory):
fullpath = os.path.join(directory, dir_name)
filenames = os.listdir(fullpath)
image_count = sum([fname.endswith(".png") for fname in filenames])
no_face = 0
no_match = 0
representative_img = None
encodings = []
for i, fname in enumerate(filenames):
if not fname.endswith(".png"):
continue
img = cv2.imread(os.path.join(fullpath, fname), 1)
cv2.imshow('image', img)
cv2.waitKey(1)
encoding = utils.load_and_cache_encoding(fullpath, fname[:-4], jitters=10)
if encoding is None:
continue
else:
encodings.append(encoding)
representative_img = os.path.join(fullpath, fname)
continue
# Dead
img = cv2.imread(img_path, 1)
cv2.imshow('image', img)
cv2.waitKey(1)
bm = None
try:
bm = utils.get_best_match(img, face_encodings, names)
except utils.NoFaceException:
no_face += 1
except utils.NoMatchException:
no_match += 1
if no_face > (image_count/2)+1:
print('writing', 'nevermatch')
utils.write_name(directory, dir_name, 'nevermatch')
break
if no_match > (image_count/2)+1:
name = utils.prompt_person(people_list)
print('writing', name)
utils.write_name(directory, dir_name, name)
break
if bm is None:
continue
name = None
if utils.prompt_yn("Is this %s?" % bm):
name = bm
if name is None:
name = utils.prompt_person(people_list)
print('writing', name)
utils.write_name(directory, dir_name, name)
break
#else:
# We didn't take any action because we didn't hit any of the flags, this is a nevermatch
#print 'writing', 'nevermatch'
#utils.write_name(directory, dir_name, 'nevermatch')
if len(encodings) == 0:
print('writing', 'nevermatch')
utils.write_name(directory, dir_name, 'nevermatch')
continue
preds = tree_model.get_predictions(encodings, max_distance=0.4)
print(preds)
votes = defaultdict(int)
for p in preds:
if p is None:
continue
votes[p] += 1
img = cv2.imread(representative_img, 1)
cv2.imshow('image', img)
cv2.waitKey(50)
# for ambiguous directories, just say we'll never match.
print(votes)
#continue
#if len(votes) > 1:
#print 'writing', 'nevermatch'
#utils.write_name(directory, dir_name, 'nevermatch')
#continue
if len(votes) == 1:
if sum(votes.values()) >= 2:
final_name = votes.keys()[0]
else:
final_name = votes.keys()[0]
if not utils.prompt_yn("Is this %s?" % final_name):
final_name = utils.prompt_person(people_list)
print('writing', final_name)
utils.write_name(directory, dir_name, final_name)
elif len(votes) > 1:
answer = utils.prompt_list([x[0] for x in sorted(votes.items(), reverse=True, key=lambda x: x[1])])
if answer is None:
answer = utils.prompt_person(people_list)
print('writing', answer)
utils.write_name(directory, dir_name, answer)
else:
final_name = utils.prompt_person(people_list)
print('writing', final_name)
utils.write_name(directory, dir_name, final_name)
continue
total_votes = sum(votes.values())
# if not enough votes, prompt
if total_votes < 2:
if len(votes) > 0:
final_name = votes.keys()[0]
if not utils.prompt_yn("Is this %s?" % final_name):
final_name = utils.prompt_person(people_list)
else:
final_name = utils.prompt_person(people_list)
print('writing', final_name)
utils.write_name(directory, dir_name, final_name)
else:
final_name = votes.keys()[0]
print('writing', final_name)
utils.write_name(directory, dir_name, final_name)
utils.write_people_list(people_list)
if __name__ == "__main__":
if len(sys.argv) > 1:
run(sys.argv[1])
else:
print("directory needed")