-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathgenerate_pickle.py
executable file
·51 lines (42 loc) · 1.38 KB
/
generate_pickle.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
#!/usr/bin/env python2
from util import *
from multiprocessing import Pool, cpu_count
from threading import local
def init():
global align, net
align = openface.AlignDlib(os.path.join(dlibModelDir, "shape_predictor_68_face_landmarks.dat"))
net = openface.TorchNeuralNet(os.path.join(openfaceModelDir, 'nn4.small2.v1.t7'), 96)
def loadImageFromFile(imgPath):
global align, net
uid = os.path.split(os.path.split(imgPath)[0])[-1]
bgrImg = cv2.imread(imgPath)
if bgrImg is None:
print("Unable to load image: {}".format(imgPath))
return
try:
rep = getRep(bgrImg, align, net)
except Exception as e:
print('{} for {}'.format(e, uid))
return
return (uid, rep)
PROCESSES = cpu_count() / 2 + 1
p = Pool(processes=PROCESSES, initializer=init)
g = glob.glob("/root/data/images/*/*")
start = time.time()
reps = p.imap_unordered(loadImageFromFile, g)
rep_dict = {}
count = 0
successes = 0
for r in reps:
count += 1
if count % 100 == 0:
print("{}s: {}/{} done".format(time.time() - start, count, len(g)))
if r:
successes += 1
if r[0] in rep_dict:
rep_dict[r[0]].append(r[1])
else:
rep_dict[r[0]] = [r[1]]
print("Loaded {}/{} refs, took {} seconds.".format(successes, len(g), time.time() - start))
with open("/root/data/data.pickle", 'wb') as f:
pickle.dump(rep_dict, f)