2021-01-17 06:04:24 +00:00
|
|
|
import os
|
|
|
|
import dlib
|
|
|
|
import cv2
|
|
|
|
import sys
|
|
|
|
import numpy as np
|
|
|
|
|
|
|
|
try:
|
2021-01-24 15:55:36 +00:00
|
|
|
import cPickle # Python2.
|
2021-01-17 06:04:24 +00:00
|
|
|
except ImportError:
|
2021-01-24 15:55:36 +00:00
|
|
|
import _pickle as cPickle # Python3.
|
2021-01-17 06:04:24 +00:00
|
|
|
|
|
|
|
|
2021-01-24 15:55:36 +00:00
|
|
|
def enroll_face_dataset():
|
|
|
|
pwd = sys.path[0]
|
|
|
|
PREDICTOR_PATH = pwd + '/Facial_models/shape_predictor_68_face_landmarks.dat'
|
|
|
|
FACE_RECOGNITION_MODEL_PATH = pwd + '/Facial_models/dlib_face_recognition_resnet_model_v1.dat'
|
2021-01-17 06:04:24 +00:00
|
|
|
|
2021-01-24 15:55:36 +00:00
|
|
|
faceDetector = dlib.get_frontal_face_detector()
|
|
|
|
shapePredictor = dlib.shape_predictor(PREDICTOR_PATH)
|
|
|
|
faceRecognizer = dlib.face_recognition_model_v1(FACE_RECOGNITION_MODEL_PATH)
|
2021-01-17 06:04:24 +00:00
|
|
|
|
2021-01-24 15:55:36 +00:00
|
|
|
faceDatasetFolder = pwd + '/Facial_images/face_rec/train/'
|
2021-01-17 06:04:24 +00:00
|
|
|
|
2021-01-24 15:55:36 +00:00
|
|
|
subfolders = []
|
|
|
|
for x in os.listdir(faceDatasetFolder):
|
|
|
|
xpath = os.path.join(faceDatasetFolder, x)
|
|
|
|
if os.path.isdir(xpath):
|
|
|
|
subfolders.append(xpath)
|
2021-01-17 06:04:24 +00:00
|
|
|
|
2021-01-24 15:55:36 +00:00
|
|
|
nameLabelMap = {}
|
|
|
|
labels = []
|
|
|
|
imagePaths = []
|
|
|
|
for i, subfolder in enumerate(subfolders):
|
|
|
|
for x in os.listdir(subfolder):
|
|
|
|
xpath = os.path.join(subfolder, x)
|
|
|
|
if x.endswith('jpg'):
|
|
|
|
imagePaths.append(xpath)
|
|
|
|
labels.append(i)
|
|
|
|
nameLabelMap[xpath] = subfolder.split('/')[-1]
|
2021-01-17 06:04:24 +00:00
|
|
|
|
2021-01-24 15:55:36 +00:00
|
|
|
index = {}
|
|
|
|
i = 0
|
|
|
|
faceDescriptors = None
|
|
|
|
for imagePath in imagePaths:
|
|
|
|
# print("processing: {}".format(imagePath))
|
|
|
|
img = cv2.imread(imagePath)
|
2021-01-17 06:04:24 +00:00
|
|
|
|
2021-01-24 15:55:36 +00:00
|
|
|
faces = faceDetector(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
|
2021-01-17 06:04:24 +00:00
|
|
|
|
2021-01-24 15:55:36 +00:00
|
|
|
# print("{} Face(s) found".format(len(faces)))
|
2021-01-17 06:04:24 +00:00
|
|
|
|
2021-01-24 15:55:36 +00:00
|
|
|
for k, face in enumerate(faces):
|
2021-01-17 06:04:24 +00:00
|
|
|
|
2021-01-24 15:55:36 +00:00
|
|
|
shape = shapePredictor(cv2.cvtColor(img, cv2.COLOR_BGR2RGB), face)
|
2021-01-17 06:04:24 +00:00
|
|
|
|
2021-01-24 15:55:36 +00:00
|
|
|
landmarks = [(p.x, p.y) for p in shape.parts()]
|
2021-01-17 06:04:24 +00:00
|
|
|
|
2021-01-24 15:55:36 +00:00
|
|
|
faceDescriptor = faceRecognizer.compute_face_descriptor(img, shape)
|
2021-01-17 06:04:24 +00:00
|
|
|
|
2021-01-24 15:55:36 +00:00
|
|
|
faceDescriptorList = [x for x in faceDescriptor]
|
|
|
|
faceDescriptorNdarray = np.asarray(faceDescriptorList, dtype=np.float64)
|
|
|
|
faceDescriptorNdarray = faceDescriptorNdarray[np.newaxis, :]
|
2021-01-17 06:04:24 +00:00
|
|
|
|
2021-01-24 15:55:36 +00:00
|
|
|
if faceDescriptors is None:
|
|
|
|
faceDescriptors = faceDescriptorNdarray
|
|
|
|
else:
|
|
|
|
faceDescriptors = np.concatenate((faceDescriptors, faceDescriptorNdarray), axis=0)
|
2021-01-17 06:04:24 +00:00
|
|
|
|
2021-01-24 15:55:36 +00:00
|
|
|
index[i] = nameLabelMap[imagePath]
|
|
|
|
i += 1
|
2021-01-17 06:04:24 +00:00
|
|
|
|
2021-01-24 15:55:36 +00:00
|
|
|
# Write descriors and index to disk
|
|
|
|
np.save(pwd + '/Facial_models/descriptors.npy', faceDescriptors)
|
|
|
|
with open(pwd + '/Facial_models/index.pkl', 'wb') as f:
|
|
|
|
cPickle.dump(index, f)
|