-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMafQud_internal.py
140 lines (117 loc) · 4.54 KB
/
MafQud_internal.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
import os
import cv2
import numpy as np
from time import time
from mtcnn import MTCNN
from deepface.detectors.FaceDetector import alignment_procedure
from keras_facenet import FaceNet
class MafQud_internal:
def __init__(self):
self.detector = MTCNN()
self.facenet = FaceNet().model
def detect_align_face(self, path):
"""Detect face with 4 landmarks and align
Args:
path (str): path for image
Returns:
numpy.ndarray, list: image array, list of face landmarks and bounding box data
"""
t0 = time()
img = cv2.imread(path)
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
detections = self.detector.detect_faces(img)
if len(detections) == 0 or len(detections) > 1:
return None
# Crop and align the face
x, y, w, h = detections[0]["box"]
x1, y1 = int(x), int(y)
x2, y2 = int(x + w), int(y + h)
face = img[y1:y2, x1:x2]
keypoints = detections[0]["keypoints"]
face_align = alignment_procedure(
face, keypoints["left_eye"], keypoints["right_eye"]
)
# resize the face and convert to numpy array
face_align = cv2.resize(face_align, (160, 160))
face_array = np.asarray(face_align)
t1 = time() - t0
print(f"Face detected and aligned in {t1}")
return face_array
def load_faces(self, path):
"""Load faces in all images for a given person
Args:
path (str): path for the given person
Returns:
list, list : list of faces in all the person images, list of face landmarks and bounding box data
"""
t0 = time()
faces = list()
for filename in os.listdir(path):
dir_ = path + filename
face = self.detect_align_face(dir_)
if face is None:
print("No face found.")
continue
faces.append(face)
t1 = time() - t0
print(f"All person images, Face detectes and aligned in {t1}")
return faces
def load_dataset(self, path):
"""Load faces in all images for a all people
with their ids.
Args:
path (str): path for dataset
data_dir_name (str): file name for csv file
Returns:
numpy.ndarray, numpy.ndarray: array of all faces for a all people, id fo each face
"""
t0 = time()
X, y = list(), list()
for subdir in os.listdir(path):
dir_ = path + subdir + "/"
if not os.path.isdir(dir_):
print("Directory doesn't exist")
continue
faces = self.load_faces(dir_)
labels = [subdir for _ in range(len(faces))]
print(">loaded %d examples for class: %s" % (len(faces), subdir))
X.extend(faces)
y.extend(labels)
t1 = time() - t0
print(f"All people images, Face detected and aligned in {t1}")
return np.asarray(X, dtype=np.float32), np.asarray(y, dtype=np.int)
def feature_encoding(self, img):
"""Extract 128 numerical feature from face array
Args:
model (tensorflow.python.keras.engine.functional.Functional): acenet keras model
img (numpy.ndarray): face array
Returns:
numpy.ndarray: array of 128 numerica feature
"""
t0 = time()
img = img.astype("float32")
mean, std = img.mean(), img.std()
img = (img - mean) / std
samples = np.expand_dims(img, axis=0)
yhat = self.facenet.predict(samples)
t1 = time() - t0
print(f"Face encoding extracted in {t1}")
return yhat[0]
def get_encodings(self, X):
"""Extract 128 numerical feature all face arrays for each person
Args:
model (tensorflow.python.keras.engine.functional.Functional): acenet keras model
X (numpy.ndarray): array of all faces for each person
Returns:
numpy.ndarray: array of encoding for each face
"""
t0 = time()
newX = list()
print("Encoding started")
for img in X:
embedding = self.feature_encoding(img)
newX.append(embedding)
print("Extraxting features for face No. %d" % (len(newX)))
t1 = time() - t0
print(f"dataset face encodings extracted in {t1}")
return np.asarray(newX)