0
0
Fork 0
mirror of https://github.com/boltgolt/howdy.git synced 2024-09-19 09:51:19 +02:00
howdy/compair.py

70 lines
1.8 KiB
Python
Raw Normal View History

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
import configparser
2018-01-05 01:59:44 +01:00
# Read config from disk
config = configparser.ConfigParser()
config.read(os.path.dirname(__file__) + "/config.ini")
2018-01-05 02:41:56 +01:00
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:
sys.exit(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:
sys.exit(1)
2018-01-05 14:34:18 +01:00
2018-01-05 16:37:00 +01:00
# Start video capture on the IR camera
video_capture = cv2.VideoCapture(int(config.get("video", "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:
if match < int(config.get("video", "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
if tries > int(config.get("video", "frame_count")):
2018-01-05 02:41:56 +01:00
stop(11)
2018-01-05 01:59:44 +01:00
tries += 1