Updated the Integration. Tested the system, it works.
|
@ -4,99 +4,105 @@ import cv2
|
||||||
import numpy as np
|
import numpy as np
|
||||||
import DBHelper
|
import DBHelper
|
||||||
|
|
||||||
try:
|
|
||||||
import cPickle # Python 2
|
|
||||||
except ImportError:
|
|
||||||
import _pickle as cPickle # Python 3
|
|
||||||
|
|
||||||
pwd = sys.path[0]
|
def inference():
|
||||||
PREDICTOR_PATH = pwd + '/Facial_models/shape_predictor_68_face_landmarks.dat'
|
try:
|
||||||
FACE_RECOGNITION_MODEL_PATH = pwd + '/Facial_models/dlib_face_recognition_resnet_model_v1.dat'
|
import cPickle # Python 2
|
||||||
|
except ImportError:
|
||||||
|
import _pickle as cPickle # Python 3
|
||||||
|
|
||||||
SKIP_FRAMES = 1
|
pwd = sys.path[0]
|
||||||
THRESHOLD = 0.4
|
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()
|
SKIP_FRAMES = 1
|
||||||
shapePredictor = dlib.shape_predictor(PREDICTOR_PATH)
|
THRESHOLD = 0.4
|
||||||
faceRecognizer = dlib.face_recognition_model_v1(FACE_RECOGNITION_MODEL_PATH)
|
|
||||||
|
|
||||||
index = np.load(pwd + '/Facial_models/index.pkl', allow_pickle=True)
|
faceDetector = dlib.get_frontal_face_detector()
|
||||||
faceDescriptorsEnrolled = np.load(pwd + '/Facial_models/descriptors.npy')
|
shapePredictor = dlib.shape_predictor(PREDICTOR_PATH)
|
||||||
|
faceRecognizer = dlib.face_recognition_model_v1(FACE_RECOGNITION_MODEL_PATH)
|
||||||
|
|
||||||
cam = cv2.VideoCapture(0)
|
index = np.load(pwd + '/Facial_models/index.pkl', allow_pickle=True)
|
||||||
count = 0
|
faceDescriptorsEnrolled = np.load(pwd + '/Facial_models/descriptors.npy')
|
||||||
|
|
||||||
x1 = x2 = y1 = y2 = 0
|
cam = cv2.VideoCapture(0)
|
||||||
|
count = 0
|
||||||
|
|
||||||
cond = False
|
x1 = x2 = y1 = y2 = 0
|
||||||
|
|
||||||
while DBHelper.get_power() == "on":
|
cond = False
|
||||||
t = time.time()
|
|
||||||
success, im = cam.read()
|
|
||||||
|
|
||||||
if not success:
|
while DBHelper.get_power() == "on":
|
||||||
print('cannot capture input from camera')
|
t = time.time()
|
||||||
break
|
success, im = cam.read()
|
||||||
|
|
||||||
if (count % SKIP_FRAMES) == 0:
|
if not success:
|
||||||
|
print('cannot capture input from camera')
|
||||||
|
break
|
||||||
|
|
||||||
img = cv2.cvtColor(im, cv2.COLOR_BGR2RGB)
|
if (count % SKIP_FRAMES) == 0:
|
||||||
faces = faceDetector(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
|
|
||||||
|
|
||||||
for face in faces:
|
img = cv2.cvtColor(im, cv2.COLOR_BGR2RGB)
|
||||||
|
faces = faceDetector(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
|
||||||
|
|
||||||
shape = shapePredictor(cv2.cvtColor(img, cv2.COLOR_BGR2RGB), face)
|
for face in faces:
|
||||||
|
|
||||||
x1 = face.left()
|
shape = shapePredictor(cv2.cvtColor(img, cv2.COLOR_BGR2RGB), face)
|
||||||
y1 = face.top()
|
|
||||||
x2 = face.right()
|
|
||||||
y2 = face.bottom()
|
|
||||||
|
|
||||||
faceDescriptor = faceRecognizer.compute_face_descriptor(img, shape)
|
x1 = face.left()
|
||||||
|
y1 = face.top()
|
||||||
|
x2 = face.right()
|
||||||
|
y2 = face.bottom()
|
||||||
|
|
||||||
# dlib format to list
|
faceDescriptor = faceRecognizer.compute_face_descriptor(img, shape)
|
||||||
faceDescriptorList = [m for m in faceDescriptor]
|
|
||||||
# to numpy array
|
|
||||||
faceDescriptorNdarray = np.asarray(faceDescriptorList, dtype=np.float64)
|
|
||||||
faceDescriptorNdarray = faceDescriptorNdarray[np.newaxis, :]
|
|
||||||
|
|
||||||
# Euclidean distances
|
# dlib format to list
|
||||||
distances = np.linalg.norm(faceDescriptorsEnrolled - faceDescriptorNdarray, axis=1)
|
faceDescriptorList = [m for m in faceDescriptor]
|
||||||
|
# to numpy array
|
||||||
|
faceDescriptorNdarray = np.asarray(faceDescriptorList, dtype=np.float64)
|
||||||
|
faceDescriptorNdarray = faceDescriptorNdarray[np.newaxis, :]
|
||||||
|
|
||||||
# Calculate minimum distance and index of face
|
# Euclidean distances
|
||||||
argmin = np.argmin(distances) # index
|
distances = np.linalg.norm(faceDescriptorsEnrolled - faceDescriptorNdarray, axis=1)
|
||||||
minDistance = distances[argmin] # minimum distance
|
|
||||||
|
|
||||||
if minDistance <= THRESHOLD:
|
# Calculate minimum distance and index of face
|
||||||
label = DBHelper.get_firstname(index[argmin]) + "_" + DBHelper.get_lastname(index[argmin])
|
argmin = np.argmin(distances) # index
|
||||||
cond = True
|
minDistance = distances[argmin] # minimum distance
|
||||||
else:
|
|
||||||
label = 'unknown'
|
|
||||||
cond = False
|
|
||||||
|
|
||||||
# print("time taken = {:.3f} seconds".format(time.time() - t))
|
if minDistance <= THRESHOLD:
|
||||||
|
label = DBHelper.get_firstname(index[argmin]) + "_" + DBHelper.get_lastname(index[argmin])
|
||||||
|
cond = True
|
||||||
|
else:
|
||||||
|
label = 'unknown'
|
||||||
|
cond = False
|
||||||
|
|
||||||
cv2.rectangle(im, (x1, y1), (x2, y2), (0, 255, 0), 2)
|
# print("time taken = {:.3f} seconds".format(time.time() - t))
|
||||||
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)
|
cv2.rectangle(im, (x1, y1), (x2, y2), (0, 255, 0), 2)
|
||||||
|
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)
|
||||||
|
|
||||||
k = cv2.waitKey(1) & 0xff
|
cv2.imshow('img', im)
|
||||||
if k == 27:
|
|
||||||
break
|
|
||||||
|
|
||||||
count += 1
|
k = cv2.waitKey(1) & 0xff
|
||||||
if cond:
|
if k == 27:
|
||||||
DBHelper.set_motor("on")
|
break
|
||||||
DBHelper.set_alarm("off")
|
|
||||||
elif not cond:
|
|
||||||
DBHelper.set_motor("off")
|
|
||||||
DBHelper.set_alarm("on")
|
|
||||||
|
|
||||||
DBHelper.set_alarm("off")
|
count += 1
|
||||||
DBHelper.set_motor("off")
|
if cond:
|
||||||
cv2.destroyAllWindows()
|
DBHelper.set_motor("on")
|
||||||
|
DBHelper.set_alarm("off")
|
||||||
|
elif not cond:
|
||||||
|
DBHelper.set_motor("off")
|
||||||
|
DBHelper.set_alarm("on")
|
||||||
|
|
||||||
|
DBHelper.set_alarm("off")
|
||||||
|
DBHelper.set_motor("off")
|
||||||
|
cv2.destroyAllWindows()
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
inference()
|
||||||
|
|
Before Width: | Height: | Size: 75 KiB After Width: | Height: | Size: 74 KiB |
Before Width: | Height: | Size: 74 KiB After Width: | Height: | Size: 74 KiB |
Before Width: | Height: | Size: 79 KiB After Width: | Height: | Size: 76 KiB |
Before Width: | Height: | Size: 79 KiB After Width: | Height: | Size: 73 KiB |
Before Width: | Height: | Size: 82 KiB After Width: | Height: | Size: 73 KiB |
Before Width: | Height: | Size: 83 KiB After Width: | Height: | Size: 74 KiB |
Before Width: | Height: | Size: 82 KiB After Width: | Height: | Size: 74 KiB |
Before Width: | Height: | Size: 82 KiB After Width: | Height: | Size: 73 KiB |
Before Width: | Height: | Size: 82 KiB After Width: | Height: | Size: 74 KiB |
Before Width: | Height: | Size: 82 KiB After Width: | Height: | Size: 73 KiB |
Before Width: | Height: | Size: 82 KiB After Width: | Height: | Size: 73 KiB |
Before Width: | Height: | Size: 81 KiB After Width: | Height: | Size: 73 KiB |
Before Width: | Height: | Size: 74 KiB After Width: | Height: | Size: 75 KiB |
Before Width: | Height: | Size: 74 KiB After Width: | Height: | Size: 75 KiB |
Before Width: | Height: | Size: 73 KiB After Width: | Height: | Size: 76 KiB |
Before Width: | Height: | Size: 76 KiB After Width: | Height: | Size: 75 KiB |
Before Width: | Height: | Size: 76 KiB After Width: | Height: | Size: 75 KiB |
Before Width: | Height: | Size: 77 KiB After Width: | Height: | Size: 75 KiB |
Before Width: | Height: | Size: 79 KiB After Width: | Height: | Size: 75 KiB |
Before Width: | Height: | Size: 80 KiB After Width: | Height: | Size: 75 KiB |
Before Width: | Height: | Size: 86 KiB After Width: | Height: | Size: 80 KiB |
Before Width: | Height: | Size: 88 KiB After Width: | Height: | Size: 80 KiB |
Before Width: | Height: | Size: 87 KiB After Width: | Height: | Size: 78 KiB |
Before Width: | Height: | Size: 88 KiB After Width: | Height: | Size: 78 KiB |
Before Width: | Height: | Size: 88 KiB After Width: | Height: | Size: 78 KiB |
Before Width: | Height: | Size: 87 KiB After Width: | Height: | Size: 78 KiB |
Before Width: | Height: | Size: 83 KiB After Width: | Height: | Size: 78 KiB |
Before Width: | Height: | Size: 82 KiB After Width: | Height: | Size: 78 KiB |
Before Width: | Height: | Size: 82 KiB After Width: | Height: | Size: 79 KiB |
Before Width: | Height: | Size: 84 KiB After Width: | Height: | Size: 79 KiB |
Before Width: | Height: | Size: 87 KiB After Width: | Height: | Size: 79 KiB |
Before Width: | Height: | Size: 86 KiB After Width: | Height: | Size: 79 KiB |
Before Width: | Height: | Size: 85 KiB After Width: | Height: | Size: 77 KiB |
Before Width: | Height: | Size: 85 KiB After Width: | Height: | Size: 78 KiB |
Before Width: | Height: | Size: 85 KiB After Width: | Height: | Size: 78 KiB |
Before Width: | Height: | Size: 88 KiB After Width: | Height: | Size: 79 KiB |
Before Width: | Height: | Size: 88 KiB After Width: | Height: | Size: 78 KiB |
Before Width: | Height: | Size: 87 KiB After Width: | Height: | Size: 78 KiB |
Before Width: | Height: | Size: 87 KiB After Width: | Height: | Size: 78 KiB |
Before Width: | Height: | Size: 87 KiB After Width: | Height: | Size: 78 KiB |
BIN
__pycache__/Facial_Recognition_Enrollment.cpython-36.pyc
Normal file
BIN
__pycache__/Facial_Recognition_Inference.cpython-36.pyc
Normal file
|
@ -32,7 +32,7 @@ def start():
|
||||||
print("Success.")
|
print("Success.")
|
||||||
except:
|
except:
|
||||||
print("No Thieves are registered.")
|
print("No Thieves are registered.")
|
||||||
Facial_Recognition_Inference
|
Facial_Recognition_Inference.inference()
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
|