Upload dlib DNN model and Enrollment & Inference files
77
Facial_Recognition_Enrollment.py
Normal 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)
|
99
Facial_Recognition_Inference.py
Normal 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()
|
|
@ -5,7 +5,7 @@ import cv2
|
|||
|
||||
|
||||
def register_your_face(label):
|
||||
num_cap = 20
|
||||
num_cap = 60
|
||||
|
||||
path = sys.path[0] + '/Facial_images/face_rec/train/' + label
|
||||
|
||||
|
|
Before Width: | Height: | Size: 80 KiB After Width: | Height: | Size: 86 KiB |
Before Width: | Height: | Size: 80 KiB After Width: | Height: | Size: 88 KiB |
Before Width: | Height: | Size: 78 KiB After Width: | Height: | Size: 87 KiB |
Before Width: | Height: | Size: 78 KiB After Width: | Height: | Size: 88 KiB |
Before Width: | Height: | Size: 78 KiB After Width: | Height: | Size: 88 KiB |
Before Width: | Height: | Size: 78 KiB After Width: | Height: | Size: 87 KiB |
Before Width: | Height: | Size: 78 KiB After Width: | Height: | Size: 83 KiB |
Before Width: | Height: | Size: 78 KiB After Width: | Height: | Size: 82 KiB |
Before Width: | Height: | Size: 79 KiB After Width: | Height: | Size: 82 KiB |
Before Width: | Height: | Size: 79 KiB After Width: | Height: | Size: 84 KiB |
Before Width: | Height: | Size: 79 KiB After Width: | Height: | Size: 87 KiB |
Before Width: | Height: | Size: 79 KiB After Width: | Height: | Size: 86 KiB |
Before Width: | Height: | Size: 77 KiB After Width: | Height: | Size: 85 KiB |
BIN
Facial_images/face_rec/train/User_2/20.jpg
Normal file
After Width: | Height: | Size: 85 KiB |
BIN
Facial_images/face_rec/train/User_2/21.jpg
Normal file
After Width: | Height: | Size: 87 KiB |
BIN
Facial_images/face_rec/train/User_2/22.jpg
Normal file
After Width: | Height: | Size: 86 KiB |
BIN
Facial_images/face_rec/train/User_2/23.jpg
Normal file
After Width: | Height: | Size: 89 KiB |
BIN
Facial_images/face_rec/train/User_2/24.jpg
Normal file
After Width: | Height: | Size: 88 KiB |
BIN
Facial_images/face_rec/train/User_2/25.jpg
Normal file
After Width: | Height: | Size: 88 KiB |
BIN
Facial_images/face_rec/train/User_2/26.jpg
Normal file
After Width: | Height: | Size: 87 KiB |
BIN
Facial_images/face_rec/train/User_2/27.jpg
Normal file
After Width: | Height: | Size: 86 KiB |
BIN
Facial_images/face_rec/train/User_2/28.jpg
Normal file
After Width: | Height: | Size: 84 KiB |
BIN
Facial_images/face_rec/train/User_2/29.jpg
Normal file
After Width: | Height: | Size: 84 KiB |
Before Width: | Height: | Size: 78 KiB After Width: | Height: | Size: 85 KiB |
BIN
Facial_images/face_rec/train/User_2/30.jpg
Normal file
After Width: | Height: | Size: 83 KiB |
BIN
Facial_images/face_rec/train/User_2/31.jpg
Normal file
After Width: | Height: | Size: 85 KiB |
BIN
Facial_images/face_rec/train/User_2/32.jpg
Normal file
After Width: | Height: | Size: 86 KiB |
BIN
Facial_images/face_rec/train/User_2/33.jpg
Normal file
After Width: | Height: | Size: 86 KiB |
BIN
Facial_images/face_rec/train/User_2/34.jpg
Normal file
After Width: | Height: | Size: 86 KiB |
BIN
Facial_images/face_rec/train/User_2/35.jpg
Normal file
After Width: | Height: | Size: 85 KiB |
BIN
Facial_images/face_rec/train/User_2/36.jpg
Normal file
After Width: | Height: | Size: 84 KiB |
BIN
Facial_images/face_rec/train/User_2/37.jpg
Normal file
After Width: | Height: | Size: 86 KiB |
BIN
Facial_images/face_rec/train/User_2/38.jpg
Normal file
After Width: | Height: | Size: 89 KiB |
BIN
Facial_images/face_rec/train/User_2/39.jpg
Normal file
After Width: | Height: | Size: 90 KiB |
Before Width: | Height: | Size: 78 KiB After Width: | Height: | Size: 85 KiB |
BIN
Facial_images/face_rec/train/User_2/40.jpg
Normal file
After Width: | Height: | Size: 91 KiB |
BIN
Facial_images/face_rec/train/User_2/41.jpg
Normal file
After Width: | Height: | Size: 90 KiB |
BIN
Facial_images/face_rec/train/User_2/42.jpg
Normal file
After Width: | Height: | Size: 90 KiB |
BIN
Facial_images/face_rec/train/User_2/43.jpg
Normal file
After Width: | Height: | Size: 92 KiB |
BIN
Facial_images/face_rec/train/User_2/44.jpg
Normal file
After Width: | Height: | Size: 92 KiB |
BIN
Facial_images/face_rec/train/User_2/45.jpg
Normal file
After Width: | Height: | Size: 94 KiB |
BIN
Facial_images/face_rec/train/User_2/46.jpg
Normal file
After Width: | Height: | Size: 92 KiB |
BIN
Facial_images/face_rec/train/User_2/47.jpg
Normal file
After Width: | Height: | Size: 92 KiB |
BIN
Facial_images/face_rec/train/User_2/48.jpg
Normal file
After Width: | Height: | Size: 92 KiB |
BIN
Facial_images/face_rec/train/User_2/49.jpg
Normal file
After Width: | Height: | Size: 92 KiB |
Before Width: | Height: | Size: 79 KiB After Width: | Height: | Size: 88 KiB |
Before Width: | Height: | Size: 78 KiB After Width: | Height: | Size: 88 KiB |
Before Width: | Height: | Size: 78 KiB After Width: | Height: | Size: 87 KiB |
Before Width: | Height: | Size: 78 KiB After Width: | Height: | Size: 87 KiB |
Before Width: | Height: | Size: 78 KiB After Width: | Height: | Size: 87 KiB |
BIN
Facial_images/face_rec/train/User_2/a.jpg
Normal file
After Width: | Height: | Size: 80 KiB |
BIN
Facial_images/face_rec/train/User_2/b.jpg
Normal file
After Width: | Height: | Size: 80 KiB |
BIN
Facial_images/face_rec/train/User_2/c.jpg
Normal file
After Width: | Height: | Size: 77 KiB |
BIN
Facial_images/face_rec/train/User_2/d.jpg
Normal file
After Width: | Height: | Size: 78 KiB |
BIN
Facial_images/face_rec/train/User_2/e.jpg
Normal file
After Width: | Height: | Size: 78 KiB |
BIN
Facial_images/face_rec/train/User_2/f.jpg
Normal file
After Width: | Height: | Size: 79 KiB |
BIN
Facial_images/face_rec/train/User_2/g.jpg
Normal file
After Width: | Height: | Size: 78 KiB |
BIN
Facial_images/face_rec/train/User_2/h.jpg
Normal file
After Width: | Height: | Size: 78 KiB |
BIN
Facial_images/face_rec/train/User_2/i.jpg
Normal file
After Width: | Height: | Size: 78 KiB |
BIN
Facial_images/face_rec/train/User_2/j.jpg
Normal file
After Width: | Height: | Size: 78 KiB |
BIN
Facial_images/face_rec/train/User_2/k.jpg
Normal file
After Width: | Height: | Size: 78 KiB |
BIN
Facial_images/face_rec/train/User_2/l.jpg
Normal file
After Width: | Height: | Size: 78 KiB |
BIN
Facial_images/face_rec/train/User_2/m.jpg
Normal file
After Width: | Height: | Size: 78 KiB |
BIN
Facial_images/face_rec/train/User_2/n.jpg
Normal file
After Width: | Height: | Size: 78 KiB |
BIN
Facial_images/face_rec/train/User_2/o.jpg
Normal file
After Width: | Height: | Size: 78 KiB |
BIN
Facial_images/face_rec/train/User_2/p.jpg
Normal file
After Width: | Height: | Size: 78 KiB |
BIN
Facial_images/face_rec/train/User_2/q.jpg
Normal file
After Width: | Height: | Size: 79 KiB |
BIN
Facial_images/face_rec/train/User_2/r.jpg
Normal file
After Width: | Height: | Size: 79 KiB |
BIN
Facial_images/face_rec/train/User_2/s.jpg
Normal file
After Width: | Height: | Size: 79 KiB |
BIN
Facial_images/face_rec/train/User_2/t.jpg
Normal file
After Width: | Height: | Size: 79 KiB |