2021-01-24 15:55:36 +00:00
|
|
|
import os, sys, time
|
2021-01-17 06:04:24 +00:00
|
|
|
import dlib
|
|
|
|
import cv2
|
|
|
|
import numpy as np
|
2021-01-24 15:55:36 +00:00
|
|
|
import DBHelper
|
2021-02-02 00:51:23 +00:00
|
|
|
import time
|
|
|
|
import Upload_Thief
|
|
|
|
|
2021-01-17 06:04:24 +00:00
|
|
|
|
2021-01-24 17:20:01 +00:00
|
|
|
def inference():
|
|
|
|
try:
|
|
|
|
import cPickle # Python 2
|
|
|
|
except ImportError:
|
|
|
|
import _pickle as cPickle # Python 3
|
2021-01-17 06:04:24 +00:00
|
|
|
|
2021-01-24 17:20:01 +00:00
|
|
|
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 17:20:01 +00:00
|
|
|
SKIP_FRAMES = 1
|
|
|
|
THRESHOLD = 0.4
|
2021-01-17 06:04:24 +00:00
|
|
|
|
2021-01-24 17:20:01 +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 17:20:01 +00:00
|
|
|
index = np.load(pwd + '/Facial_models/index.pkl', allow_pickle=True)
|
|
|
|
faceDescriptorsEnrolled = np.load(pwd + '/Facial_models/descriptors.npy')
|
2021-01-17 06:04:24 +00:00
|
|
|
|
2021-01-24 17:20:01 +00:00
|
|
|
cam = cv2.VideoCapture(0)
|
|
|
|
count = 0
|
2021-01-24 00:00:51 +00:00
|
|
|
|
2021-01-24 17:20:01 +00:00
|
|
|
x1 = x2 = y1 = y2 = 0
|
2021-01-24 15:55:36 +00:00
|
|
|
|
2021-01-24 17:20:01 +00:00
|
|
|
cond = False
|
2021-02-02 00:56:51 +00:00
|
|
|
thief_time = 0
|
2021-02-02 01:19:29 +00:00
|
|
|
label = 'unknown'
|
|
|
|
|
2021-01-24 17:20:01 +00:00
|
|
|
while DBHelper.get_power() == "on":
|
|
|
|
t = time.time()
|
|
|
|
success, im = cam.read()
|
2021-01-17 06:04:24 +00:00
|
|
|
|
2021-01-24 17:20:01 +00:00
|
|
|
if not success:
|
|
|
|
print('cannot capture input from camera')
|
|
|
|
break
|
2021-01-17 06:04:24 +00:00
|
|
|
|
2021-01-24 17:20:01 +00:00
|
|
|
if (count % SKIP_FRAMES) == 0:
|
2021-01-17 06:04:24 +00:00
|
|
|
|
2021-01-24 17:20:01 +00:00
|
|
|
img = cv2.cvtColor(im, cv2.COLOR_BGR2RGB)
|
|
|
|
faces = faceDetector(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
|
2021-01-17 06:04:24 +00:00
|
|
|
|
2021-01-24 17:20:01 +00:00
|
|
|
for face in faces:
|
2021-01-17 06:04:24 +00:00
|
|
|
|
2021-01-24 17:20:01 +00:00
|
|
|
shape = shapePredictor(cv2.cvtColor(img, cv2.COLOR_BGR2RGB), face)
|
2021-01-17 06:04:24 +00:00
|
|
|
|
2021-01-24 17:20:01 +00:00
|
|
|
x1 = face.left()
|
|
|
|
y1 = face.top()
|
|
|
|
x2 = face.right()
|
|
|
|
y2 = face.bottom()
|
2021-01-17 06:04:24 +00:00
|
|
|
|
2021-01-24 17:20:01 +00:00
|
|
|
faceDescriptor = faceRecognizer.compute_face_descriptor(img, shape)
|
2021-01-17 06:04:24 +00:00
|
|
|
|
2021-01-24 17:20:01 +00:00
|
|
|
# dlib format to list
|
|
|
|
faceDescriptorList = [m for m in faceDescriptor]
|
|
|
|
# to numpy array
|
|
|
|
faceDescriptorNdarray = np.asarray(faceDescriptorList, dtype=np.float64)
|
|
|
|
faceDescriptorNdarray = faceDescriptorNdarray[np.newaxis, :]
|
2021-01-17 06:04:24 +00:00
|
|
|
|
2021-01-24 17:20:01 +00:00
|
|
|
# Euclidean distances
|
|
|
|
distances = np.linalg.norm(faceDescriptorsEnrolled - faceDescriptorNdarray, axis=1)
|
2021-01-17 06:04:24 +00:00
|
|
|
|
2021-01-24 17:20:01 +00:00
|
|
|
# Calculate minimum distance and index of face
|
|
|
|
argmin = np.argmin(distances) # index
|
|
|
|
minDistance = distances[argmin] # minimum distance
|
2021-01-17 06:04:24 +00:00
|
|
|
|
2021-01-24 17:20:01 +00:00
|
|
|
if minDistance <= THRESHOLD:
|
|
|
|
label = DBHelper.get_firstname(index[argmin]) + "_" + DBHelper.get_lastname(index[argmin])
|
|
|
|
cond = True
|
|
|
|
else:
|
|
|
|
label = 'unknown'
|
|
|
|
cond = False
|
2021-01-17 06:04:24 +00:00
|
|
|
|
2021-01-24 17:20:01 +00:00
|
|
|
# print("time taken = {:.3f} seconds".format(time.time() - t))
|
2021-01-17 06:04:24 +00:00
|
|
|
|
2021-01-24 17:20:01 +00:00
|
|
|
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)
|
2021-01-17 06:04:24 +00:00
|
|
|
|
2021-01-24 17:20:01 +00:00
|
|
|
cv2.imshow('img', im)
|
2021-01-17 06:04:24 +00:00
|
|
|
|
2021-01-24 17:20:01 +00:00
|
|
|
k = cv2.waitKey(1) & 0xff
|
|
|
|
if k == 27:
|
|
|
|
break
|
2021-01-17 06:04:24 +00:00
|
|
|
|
2021-01-24 17:20:01 +00:00
|
|
|
count += 1
|
|
|
|
if cond:
|
|
|
|
DBHelper.set_motor("on")
|
|
|
|
DBHelper.set_alarm("off")
|
2021-02-02 00:56:51 +00:00
|
|
|
thief_time = 0
|
2021-02-02 00:52:44 +00:00
|
|
|
time.sleep(1)
|
2021-01-24 17:20:01 +00:00
|
|
|
elif not cond:
|
|
|
|
DBHelper.set_motor("off")
|
|
|
|
DBHelper.set_alarm("on")
|
2021-02-02 00:56:51 +00:00
|
|
|
thief_time += 1
|
2021-02-02 00:51:23 +00:00
|
|
|
time.sleep(1)
|
2021-02-02 00:56:51 +00:00
|
|
|
if thief_time == 120:
|
2021-02-02 00:51:23 +00:00
|
|
|
Upload_Thief.upload_thief_face()
|
|
|
|
DBHelper.set_power("off")
|
2021-01-24 17:20:01 +00:00
|
|
|
|
|
|
|
DBHelper.set_alarm("off")
|
|
|
|
DBHelper.set_motor("off")
|
|
|
|
cv2.destroyAllWindows()
|
|
|
|
|
2021-02-02 00:51:23 +00:00
|
|
|
|
2021-01-24 17:20:01 +00:00
|
|
|
if __name__ == "__main__":
|
2021-02-02 00:51:23 +00:00
|
|
|
inference()
|