Vehicle-Anti-Theft-Face-Rec.../Facial_Recognition_Inference.py

121 lines
3.6 KiB
Python
Raw Normal View History

import os, sys, time
import dlib
import cv2
import numpy as np
import DBHelper
2021-02-02 00:51:23 +00:00
import time
import Upload_Thief
def inference():
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 = 1
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(0)
count = 0
2021-01-24 00:00:51 +00:00
x1 = x2 = y1 = y2 = 0
cond = False
2021-02-04 22:41:59 +00:00
thief = False
2021-02-02 00:56:51 +00:00
thief_time = 0
2021-02-02 01:19:29 +00:00
label = 'unknown'
while DBHelper.get_power() == "on":
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 = DBHelper.get_firstname(index[argmin]) + "_" + DBHelper.get_lastname(index[argmin])
cond = True
else:
label = 'unknown'
cond = False
# print("time taken = {:.3f} seconds".format(time.time() - t))
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)
cv2.imshow('img', im)
k = cv2.waitKey(1) & 0xff
if k == 27:
break
count += 1
if cond:
DBHelper.set_motor("on")
DBHelper.set_alarm("off")
2021-02-02 00:56:51 +00:00
thief_time = 0
elif not cond:
DBHelper.set_motor("off")
DBHelper.set_alarm("on")
2021-02-02 00:56:51 +00:00
thief_time += 1
2021-02-04 22:41:59 +00:00
if thief_time == 10:
thief = True
2021-02-02 00:51:23 +00:00
DBHelper.set_power("off")
DBHelper.set_alarm("off")
DBHelper.set_motor("off")
cv2.destroyAllWindows()
2021-02-04 22:41:59 +00:00
if thief:
Upload_Thief.upload_thief_face()
2021-02-02 00:51:23 +00:00
if __name__ == "__main__":
2021-02-02 00:51:23 +00:00
inference()