-
Notifications
You must be signed in to change notification settings - Fork 0
/
face_recognition_system.py
71 lines (59 loc) · 2.43 KB
/
face_recognition_system.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
name = input("What is your name?")
import os
import sys
import cv2
import utils
from keras.models import load_model
import FaceDetection
import collections
# validate that the user has ran the model training process
files = os.listdir()
if 'siamese_nn.h5' not in files:
print("Error: Pre-trained Neural Network not found!")
print("Please run siamese_nn.py first")
sys.exit()
# validate that the user has ran the onboarding process
if 'true_img.png' not in files:
print("Error: True image not found!")
print("Please run onbarding.py first")
sys.exit()
# load pre-trained Siamese neural network
model = load_model('siamese_nn.h5', custom_objects={'contrastive_loss': utils.contrastive_loss, 'euclidean_distance': utils.euclidean_distance})
# prepare the true image obtained during onboard
true_img = cv2.imread('true_img.png', 0)
true_img = true_img.astype('float32')/255
true_img = cv2.resize(true_img, (92, 112))
true_img = true_img.reshape(1, true_img.shape[0], true_img.shape[1], 1)
video_capture = cv2.VideoCapture(0)
preds = collections.deque(maxlen=15)
while True:
# Capture frame-by-frame
_, frame = video_capture.read()
# Detect Faces
frame, face_img, face_coords = FaceDetection.detect_faces(frame, draw_box=False)
if face_img is not None:
face_img = cv2.cvtColor(face_img, cv2.COLOR_BGR2GRAY)
face_img = face_img.astype('float32')/255
face_img = cv2.resize(face_img, (92, 112))
face_img = face_img.reshape(1, face_img.shape[0], face_img.shape[1], 1)
preds.append(1-model.predict([true_img, face_img])[0][0])
x,y,w,h = face_coords
if len(preds) == 15 and sum(preds)/15 >= 0.3:
text = "Identity: {}".format(name)
cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 255, 0), 5)
elif len(preds) < 15:
text = "Identifying ..."
cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 165, 255), 5)
else:
text = "Identity Unknown!"
cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 0, 255), 5)
frame = utils.write_on_frame(frame, text, face_coords[0], face_coords[1]-10)
else:
preds = collections.deque(maxlen=15) # clear existing predictions if no face detected
# Display the resulting frame
cv2.imshow('Video', frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# When everything is done, release the capture
video_capture.release()
cv2.destroyAllWindows()