From 84889c278538b653a6cac6eddda40f32651bbabb Mon Sep 17 00:00:00 2001 From: boltgolt Date: Tue, 13 Feb 2018 22:03:03 +0100 Subject: [PATCH] Added clear command and requred sudo --- cli.py | 20 ++++++++++++++++++-- cli/add.py | 2 +- cli/clear.py | 25 +++++++++++++++++++++++++ cli/help.py | 24 ++++++++++++------------ cli/list.py | 8 ++++---- compare.py | 26 ++++++++++++++++---------- 6 files changed, 76 insertions(+), 29 deletions(-) create mode 100644 cli/clear.py diff --git a/cli.py b/cli.py index 41a3528..3a90180 100755 --- a/cli.py +++ b/cli.py @@ -1,15 +1,31 @@ #!/usr/bin/env python3 import sys +import os -if (len(sys.argv) == 1): +if (len(sys.argv) < 3): + print("Howdy IR face recognition help") import cli.help sys.exit() -cmd = sys.argv[1] +user = sys.argv[1] +cmd = sys.argv[2] + +if cmd in ["list", "add", "remove", "clear"] and os.getenv("SUDO_USER") is None: + print("Please run this command with sudo") + sys.exit() if cmd == "list": import cli.list elif cmd == "help": + print("Howdy IR face recognition") import cli.help elif cmd == "add": import cli.add +elif cmd == "clear": + import cli.clear +else: + if sys.argv[1] in ["list", "add", "remove", "clear", "help"]: + print("Usage: howdy ") + else: + print('Unknown command "' + cmd + '"') + import cli.help diff --git a/cli/add.py b/cli/add.py index f7b3c2a..f4fe2cb 100644 --- a/cli/add.py +++ b/cli/add.py @@ -66,7 +66,7 @@ def captureFrame(delay): insert_model["data"].append(clean_enc) # The current user -user = os.environ.get("USER") +user = sys.argv[1] # The name of the tmp frame file to user tmp_file = "/tmp/howdy_" + user + ".jpg" # The permanent file to store the encoded model in diff --git a/cli/clear.py b/cli/clear.py new file mode 100644 index 0000000..3d12715 --- /dev/null +++ b/cli/clear.py @@ -0,0 +1,25 @@ +import os +import sys + +# Get the full path to this file +path = os.path.dirname(os.path.abspath(__file__)) +# Get the passed user +user = sys.argv[1] + +if not os.path.exists(path + "/../models"): + print("No models created yet, can't clear them if they don't exist") + sys.exit() + +if not os.path.isfile(path + "/../models/" + user + ".dat"): + print(user + " has no models or they have been cleared already") + sys.exit() + +print("This will clear all models for " + user) +ans = input("Do you want to continue [y/N]: ") + +if (ans.lower() != "y"): + print('\nInerpeting as a "NO"') + sys.exit() + +os.remove(path + "/../models/" + user + ".dat") +print("\nModels cleared") diff --git a/cli/help.py b/cli/help.py index f8bdbd7..3b7641e 100644 --- a/cli/help.py +++ b/cli/help.py @@ -1,14 +1,14 @@ -print("Howdy IR face recognition\n") +print(""" +Usage: + howdy [argument] -print("Usage:") -print(" howdy [argument]\n") +Commands: + help Show this help page + list List all saved face models for the current user + add Add a new face model for the current user + remove [id] Remove a specific model + clear Remove all face models for the current user -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") +For support please visit +https://github.com/Boltgolt/howdy\ +""") diff --git a/cli/list.py b/cli/list.py index 78a62aa..64e7480 100644 --- a/cli/list.py +++ b/cli/list.py @@ -4,20 +4,20 @@ import json import time path = os.path.dirname(os.path.realpath(__file__)) + "/.." +user = sys.argv[1] if not os.path.exists(path + "/models"): print("Face models have not been initialized yet, please run:") - print("\n\thowdy add\n") + print("\n\thowdy " + user + " add\n") sys.exit(1) -user = os.environ.get("USER") enc_file = path + "/models/" + user + ".dat" try: encodings = json.load(open(enc_file)) except FileNotFoundError: - print("No face model known for the current user (" + user + "), please run:") - print("\n\thowdy add\n") + print("No face model known for the user " + user + ", please run:") + print("\n\thowdy " + user + " add\n") sys.exit(1) print("Known face models for " + user + ":") diff --git a/compare.py b/compare.py index a3b53e2..d075aa2 100644 --- a/compare.py +++ b/compare.py @@ -13,10 +13,6 @@ import configparser # Start timing timings = [time.time()] -# Import face recognition, takes some time -import face_recognition -timings.append(time.time()) - # Read config from disk config = configparser.ConfigParser() config.read(os.path.dirname(os.path.abspath(__file__)) + "/config.ini") @@ -56,6 +52,11 @@ if len(models) < 1: for model in models: encodings += model["data"] +# Import face recognition, takes some time +timings.append(time.time()) +import face_recognition +timings.append(time.time()) + # Start video capture on the IR camera video_capture = cv2.VideoCapture(int(config.get("video", "device_id"))) timings.append(time.time()) @@ -90,13 +91,18 @@ while True: if config.get("debug", "end_report") == "true": print("DEBUG END REPORT\n") - print("Time spend") - print(" Importing face_recognition: " + str(round((timings[1] - timings[0]) * 1000)) + "ms") - print(" Opening the camera: " + str(round((timings[2] - timings[1]) * 1000)) + "ms") - print(" Searching for known face: " + str(round((timings[3] - timings[2]) * 1000)) + "ms\n") + def print_timing(label, offset): + """Helper function to print a timing from the list""" + print(" " + label + ": " + str(round((timings[1 + offset] - timings[offset]) * 1000)) + "ms") - print("Frames searched: " + str(frames)) - print("Certainty winning frame: " + str(round(match * 10, 3))) + print("Time spend") + print_timing("Starting up", 0) + print_timing("Importing face_recognition", 1) + print_timing("Opening the camera", 2) + print_timing("Searching for known face", 3) + + print("\nFrames searched: " + str(frames) + " (" + str(round(float(frames) / (timings[4] - timings[2]), 2)) + " fps)") + print("Certainty of winning frame: " + str(round(match * 10, 3))) exposures = ["long", "medium", "short"] model_id = math.floor(float(match_index) / 3)