-
Notifications
You must be signed in to change notification settings - Fork 3
/
extract_embeddings.py
96 lines (76 loc) · 2.62 KB
/
extract_embeddings.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
import cv2
import os
import numpy as np
from tensorflow.keras.models import load_model
import pickle
rootdir = os.getcwd()
class Extract_Embeddings():
def __init__(self,model_path):
self.model_path = model_path
self.dataset_dir = os.path.join(rootdir,'dataset')
def load_model(self):
model = load_model(self.model_path)
return model
def check_pretrained_file(self,embeddings_model):
self.embeddings_model = embeddings_model
data = pickle.loads(open(embeddings_model, "rb").read())
names = np.array(data["names"])
unique_names = np.unique(names).tolist()
return [data,unique_names]
def get_staff_details(self):
details = os.listdir(self.dataset_dir)
staff_details = {}
for item in details:
name = item.split("_")[0]
id = item.split("_")[1]
staff_details[name] = id
return staff_details
def get_remaining_names(self,dictionaries,unique_names):
self.dictionaries = dictionaries
self.unique_names = unique_names
remaining_names = np.setdiff1d(list(dictionaries.keys()),unique_names).tolist()
return remaining_names
def get_all_face_pixels(self,dictionaries):
image_ids = []
image_paths = []
image_arrays = []
names = []
face_ids = []
for category in list(dictionaries.keys()):
path = os.path.join(self.dataset_dir,category + "_" + dictionaries[category])
for img in os.listdir(path):
img_array = cv2.imread(os.path.join(path,img))
image_paths.append(os.path.join(path,img))
image_ids.append(img)
image_arrays.append(img_array)
names.append(category)
face_ids.append(dictionaries[category])
return [image_ids,image_paths,image_arrays,names,face_ids]
def get_remaining_face_pixels(self,dictionaries,remaining_names):
self.dictionaries = dictionaries
self.remaining_names = remaining_names
image_ids = []
image_paths = []
image_arrays = []
names = []
face_ids = []
if len(remaining_names) != 0:
for category in list(remaining_names):
path = os.path.join(self.dataset_dir,category + "_" + dictionaries[category])
for img in os.listdir(path):
img_array = cv2.imread(os.path.join(path,img))
image_paths.append(os.path.join(path,img))
image_ids.append(img)
image_arrays.append(img_array)
names.append(category)
face_ids.append(dictionaries[category])
return [image_ids,image_paths,image_arrays,names,face_ids]
else:
return None
def normalize_pixels(self,imagearrays):
self.imagearrays = imagearrays
face_pixels = np.array(self.imagearrays)
face_pixels = face_pixels.astype('float32')
mean, std = face_pixels.mean(), face_pixels.std()
face_pixels = (face_pixels - mean) / std
return face_pixels