Switched from JPG ref to JSON ref

This commit is contained in:
boltgolt 2018-01-05 14:34:18 +01:00
parent 4d3d0d4701
commit 9c15010107
4 changed files with 77 additions and 23 deletions

View file

@ -2,6 +2,7 @@ import face_recognition
import cv2 import cv2
import sys import sys
import os import os
import json
import config import config
@ -16,20 +17,18 @@ except IndexError:
sys.exit(1) sys.exit(1)
user = sys.argv[1] user = sys.argv[1]
# Get a reference to webcam #0 (the default one)
video_capture = cv2.VideoCapture(config.device_id)
encodings = [] encodings = []
tries = 0
try: try:
for exposure in ["L", "M", "S"]: encodings = json.load(open(os.path.dirname(__file__) + "/models/lem.dat"))
ref = face_recognition.load_image_file(os.path.dirname(__file__) + "/models/" + user + "/" + exposure + ".jpg")
enc = face_recognition.face_encodings(ref)[0]
encodings.append(enc)
except FileNotFoundError: except FileNotFoundError:
stop(10) stop(10)
tries = 0 if len(encodings) < 3:
stop(1)
video_capture = cv2.VideoCapture(config.device_id)
while True: while True:
# Grab a single frame of video # Grab a single frame of video

View file

@ -1,29 +1,62 @@
import face_recognition
import subprocess import subprocess
import time import time
import os import os
import sys
import json
import config
import utils
def captureFrame(delay):
subprocess.call(["fswebcam", "-S", str(delay), "--no-banner", "-d", "/dev/video" + str(config.device_id), tmp_file], stderr=open(os.devnull, "wb"))
ref = face_recognition.load_image_file(tmp_file)
enc = face_recognition.face_encodings(ref)
if len(enc) == 0:
print("No face detected, aborting")
sys.exit()
if len(enc) > 1:
print("Multiple faces detected, aborting")
sys.exit()
clean_enc = []
for point in enc[0]:
clean_enc.append(point)
encodings.append(clean_enc)
user = os.environ.get("USER") user = os.environ.get("USER")
tmp_file = "/tmp/howdy_" + user + ".jpg"
enc_file = "./models/" + user + ".dat"
encodings = []
if not os.path.exists("models"): if not os.path.exists("models"):
print("No face model folder found, creating one")
os.makedirs("models") os.makedirs("models")
if not os.path.exists("models/" + user): try:
print("No face model folder found, creating one") encodings = json.load(open(enc_file))
os.makedirs("models/" + user) except FileNotFoundError:
encodings = False
print("Learning face for the user account " + os.environ.get("USER")) if encodings != False:
print("Please look straigt into the camera for 5 seconds") encodings = utils.print_menu(encodings)
time.sleep(2.5) print("\nLearning face for the user account " + user)
print("Please look straight into the camera for 5 seconds")
subprocess.call(["fswebcam", "-S", "30", "--no-banner", "-d", "/dev/video1", "./models/" + user + "/L.jpg"], stderr=open(os.devnull, "wb")) time.sleep(2)
time.sleep(.3) for delay in [30, 6, 0]:
time.sleep(.3)
captureFrame(delay)
subprocess.call(["fswebcam", "-S", "6", "--no-banner", "-d", "/dev/video1", "./models/" + user + "/M.jpg"], stderr=open(os.devnull, "wb")) with open(enc_file, "w") as datafile:
json.dump(encodings, datafile)
time.sleep(.3) os.remove(tmp_file)
subprocess.call(["fswebcam", "--no-banner", "-d", "/dev/video1", "./models/" + user + "/S.jpg"], stderr=open(os.devnull, "wb"))
print("Done.") print("Done.")

6
pam.py
View file

@ -3,10 +3,10 @@ import sys
import os import os
def doAuth(pamh): def doAuth(pamh):
status = subprocess.call(["python3", "/compair.py", pamh.get_user()]) status = subprocess.call(["python3", os.path.dirname(__file__) + "/compair.py", pamh.get_user()])
if status == 10: if status == 10:
print("No face model is known for this user, aborting") print("No face model is known for this user, skiping")
return pamh.PAM_SYSTEM_ERR return pamh.PAM_SYSTEM_ERR
if status == 11: if status == 11:
print("Timeout reached, ould not find a known face") print("Timeout reached, ould not find a known face")
@ -15,7 +15,7 @@ def doAuth(pamh):
print("Identified face as " + os.environ.get("USER")) print("Identified face as " + os.environ.get("USER"))
return pamh.PAM_SUCCESS return pamh.PAM_SUCCESS
print(status) print("Unknown error: " + str(status))
return pamh.PAM_SYSTEM_ERR return pamh.PAM_SYSTEM_ERR
def pam_sm_authenticate(pamh, flags, args): def pam_sm_authenticate(pamh, flags, args):

22
utils.py Normal file
View file

@ -0,0 +1,22 @@
def print_menu(encodings):
if len(encodings) == 3:
print("There is 1 existing face model for this user")
else:
print("There are " + str(int(len(encodings) / 3)) + " existing face models for this user")
print("What do you want to do?\n")
print("1: Add additional face model")
print("2: Overwrite older model(s)")
print("0: Exit")
com = input("Option: ")
if com == "1":
return encodings
elif com == "2":
return []
elif com == "0":
sys.exit()
else:
print("Invalid option '" + com + "'\n")
return print_menu(encodings)