-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerateFaceEmbedded.py
67 lines (61 loc) · 2.04 KB
/
generateFaceEmbedded.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
from openface.openface_model import create_model
from openface.preprocess_face_data import load_metadata
from openface.align import AlignDlib
import numpy as np
import cv2
import config
import os
from datetime import datetime
# using pre-trained model
print('load_model')
nn4_small2_pretrained = create_model()
nn4_small2_pretrained.load_weights('models/nn4.small2.v1.h5')
# nn4_small2_pretrained.summary()
start = datetime.now()
# load customDataset
print(config.faceImagesPath)
metadata = load_metadata('faceImages', num=2)
print(metadata)
def load_image(path):
img = cv2.imread(path, 1)
# OpenCV loads images with color channels
# in BGR order. So we need to reverse them
return img[..., ::-1]
# Initialize the OpenFace face alignment utility
aligment = AlignDlib('models/landmarks.dat')
# Align image on face
def align_image(img):
return aligment.align(96, img, aligment.getLargestFaceBoundingBox(img),
landmarkIndices=AlignDlib.OUTER_EYES_AND_NOSE)
# Embedding vectors
good_image_index = []
unfit_image_index = []
embedded = np.zeros((metadata.shape[0], 128))
print('preprocess image')
for i, m in enumerate(metadata):
img = load_image(m.image_path())
img = align_image(img)
try:
# scale RGB values to interval [0,1]
img = (img / 255.).astype(np.float32)
except TypeError:
unfit_image_index.append(i)
print("The image is not Clear to extract the Embeddings")
else:
# obtain embedding vector for image
embedded[i] = nn4_small2_pretrained.predict(np.expand_dims(img, axis=0))[0]
good_image_index.append(i)
stop = datetime.now()
print(stop - start)
metadata = metadata[good_image_index]
print(metadata)
embedded = embedded[good_image_index]
print(embedded)
print('face embedded create complete')
print('save metadata and embedded')
if not os.path.exists(config.faceData):
os.makedirs(config.faceData, exist_ok='True')
# save metadata
np.save(config.faceData+'/metadata.npy', metadata)
# save embedded
np.save(config.faceData+'/embedded.npy', embedded)