-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup.py
57 lines (38 loc) · 1.39 KB
/
setup.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
from settings import SHAPE_PREDICTOR_MODEL, FACE_RECOGNITION_MODEL, TRAIN_DATA_FOLDER, LABELS
import pickle
import dlib
import glob
import cv2
import os
def load_labels():
with open(LABELS, 'rb') as labels_data:
return pickle.load(labels_data)
def save_labels(labels):
with open(LABELS, 'wb') as labels_data:
pickle.dump(labels, labels_data)
def get_dlib_components():
detector = dlib.get_frontal_face_detector()
frm = dlib.face_recognition_model_v1(FACE_RECOGNITION_MODEL)
sp = dlib.shape_predictor(SHAPE_PREDICTOR_MODEL)
return detector, frm, sp
def init_labels():
detector, frm, sp = get_dlib_components()
labels = {}
for image_path in glob.glob(os.path.join(TRAIN_DATA_FOLDER, "*.jpg")):
label = image_path[len(TRAIN_DATA_FOLDER) + 1:len(image_path) - 4]
labels[label] = []
image = cv2.imread(image_path)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
dets = detector(gray)
if len(dets) != 1:
continue
for _, d in enumerate(dets):
shape = sp(gray, d)
vector = frm.compute_face_descriptor(image, shape)
labels[label].append(vector)
return labels
if __name__ == '__main__':
print('Gathering information about labels from %s' % TRAIN_DATA_FOLDER)
labels = init_labels()
save_labels(labels)
print('Done! Data saved to %s' % LABELS)