Integrate face registration and enrollment
This commit is contained in:
parent
2d1ee72eb7
commit
4d0d442ab6
5 changed files with 54 additions and 51 deletions
|
@ -9,69 +9,70 @@ try:
|
||||||
except ImportError:
|
except ImportError:
|
||||||
import _pickle as cPickle # Python3.
|
import _pickle as cPickle # Python3.
|
||||||
|
|
||||||
pwd = sys.path[0]
|
def enroll_face_dataset():
|
||||||
PREDICTOR_PATH = pwd + '/Facial_models/shape_predictor_68_face_landmarks.dat'
|
pwd = sys.path[0]
|
||||||
FACE_RECOGNITION_MODEL_PATH = pwd + '/Facial_models/dlib_face_recognition_resnet_model_v1.dat'
|
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'
|
||||||
|
|
||||||
faceDetector = dlib.get_frontal_face_detector()
|
faceDetector = dlib.get_frontal_face_detector()
|
||||||
shapePredictor = dlib.shape_predictor(PREDICTOR_PATH)
|
shapePredictor = dlib.shape_predictor(PREDICTOR_PATH)
|
||||||
faceRecognizer = dlib.face_recognition_model_v1(FACE_RECOGNITION_MODEL_PATH)
|
faceRecognizer = dlib.face_recognition_model_v1(FACE_RECOGNITION_MODEL_PATH)
|
||||||
|
|
||||||
|
|
||||||
faceDatasetFolder = pwd + '/Facial_images/face_rec/train/'
|
faceDatasetFolder = pwd + '/Facial_images/face_rec/train/'
|
||||||
|
|
||||||
subfolders = []
|
subfolders = []
|
||||||
for x in os.listdir(faceDatasetFolder):
|
for x in os.listdir(faceDatasetFolder):
|
||||||
xpath = os.path.join(faceDatasetFolder, x)
|
xpath = os.path.join(faceDatasetFolder, x)
|
||||||
if os.path.isdir(xpath):
|
if os.path.isdir(xpath):
|
||||||
subfolders.append(xpath)
|
subfolders.append(xpath)
|
||||||
|
|
||||||
|
|
||||||
nameLabelMap = {}
|
nameLabelMap = {}
|
||||||
labels = []
|
labels = []
|
||||||
imagePaths = []
|
imagePaths = []
|
||||||
for i, subfolder in enumerate(subfolders):
|
for i, subfolder in enumerate(subfolders):
|
||||||
for x in os.listdir(subfolder):
|
for x in os.listdir(subfolder):
|
||||||
xpath = os.path.join(subfolder, x)
|
xpath = os.path.join(subfolder, x)
|
||||||
if x.endswith('jpg'):
|
if x.endswith('jpg'):
|
||||||
imagePaths.append(xpath)
|
imagePaths.append(xpath)
|
||||||
labels.append(i)
|
labels.append(i)
|
||||||
nameLabelMap[xpath] = subfolder.split('/')[-1]
|
nameLabelMap[xpath] = subfolder.split('/')[-1]
|
||||||
|
|
||||||
index = {}
|
index = {}
|
||||||
i = 0
|
i = 0
|
||||||
faceDescriptors = None
|
faceDescriptors = None
|
||||||
for imagePath in imagePaths:
|
for imagePath in imagePaths:
|
||||||
print("processing: {}".format(imagePath))
|
print("processing: {}".format(imagePath))
|
||||||
img = cv2.imread(imagePath)
|
img = cv2.imread(imagePath)
|
||||||
|
|
||||||
faces = faceDetector(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
|
faces = faceDetector(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
|
||||||
|
|
||||||
print("{} Face(s) found".format(len(faces)))
|
print("{} Face(s) found".format(len(faces)))
|
||||||
|
|
||||||
for k, face in enumerate(faces):
|
for k, face in enumerate(faces):
|
||||||
|
|
||||||
shape = shapePredictor(cv2.cvtColor(img, cv2.COLOR_BGR2RGB), face)
|
shape = shapePredictor(cv2.cvtColor(img, cv2.COLOR_BGR2RGB), face)
|
||||||
|
|
||||||
landmarks = [(p.x, p.y) for p in shape.parts()]
|
landmarks = [(p.x, p.y) for p in shape.parts()]
|
||||||
|
|
||||||
faceDescriptor = faceRecognizer.compute_face_descriptor(img, shape)
|
faceDescriptor = faceRecognizer.compute_face_descriptor(img, shape)
|
||||||
|
|
||||||
|
|
||||||
faceDescriptorList = [x for x in faceDescriptor]
|
faceDescriptorList = [x for x in faceDescriptor]
|
||||||
faceDescriptorNdarray = np.asarray(faceDescriptorList, dtype=np.float64)
|
faceDescriptorNdarray = np.asarray(faceDescriptorList, dtype=np.float64)
|
||||||
faceDescriptorNdarray = faceDescriptorNdarray[np.newaxis, :]
|
faceDescriptorNdarray = faceDescriptorNdarray[np.newaxis, :]
|
||||||
|
|
||||||
|
|
||||||
if faceDescriptors is None:
|
if faceDescriptors is None:
|
||||||
faceDescriptors = faceDescriptorNdarray
|
faceDescriptors = faceDescriptorNdarray
|
||||||
else:
|
else:
|
||||||
faceDescriptors = np.concatenate((faceDescriptors, faceDescriptorNdarray), axis=0)
|
faceDescriptors = np.concatenate((faceDescriptors, faceDescriptorNdarray), axis=0)
|
||||||
|
|
||||||
index[i] = nameLabelMap[imagePath]
|
index[i] = nameLabelMap[imagePath]
|
||||||
i += 1
|
i += 1
|
||||||
|
|
||||||
# Write descriors and index to disk
|
# Write descriors and index to disk
|
||||||
np.save(pwd+'/Facial_models/descriptors.npy', faceDescriptors)
|
np.save(pwd+'/Facial_models/descriptors.npy', faceDescriptors)
|
||||||
with open(pwd+'/Facial_models/index.pkl', 'wb') as f:
|
with open(pwd+'/Facial_models/index.pkl', 'wb') as f:
|
||||||
cPickle.dump(index, f)
|
cPickle.dump(index, f)
|
||||||
|
|
|
@ -2,10 +2,11 @@ import sys
|
||||||
import os
|
import os
|
||||||
import math
|
import math
|
||||||
import cv2
|
import cv2
|
||||||
|
import Facial_Recognition_Enrollment
|
||||||
|
|
||||||
|
|
||||||
def register_your_face(label):
|
def register_your_face(label):
|
||||||
num_cap = 60
|
num_cap = 50
|
||||||
|
|
||||||
path = sys.path[0] + '/Facial_images/face_rec/train/' + label
|
path = sys.path[0] + '/Facial_images/face_rec/train/' + label
|
||||||
|
|
||||||
|
@ -14,7 +15,7 @@ def register_your_face(label):
|
||||||
if not folder:
|
if not folder:
|
||||||
os.makedirs(path)
|
os.makedirs(path)
|
||||||
|
|
||||||
cap = cv2.VideoCapture(0)
|
cap = cv2.VideoCapture(1)
|
||||||
c = 0
|
c = 0
|
||||||
while c < num_cap:
|
while c < num_cap:
|
||||||
ret, frame = cap.read()
|
ret, frame = cap.read()
|
||||||
|
@ -31,5 +32,6 @@ def register_your_face(label):
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
label = input('Enter a label:')
|
label = input('Enter a label: ')
|
||||||
register_your_face(label)
|
register_your_face(label)
|
||||||
|
Facial_Recognition_Enrollment.enroll_face_dataset()
|
||||||
|
|
Binary file not shown.
Binary file not shown.
BIN
__pycache__/Facial_Recognition_Enrollment.cpython-38.pyc
Normal file
BIN
__pycache__/Facial_Recognition_Enrollment.cpython-38.pyc
Normal file
Binary file not shown.
Loading…
Reference in a new issue