From 494e414949ce47ae421666e8297875d8cf920365 Mon Sep 17 00:00:00 2001 From: boltgolt Date: Sat, 10 Feb 2018 16:39:41 +0100 Subject: [PATCH] Moved learn.py to the CLI and added a help page --- cli.py | 8 ++++++ learn.py => cli/add.py | 57 +++++++++++++++++++++++++++++------------- cli/help.py | 14 +++++++++++ utils.py | 27 -------------------- 4 files changed, 62 insertions(+), 44 deletions(-) rename learn.py => cli/add.py (70%) create mode 100644 cli/help.py delete mode 100644 utils.py diff --git a/cli.py b/cli.py index 2c645f1..41a3528 100755 --- a/cli.py +++ b/cli.py @@ -1,7 +1,15 @@ #!/usr/bin/env python3 import sys +if (len(sys.argv) == 1): + import cli.help + sys.exit() + cmd = sys.argv[1] if cmd == "list": import cli.list +elif cmd == "help": + import cli.help +elif cmd == "add": + import cli.add diff --git a/learn.py b/cli/add.py similarity index 70% rename from learn.py rename to cli/add.py index 9864095..e3bd9b1 100644 --- a/learn.py +++ b/cli/add.py @@ -1,24 +1,33 @@ # Save the face of the user in encoded form # Import required modules -import face_recognition import subprocess import time import os import sys import json -# Import config and extra functions +# Import config import configparser -import utils + +try: + import face_recognition +except ImportError as err: + print(err) + + print("\nCan't import face_recognition module, check the output of") + print("pip3 show face_recognition") + sys.exit() + +path = os.path.dirname(os.path.abspath(__file__)) # Read config from disk config = configparser.ConfigParser() -config.read(os.path.dirname(os.path.abspath(__file__)) + "/config.ini") +config.read(path + "/../config.ini") def captureFrame(delay): """Capture and encode 1 frame of video""" - global encodings + global insert_model # Call fswebcam to save a frame to /tmp with a set delay exit_code = subprocess.call(["fswebcam", "-S", str(delay), "--no-banner", "-d", "/dev/video" + str(config.get("video", "device_id")), tmp_file]) @@ -54,36 +63,48 @@ def captureFrame(delay): for point in enc[0]: clean_enc.append(point) - encodings.append(clean_enc) + insert_model["data"].append(clean_enc) # The current user user = os.environ.get("USER") # The name of the tmp frame file to user tmp_file = "/tmp/howdy_" + user + ".jpg" # The permanent file to store the encoded model in -enc_file = "./models/" + user + ".dat" +enc_file = path + "/../models/" + user + ".dat" # Known encodings encodings = [] # Make the ./models folder if it doesn't already exist -if not os.path.exists("models"): +if not os.path.exists(path + "/../models"): print("No face model folder found, creating one") - os.makedirs("models") + os.makedirs(path + "/../models") # To try read a premade encodings file if it exists try: encodings = json.load(open(enc_file)) except FileNotFoundError: - encodings = False - -# If a file does exist, ask the user what needs to be done -if encodings != False: - encodings = utils.print_menu(encodings) -else: encodings = [] -print("\nLearning face for the user account " + user) -print("Please look straight into the camera for 5 seconds") +print("Adding face model for the user account " + user) + +label = "Initial model" + +if len(encodings) > 0: + label = "Model #" + str(len(encodings) + 1) + +label_in = input("Enter a label for this new model [" + label + "]: ") + +if label_in != "": + label = label_in + +insert_model = { + "time": int(time.time()), + "label": label, + "id": len(encodings), + "data": [] +} + +print("\nPlease look straight into the camera for 5 seconds") # Give the user time to read time.sleep(2) @@ -93,6 +114,8 @@ for delay in [30, 6, 0]: time.sleep(.3) captureFrame(delay) +encodings.append(insert_model) + # Save the new encodings to disk with open(enc_file, "w") as datafile: json.dump(encodings, datafile) diff --git a/cli/help.py b/cli/help.py new file mode 100644 index 0000000..f8bdbd7 --- /dev/null +++ b/cli/help.py @@ -0,0 +1,14 @@ +print("Howdy IR face recognition\n") + +print("Usage:") +print(" howdy [argument]\n") + +print("Commands:") +print(" help Show this help page") +print(" list List all saved face models for the current user") +print(" add Add a new face model for the current user") +print(" remove [id] Remove a specific model") +print(" clear Remove all face models for the current user") + +print("\nFor support please visit") +print("https://github.com/Boltgolt/howdy") diff --git a/utils.py b/utils.py deleted file mode 100644 index caefdae..0000000 --- a/utils.py +++ /dev/null @@ -1,27 +0,0 @@ -# Useful support functions - -import sys - -def print_menu(encodings): - """Show a menu asking the user what he wants to do""" - 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)