Upload dlib DNN model and Enrollment & Inference files

This commit is contained in:
Feier Zhang 2021-01-17 01:04:24 -05:00
parent ddcfaeff8a
commit 2a34cac0b7
76 changed files with 177 additions and 1 deletions

View file

@ -0,0 +1,77 @@
import os
import dlib
import cv2
import sys
import numpy as np
try:
import cPickle # Python2.
except ImportError:
import _pickle as cPickle # Python3.
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'
faceDetector = dlib.get_frontal_face_detector()
shapePredictor = dlib.shape_predictor(PREDICTOR_PATH)
faceRecognizer = dlib.face_recognition_model_v1(FACE_RECOGNITION_MODEL_PATH)
faceDatasetFolder = pwd + '/Facial_images/face_rec/train/'
subfolders = []
for x in os.listdir(faceDatasetFolder):
xpath = os.path.join(faceDatasetFolder, x)
if os.path.isdir(xpath):
subfolders.append(xpath)
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]
index = {}
i = 0
faceDescriptors = None
for imagePath in imagePaths:
print("processing: {}".format(imagePath))
img = cv2.imread(imagePath)
faces = faceDetector(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
print("{} Face(s) found".format(len(faces)))
for k, face in enumerate(faces):
shape = shapePredictor(cv2.cvtColor(img, cv2.COLOR_BGR2RGB), face)
landmarks = [(p.x, p.y) for p in shape.parts()]
faceDescriptor = faceRecognizer.compute_face_descriptor(img, shape)
faceDescriptorList = [x for x in faceDescriptor]
faceDescriptorNdarray = np.asarray(faceDescriptorList, dtype=np.float64)
faceDescriptorNdarray = faceDescriptorNdarray[np.newaxis, :]
if faceDescriptors is None:
faceDescriptors = faceDescriptorNdarray
else:
faceDescriptors = np.concatenate((faceDescriptors, faceDescriptorNdarray), axis=0)
index[i] = nameLabelMap[imagePath]
i += 1
# 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)

View file

@ -0,0 +1,99 @@
import os,sys,time
import dlib
import cv2
import numpy as np
try:
import cPickle # Python 2
except ImportError:
import _pickle as cPickle # Python 3
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'
SKIP_FRAMES = 10
THRESHOLD = 0.4
faceDetector = dlib.get_frontal_face_detector()
shapePredictor = dlib.shape_predictor(PREDICTOR_PATH)
faceRecognizer = dlib.face_recognition_model_v1(FACE_RECOGNITION_MODEL_PATH)
index = np.load(pwd+'/Facial_models/index.pkl', allow_pickle=True)
faceDescriptorsEnrolled = np.load(pwd+'/Facial_models/descriptors.npy')
cam = cv2.VideoCapture(1)
count = 0
while True:
t = time.time()
success, im = cam.read()
if not success:
print('cannot capture input from camera')
break
if (count % SKIP_FRAMES) == 0:
img = cv2.cvtColor(im, cv2.COLOR_BGR2RGB)
faces = faceDetector(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
for face in faces:
shape = shapePredictor(cv2.cvtColor(img, cv2.COLOR_BGR2RGB), face)
x1 = face.left()
y1 = face.top()
x2 = face.right()
y2 = face.bottom()
faceDescriptor = faceRecognizer.compute_face_descriptor(img, shape)
# dlib format to list
faceDescriptorList = [m for m in faceDescriptor]
# to numpy array
faceDescriptorNdarray = np.asarray(faceDescriptorList, dtype=np.float64)
faceDescriptorNdarray = faceDescriptorNdarray[np.newaxis, :]
# Euclidean distances
distances = np.linalg.norm(faceDescriptorsEnrolled - faceDescriptorNdarray, axis=1)
# Calculate minimum distance and index of face
argmin = np.argmin(distances) # index
minDistance = distances[argmin] # minimum distance
if minDistance <= THRESHOLD:
label = index[argmin]
else:
label = 'unknown'
print("time taken = {:.3f} seconds".format(time.time() - t))
cv2.rectangle(im, (x1, y1), (x2, y2), (0, 255, 0), 2)
#center = (int((x1 + x2)/2.0), int((y1 + y2)/2.0))
#radius = int((y2-y1)/2.0)
#color = (0, 255, 0)
#cv2.circle(im, center, radius, color, thickness=1, lineType=8, shift=0)
font_face = cv2.FONT_HERSHEY_SIMPLEX
font_scale = 0.8
text_color = (0, 255, 0)
printLabel = '{} {:0.4f}'.format(label, minDistance)
cv2.putText(im, printLabel, (int(x1), int(y1)) , font_face, font_scale, text_color, thickness=2)
cv2.imshow('img', im)
k = cv2.waitKey(1) & 0xff
if k == 27:
break
count += 1
cv2.destroyAllWindows()

View file

@ -5,7 +5,7 @@ import cv2
def register_your_face(label): def register_your_face(label):
num_cap = 20 num_cap = 60
path = sys.path[0] + '/Facial_images/face_rec/train/' + label path = sys.path[0] + '/Facial_images/face_rec/train/' + label

Binary file not shown.

Before

Width:  |  Height:  |  Size: 80 KiB

After

Width:  |  Height:  |  Size: 86 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 80 KiB

After

Width:  |  Height:  |  Size: 88 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 78 KiB

After

Width:  |  Height:  |  Size: 87 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 78 KiB

After

Width:  |  Height:  |  Size: 88 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 78 KiB

After

Width:  |  Height:  |  Size: 88 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 78 KiB

After

Width:  |  Height:  |  Size: 87 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 78 KiB

After

Width:  |  Height:  |  Size: 83 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 78 KiB

After

Width:  |  Height:  |  Size: 82 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 79 KiB

After

Width:  |  Height:  |  Size: 82 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 79 KiB

After

Width:  |  Height:  |  Size: 84 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 79 KiB

After

Width:  |  Height:  |  Size: 87 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 79 KiB

After

Width:  |  Height:  |  Size: 86 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 77 KiB

After

Width:  |  Height:  |  Size: 85 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 85 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 87 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 86 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 89 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 88 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 88 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 87 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 86 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 84 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 84 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 78 KiB

After

Width:  |  Height:  |  Size: 85 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 83 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 85 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 86 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 86 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 86 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 85 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 84 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 86 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 89 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 90 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 78 KiB

After

Width:  |  Height:  |  Size: 85 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 91 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 90 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 90 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 92 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 92 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 94 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 92 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 92 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 92 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 92 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 79 KiB

After

Width:  |  Height:  |  Size: 88 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 78 KiB

After

Width:  |  Height:  |  Size: 88 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 78 KiB

After

Width:  |  Height:  |  Size: 87 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 78 KiB

After

Width:  |  Height:  |  Size: 87 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 78 KiB

After

Width:  |  Height:  |  Size: 87 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 80 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 80 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 77 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 78 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 78 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 79 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 78 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 78 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 78 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 78 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 78 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 78 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 78 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 78 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 78 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 78 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 79 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 79 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 79 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 79 KiB

Binary file not shown.

BIN
Facial_models/index.pkl Normal file

Binary file not shown.