2018-01-05 16:37:00 +01:00
|
|
|
# Compair incomming video with known faces
|
|
|
|
# Running in a local python instance to get around PATH issues
|
|
|
|
|
|
|
|
# Import required modules
|
2018-01-05 01:59:44 +01:00
|
|
|
import face_recognition
|
|
|
|
import cv2
|
|
|
|
import sys
|
|
|
|
import os
|
2018-01-05 14:34:18 +01:00
|
|
|
import json
|
2018-01-05 01:59:44 +01:00
|
|
|
|
2018-01-05 16:37:00 +01:00
|
|
|
# Import config
|
2018-01-05 02:41:56 +01:00
|
|
|
import config
|
|
|
|
|
2018-01-05 01:59:44 +01:00
|
|
|
def stop(status):
|
2018-01-05 16:37:00 +01:00
|
|
|
"""Stop the execution and close video stream"""
|
2018-01-05 01:59:44 +01:00
|
|
|
video_capture.release()
|
|
|
|
sys.exit(status)
|
|
|
|
|
2018-01-05 16:37:00 +01:00
|
|
|
# Make sure we were given an username to tast against
|
2018-01-05 01:59:44 +01:00
|
|
|
try:
|
|
|
|
if not isinstance(sys.argv[1], str):
|
|
|
|
sys.exit(1)
|
|
|
|
except IndexError:
|
|
|
|
sys.exit(1)
|
|
|
|
|
2018-01-05 16:37:00 +01:00
|
|
|
# The username of the authenticating user
|
2018-01-05 01:59:44 +01:00
|
|
|
user = sys.argv[1]
|
2018-01-05 16:37:00 +01:00
|
|
|
# List of known faces, encoded by face_recognition
|
2018-01-05 01:59:44 +01:00
|
|
|
encodings = []
|
2018-01-05 16:37:00 +01:00
|
|
|
# Amount of frames already matched
|
2018-01-05 14:34:18 +01:00
|
|
|
tries = 0
|
2018-01-05 01:59:44 +01:00
|
|
|
|
2018-01-05 16:37:00 +01:00
|
|
|
# Try to load the face model from the models folder
|
2018-01-05 01:59:44 +01:00
|
|
|
try:
|
2018-01-05 16:07:48 +01:00
|
|
|
encodings = json.load(open(os.path.dirname(__file__) + "/models/" + user + ".dat"))
|
2018-01-05 01:59:44 +01:00
|
|
|
except FileNotFoundError:
|
2018-01-05 02:41:56 +01:00
|
|
|
stop(10)
|
2018-01-05 01:59:44 +01:00
|
|
|
|
2018-01-05 16:37:00 +01:00
|
|
|
# Verify that we have a valid model file
|
2018-01-05 14:34:18 +01:00
|
|
|
if len(encodings) < 3:
|
|
|
|
stop(1)
|
|
|
|
|
2018-01-05 16:37:00 +01:00
|
|
|
# Start video capture on the IR camera
|
2018-01-05 14:34:18 +01:00
|
|
|
video_capture = cv2.VideoCapture(config.device_id)
|
2018-01-05 01:59:44 +01:00
|
|
|
|
|
|
|
while True:
|
|
|
|
# Grab a single frame of video
|
|
|
|
ret, frame = video_capture.read()
|
|
|
|
|
2018-01-05 16:37:00 +01:00
|
|
|
# Get all faces from that frame as encodings
|
2018-01-05 01:59:44 +01:00
|
|
|
face_encodings = face_recognition.face_encodings(frame)
|
|
|
|
|
2018-01-05 16:37:00 +01:00
|
|
|
# Loop through each face
|
2018-01-05 01:59:44 +01:00
|
|
|
for face_encoding in face_encodings:
|
2018-01-05 16:37:00 +01:00
|
|
|
# Match this found face against a known face
|
2018-01-05 01:59:44 +01:00
|
|
|
matches = face_recognition.face_distance(encodings, face_encoding)
|
|
|
|
|
2018-01-05 16:37:00 +01:00
|
|
|
# Check if any match is certain enough to be the user we're looking for
|
2018-01-05 01:59:44 +01:00
|
|
|
for match in matches:
|
2018-01-05 16:37:00 +01:00
|
|
|
if match < config.certainty and match > 0:
|
2018-01-05 01:59:44 +01:00
|
|
|
stop(0)
|
|
|
|
|
2018-01-05 16:37:00 +01:00
|
|
|
# Stop if we've exceded the maximum retry count
|
2018-01-05 02:41:56 +01:00
|
|
|
if tries > config.frame_count:
|
|
|
|
stop(11)
|
2018-01-05 01:59:44 +01:00
|
|
|
|
|
|
|
tries += 1
|