diff --git a/.gitignore b/.gitignore index 7bbc71c..10f2e64 100644 --- a/.gitignore +++ b/.gitignore @@ -99,3 +99,6 @@ ENV/ # mypy .mypy_cache/ + +# Ignore generated models +/models diff --git a/compair.py b/compair.py new file mode 100644 index 0000000..feb16a0 --- /dev/null +++ b/compair.py @@ -0,0 +1,53 @@ +import face_recognition +import cv2 +import sys +import os + +def stop(status): + video_capture.release() + sys.exit(status) + +path = "" +distance = 3 + +try: + if not isinstance(sys.argv[1], str): + sys.exit(1) +except IndexError: + sys.exit(1) + +user = sys.argv[1] + +# Get a reference to webcam #0 (the default one) +video_capture = cv2.VideoCapture(1) + +encodings = [] + +try: + for exposure in ["L", "M", "S"]: + ref = face_recognition.load_image_file(path + "/" + user + "/" + exposure + ".jpg") + enc = face_recognition.face_encodings(ref)[0] + encodings.append(enc) +except FileNotFoundError: + stop(802) + +tries = 0 + +while True: + # Grab a single frame of video + ret, frame = video_capture.read() + + face_encodings = face_recognition.face_encodings(frame) + + # Loop through each face in this frame of video + for face_encoding in face_encodings: + matches = face_recognition.face_distance(encodings, face_encoding) + + for match in matches: + if match < distance: + stop(0) + + if tries => 100: + stop(801) + + tries += 1 diff --git a/learn.py b/learn.py new file mode 100644 index 0000000..c16aa88 --- /dev/null +++ b/learn.py @@ -0,0 +1,29 @@ +import subprocess +import time +import os + +user = os.environ.get("USER") + +if not os.path.exists("models"): + os.makedirs("models") + +if not os.path.exists("models/" + user): + print("No face model folder found, creating one") + os.makedirs("models/" + user) + +print("Learning face for the user account " + os.environ.get("USER")) +print("Please look straigt into the camera for 5 seconds") + +time.sleep(2.5) + +subprocess.call(["fswebcam", "-S", "30", "--no-banner", "-d", "/dev/video1", "./models/" + user + "/L.jpg"], stderr=open(os.devnull, "wb")) + +time.sleep(.3) + +subprocess.call(["fswebcam", "-S", "6", "--no-banner", "-d", "/dev/video1", "./models/" + user + "/M.jpg"], stderr=open(os.devnull, "wb")) + +time.sleep(.3) + +subprocess.call(["fswebcam", "--no-banner", "-d", "/dev/video1", "./models/" + user + "/S.jpg"], stderr=open(os.devnull, "wb")) + +print("Done.") diff --git a/pam.py b/pam.py new file mode 100644 index 0000000..fd2a11c --- /dev/null +++ b/pam.py @@ -0,0 +1,18 @@ +import subprocess +import sys +import os + +def pam_sm_authenticate(pamh, flags, args): + status = subprocess.call(["python3", "compair.py", pamh.get_user()]) + + if status == 801: + print("Timeout reached, ould not find a known face") + return pamh.PAM_SYSTEM_ERR + if status == 34: + print("No face model is known for this user") + return pamh.PAM_SYSTEM_ERR + if status == 0: + print("Identified face as " + os.environ.get("USER")) + return pamh.PAM_SUCCESS + + return pamh.PAM_SYSTEM_ERR