-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhalo.py
161 lines (124 loc) · 6 KB
/
halo.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
from model import load_FbDeepFace
import os
import pickle
from tqdm import tqdm
import helper
import pandas as pd
import numpy as np
class FaceRecognition:
def __init__(self, database="./database"):
self.model = load_FbDeepFace()
self.database = database
# Check if the representations of employee faces is exist or not
if os.path.isdir(self.database) == True:
file_name = "representations.pkl"
file_name = file_name.replace("-", "_").lower()
isTrainAgain = False
if os.path.exists(os.path.join(self.database, file_name)):
f = open(os.path.join(self.database, file_name), "rb")
try:
representations = pickle.load(f)
except EOFError:
print("representations.pkl seems empty")
_, counts = self.__count_files(self.database)
# If representations.pkl exist but there are new employees or resign employees
if len(representations) != counts:
print("In database: {}".format(counts))
print("In representations.pkl: {}".format(len(representations)))
print("Found new employees or one of them have resign")
print("Begin analyzing")
isTrainAgain = True
else:
self.representations = representations
print(
"There are {} of {} faces found in the database".format(
len(self.representations), counts
)
)
# Find the employees face representation as vector
if (
isTrainAgain
or os.path.exists(os.path.join(self.database, file_name)) == False
):
employees, _ = self.__count_files(self.database)
if len(employees) == 0:
raise ValueError("There is no image in ", self.database, " folder!")
# ------------------------
# find representations for db images
representations = []
pbar = tqdm(range(0, len(employees)))
# for employee in employees:
for index in pbar:
employee = employees[index]
pbar.set_description("Finding embedding for {}".format(employee))
shape = self.model.layers[0].input_shape
input_shape = shape[0][1:3] if type(shape) is list else shape[1:3]
input_shape_x = input_shape[0]
input_shape_y = input_shape[1]
img = helper.detectFace(
employee, (input_shape_y, input_shape_x), enforce_detection=True
)
representation = self.model.predict(img)[0, :]
instance = []
instance.append(employee)
instance.append(representation)
# -------------------------------
representations.append(instance)
f = open(self.database + "/" + file_name, "wb")
pickle.dump(representations, f)
f.close()
self.representations = representations
print("Representations stored in ", self.database, "/", file_name)
else:
raise ValueError("database not a directory")
def predict(self, img):
if self.representations == None or len(self.representations) == 0:
raise AttributeError("Representations file not loaded correctly")
df = pd.DataFrame(self.representations, columns=["identity", "representation"])
target_representation = self.model.predict(img)[0, :]
distances = []
for index, col in df.iterrows():
source_representation = col["representation"]
# distance = self.__euclideanDistance(self.__l2_normalize(source_representation), self.__l2_normalize(target_representation))
# distance = self.__cosineDistance(source_representation, target_representation)
distance = self.__euclideanDistance(
source_representation, target_representation
)
distances.append(distance)
# threshold = helper.findThreshold('DeepFace', 'euclidean_l2')
# threshold = helper.findThreshold('DeepFace', 'cosine')
threshold = helper.findThreshold("DeepFace", "euclidean")
df["distances"] = distances
df = df.drop(columns=["representation"])
df = df[df.distances <= threshold]
df = df.sort_values(by=["distances"], ascending=True).reset_index(drop=True)
print(df)
if df.empty:
return "", ""
person = df.iloc[0]["identity"]
confidence = df.iloc[0]["distances"]
folder, sep, name_imageName = person[2::].partition("/")
name, sep, imageName = name_imageName.partition("/")
return name.capitalize(), confidence
def __cosineDistance(self, origin, test):
a = np.matmul(np.transpose(origin), test)
b = np.sum(np.multiply(origin, origin))
c = np.sum(np.multiply(test, test))
return 1 - (a / (np.sqrt(b) * np.sqrt(c)))
def __count_files(self, dir_path):
count = 0
items = []
for root, directory, files in os.walk(dir_path):
for f in files:
if ".jpg" in f:
count += 1
exact_path = root + "/" + f
items.append(exact_path)
return (items, count)
def __l2_normalize(self, x):
return x / np.sqrt(np.sum(np.multiply(x, x)))
def __euclideanDistance(self, source_representation, test_representation):
euclidean_distance = source_representation - test_representation
euclidean_distance = np.sum(np.multiply(euclidean_distance, euclidean_distance))
euclidean_distance = np.sqrt(euclidean_distance)
return euclidean_distance