-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathsimple_extractEmbeddings.py
107 lines (78 loc) · 3.25 KB
/
simple_extractEmbeddings.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
# import the necessary packages
from imutils import paths
import numpy as np
import imutils
import cv2
import os
import model as embedding
import torch
import pickle
# face detection model paths
protoPath = "./model_paths/deploy.prototxt.txt"
modelPath = "./model_paths/res10_300x300_ssd_iter_140000.caffemodel"
# loading detection model
detector = cv2.dnn.readNetFromCaffe(protoPath, modelPath)
# load embedding model
embedder = embedding.InceptionResnetV1(pretrained='vggface2').eval()
# paths to save pickle files
currentDir = os.getcwd()
# images folder
dataset = os.path.join(currentDir, "dataset")
# paths to save pickle files
embeddingPickle = os.path.join(currentDir, "output/SimpleEmbeddings.pickle")
# getting all images paths
imagePaths = list(paths.list_images(dataset))
# create lists to append ImgPaths/names/imageIDs/boxs/embeddings
ImgPaths = []
names = []
imageIDs = []
boxs = []
embeddings = []
# initlize the total number of faces processed
total = 0
# loop over the image paths
for (i, imagePath) in enumerate(imagePaths):
#extract the person name from the image path
name = imagePath.split(os.path.sep)[-2]
imageID = imagePath.split(os.path.sep)[-1].split('.')[-2]
image = cv2.imread(imagePath)
(h,w) = image.shape[:2]
blob = cv2.dnn.blobFromImage(cv2.resize(image, (300, 300)), 1.0, (300, 300), (104.0, 177.0, 123.0))
detector.setInput(blob)
detections = detector.forward()
if len(detections) > 0:
# we're making the assumption that each image has only ONE
# face, so find the bounding box with the largest probalility
i = np.argmax(detections[0, 0, :, 2])
confidence = detections[0, 0, i, 2]
# ensure that the detection with the largest probability also
# means our minimum probability test (thus helping filter out
# weak detections)
if confidence > 0.5:
box = detections[0, 0, i, 3:7] * np.array([w, h, w, h])
(startX, startY, endX, endY) = box.astype("int")
face = image[startY:endY , startX:endX]
(fH , fW) = face.shape[:2]
# ensure the facce width and height are sufficently large
if fW < 20 or fH < 20:
continue
try:
faceBlob = cv2.dnn.blobFromImage(face, 1.0 / 255,(160, 160), (0, 0, 0), swapRB=True, crop=False)
except:
print("[Error] - Face size in Image not sufficent to get Embeddings : ", imagePath)
continue
faceTensor = torch.tensor(faceBlob)
faceEmbed = embedder(faceTensor)
flattenEmbed = faceEmbed.squeeze(0).detach().numpy()
ImgPaths.append(imagePath)
imageIDs.append(imageID)
names.append(name)
boxs.append(box)
embeddings.append(flattenEmbed)
total += 1
# dump the facial embeddings + names to disk
print("[INFO] serializing {} encodings ....".format(total))
data = {"paths":ImgPaths, "names":names, "imageIDs":imageIDs, "boxs":boxs, "embeddings":embeddings}
f = open(embeddingPickle , "wb")
f.write(pickle.dumps(data))
f.close()