diff --git a/howdy/src/cli/add.py b/howdy/src/cli/add.py index 19e8b8c..0c64f2c 100644 --- a/howdy/src/cli/add.py +++ b/howdy/src/cli/add.py @@ -9,6 +9,7 @@ import configparser import builtins import numpy as np import paths +import paths_factory from recorders.video_capture import VideoCapture from i18n import _ @@ -28,7 +29,7 @@ except ImportError as err: import cv2 # Test if at lest 1 of the data files is there and abort if it's not -if not os.path.isfile(paths.dlib_data_dir / "shape_predictor_5_face_landmarks.dat"): +if not os.path.isfile(paths_factory.shape_predictor_5_face_landmarks_path()): print(_("Data files have not been downloaded, please run the following commands:")) print("\n\tcd " + paths.dlib_data_dir) print("\tsudo ./install.sh\n") @@ -36,20 +37,20 @@ if not os.path.isfile(paths.dlib_data_dir / "shape_predictor_5_face_landmarks.da # Read config from disk config = configparser.ConfigParser() -config.read(paths.config_dir / "config.ini") +config.read(paths_factory.config_file_path()) use_cnn = config.getboolean("core", "use_cnn", fallback=False) if use_cnn: - face_detector = dlib.cnn_face_detection_model_v1(paths.dlib_data_dir / "mmod_human_face_detector.dat") + face_detector = dlib.cnn_face_detection_model_v1(str(paths_factory.mmod_human_face_detector_path())) else: face_detector = dlib.get_frontal_face_detector() -pose_predictor = dlib.shape_predictor(paths.dlib_data_dir / "shape_predictor_5_face_landmarks.dat") -face_encoder = dlib.face_recognition_model_v1(paths.dlib_data_dir / "dlib_face_recognition_resnet_model_v1.dat") +pose_predictor = dlib.shape_predictor(str(paths_factory.shape_predictor_5_face_landmarks_path())) +face_encoder = dlib.face_recognition_model_v1(str(paths_factory.dlib_face_recognition_resnet_model_v1_path())) user = builtins.howdy_user # The permanent file to store the encoded model in -enc_file = paths.user_models_dir / f"{user}.dat" +enc_file = paths_factory.user_model_path(user) # Known encodings encodings = [] diff --git a/howdy/src/cli/clear.py b/howdy/src/cli/clear.py index c5a48b6..2949eef 100644 --- a/howdy/src/cli/clear.py +++ b/howdy/src/cli/clear.py @@ -5,6 +5,7 @@ import os import sys import builtins import paths +import paths_factory from i18n import _ @@ -17,7 +18,7 @@ if not os.path.exists(paths.user_models_dir): sys.exit(1) # Check if the user has a models file to delete -if not os.path.isfile(paths.user_models_dir / f"{user}.dat"): +if not os.path.isfile(paths_factory.user_model_path(user)): print(_("{} has no models or they have been cleared already").format(user)) sys.exit(1) @@ -33,5 +34,5 @@ if not builtins.howdy_args.y: sys.exit(1) # Delete otherwise -os.remove(paths.user_models_dir / f"{user}.dat") +os.remove(paths_factory.user_model_path(user)) print(_("\nModels cleared")) diff --git a/howdy/src/cli/config.py b/howdy/src/cli/config.py index c298099..34860e8 100644 --- a/howdy/src/cli/config.py +++ b/howdy/src/cli/config.py @@ -4,6 +4,7 @@ import os import subprocess import paths +import paths_factory from i18n import _ @@ -20,4 +21,4 @@ elif os.path.isfile("/etc/alternatives/editor"): editor = "/etc/alternatives/editor" # Open the editor as a subprocess and fork it -subprocess.call([editor, paths.config_dir / "config.ini"]) +subprocess.call([editor, paths_factory.config_file_path()]) diff --git a/howdy/src/cli/disable.py b/howdy/src/cli/disable.py index ac98ea0..42b2d3c 100644 --- a/howdy/src/cli/disable.py +++ b/howdy/src/cli/disable.py @@ -7,11 +7,12 @@ import builtins import fileinput import configparser import paths +import paths_factory from i18n import _ # Get the absolute filepath -config_path = paths.config_dir.parent / "config.ini" +config_path = paths_factory.config_file_path() # Read config from disk config = configparser.ConfigParser() diff --git a/howdy/src/cli/list.py b/howdy/src/cli/list.py index 2e5073a..6ed00f3 100644 --- a/howdy/src/cli/list.py +++ b/howdy/src/cli/list.py @@ -7,6 +7,7 @@ import json import time import builtins import paths +import paths_factory from i18n import _ @@ -19,7 +20,7 @@ if not os.path.exists(paths.user_models_dir): sys.exit(1) # Path to the models file -enc_file = paths.user_models_dir / f"{user}.dat" +enc_file = paths_factory.user_model_path(user) # Try to load the models file and abort if the user does not have it yet try: diff --git a/howdy/src/cli/remove.py b/howdy/src/cli/remove.py index d4df673..799b629 100644 --- a/howdy/src/cli/remove.py +++ b/howdy/src/cli/remove.py @@ -6,6 +6,7 @@ import os import json import builtins import paths +import paths_factory from i18n import _ @@ -27,7 +28,7 @@ if not os.path.exists(paths.user_models_dir): sys.exit(1) # Path to the models file -enc_file = paths.user_models_dir / f"{user}.dat" +enc_file = paths_factory.user_model_path(user) # Try to load the models file and abort if the user does not have it yet try: diff --git a/howdy/src/cli/set.py b/howdy/src/cli/set.py index a4184b1..eddad43 100644 --- a/howdy/src/cli/set.py +++ b/howdy/src/cli/set.py @@ -6,11 +6,12 @@ import os import builtins import fileinput import paths +import paths_factory from i18n import _ # Get the absolute filepath -config_path = paths.config_dir / "/config.ini" +config_path = paths_factory.config_file_path() # Check if enough arguments have been passed if len(builtins.howdy_args.arguments) < 2: diff --git a/howdy/src/cli/snap.py b/howdy/src/cli/snap.py index e16187e..ff5dfa4 100644 --- a/howdy/src/cli/snap.py +++ b/howdy/src/cli/snap.py @@ -6,13 +6,14 @@ import configparser import datetime import snapshot import paths +import paths_factory from recorders.video_capture import VideoCapture from i18n import _ # Read the config config = configparser.ConfigParser() -config.read(paths.config_dir / "config.ini") +config.read(paths_factory.config_file_path()) # Start video capture video_capture = VideoCapture(config) diff --git a/howdy/src/cli/test.py b/howdy/src/cli/test.py index 68477ed..b7c02a9 100644 --- a/howdy/src/cli/test.py +++ b/howdy/src/cli/test.py @@ -11,16 +11,14 @@ import dlib import cv2 import numpy as np import paths +import paths_factory from i18n import _ from recorders.video_capture import VideoCapture -# The absolute path to the config directory -path = "/etc/howdy" - # Read config from disk config = configparser.ConfigParser() -config.read(paths.config_dir / "config.ini") +config.read(paths_factory.config_file_path()) if config.get("video", "recording_plugin", fallback="opencv") != "opencv": print(_("Howdy has been configured to use a recorder which doesn't support the test command yet, aborting")) @@ -60,20 +58,20 @@ use_cnn = config.getboolean('core', 'use_cnn', fallback=False) if use_cnn: face_detector = dlib.cnn_face_detection_model_v1( - paths.dlib_data_dir / "mmod_human_face_detector.dat" + paths_factory.mmod_human_face_detector_path() ) else: face_detector = dlib.get_frontal_face_detector() -pose_predictor = dlib.shape_predictor(paths.dlib_data_dir / "shape_predictor_5_face_landmarks.dat") -face_encoder = dlib.face_recognition_model_v1(paths.dlib_data_dir / "dlib_face_recognition_resnet_model_v1.dat") +pose_predictor = dlib.shape_predictor(paths_factory.shape_predictor_5_face_landmarks_path()) +face_encoder = dlib.face_recognition_model_v1(paths_factory.dlib_face_recognition_resnet_model_v1_path()) encodings = [] models = None try: user = builtins.howdy_user - models = json.load(open(paths.user_models_dir / f"{user}.dat")) + models = json.load(open(paths_factory.user_model_path(user))) for model in models: encodings += model["data"] diff --git a/howdy/src/compare.py b/howdy/src/compare.py index 48d3e9f..0878ed8 100644 --- a/howdy/src/compare.py +++ b/howdy/src/compare.py @@ -24,6 +24,7 @@ import snapshot import numpy as np import _thread as thread import paths +import paths_factory # Allow imports from the local howdy folder sys.path.append('/lib/security/howdy') @@ -49,7 +50,7 @@ def init_detector(lock): global face_detector, pose_predictor, face_encoder # Test if at lest 1 of the data files is there and abort if it's not - if not os.path.isfile(paths.dlib_data_dir / "shape_predictor_5_face_landmarks.dat"): + if not os.path.isfile(str(paths_factory.shape_predictor_5_face_landmarks_path())): print(_("Data files have not been downloaded, please run the following commands:")) print("\n\tcd " + paths.dlib_data_dir) print("\tsudo ./install.sh\n") @@ -58,13 +59,13 @@ def init_detector(lock): # Use the CNN detector if enabled if use_cnn: - face_detector = dlib.cnn_face_detection_model_v1(paths.dlib_data_dir / "mmod_human_face_detector.dat") + face_detector = dlib.cnn_face_detection_model_v1(str(paths_factory.mmod_human_face_detector_path())) else: face_detector = dlib.get_frontal_face_detector() # Start the others regardless - pose_predictor = dlib.shape_predictor(paths.dlib_data_dir / "shape_predictor_5_face_landmarks.dat") - face_encoder = dlib.face_recognition_model_v1(paths.dlib_data_dir / "dlib_face_recognition_resnet_model_v1.dat") + pose_predictor = dlib.shape_predictor(str(paths_factory.shape_predictor_5_face_landmarks_path())) + face_encoder = dlib.face_recognition_model_v1(str(paths_factory.dlib_face_recognition_resnet_model_v1_path())) # Note the time it took to initialize detectors timings["ll"] = time.time() - timings["ll"] @@ -127,7 +128,7 @@ face_encoder = None # Try to load the face model from the models folder try: - models = json.load(open(paths.user_models_dir / f"{user}.dat")) + models = json.load(open(paths_factory.user_model_path(user))) for model in models: encodings += model["data"] @@ -140,7 +141,7 @@ if len(models) < 1: # Read config from disk config = configparser.ConfigParser() -config.read(paths.config_dir / "config.ini") +config.read(paths_factory.config_file_path()) # Get all config values needed use_cnn = config.getboolean("core", "use_cnn", fallback=False) diff --git a/howdy/src/paths_factory.py b/howdy/src/paths_factory.py new file mode 100644 index 0000000..7a0d1ba --- /dev/null +++ b/howdy/src/paths_factory.py @@ -0,0 +1,36 @@ +from pathlib import PurePath +import paths + +models = [ + "shape_predictor_5_face_landmarks.dat", + "mmod_human_face_detector.dat", + "dlib_face_recognition_resnet_model_v1.dat", +] + + +def shape_predictor_5_face_landmarks_path() -> PurePath: + return paths.dlib_data_dir / models[0] + + +def mmod_human_face_detector_path() -> PurePath: + return paths.dlib_data_dir / models[1] + + +def dlib_face_recognition_resnet_model_v1_path() -> PurePath: + return paths.dlib_data_dir / models[2] + + +def user_model_path(user: str) -> PurePath: + return paths.user_models_dir / f"{user}.dat" + + +def config_file_path() -> PurePath: + return paths.config_dir / "config.ini" + + +def snapshots_dir_path() -> PurePath: + return paths.log_path / "snapshots" + + +def snapshot_path(snapshot: str) -> PurePath: + return snapshots_dir_path() / snapshot diff --git a/howdy/src/snapshot.py b/howdy/src/snapshot.py index 34e3428..53dd7fa 100644 --- a/howdy/src/snapshot.py +++ b/howdy/src/snapshot.py @@ -5,6 +5,7 @@ import cv2 import os import datetime import numpy as np +import paths_factory def generate(frames, text_lines): @@ -50,15 +51,13 @@ def generate(frames, text_lines): line_number += 1 # Made sure a snapshot folder exist - if not os.path.exists(paths.log_path): - os.makedirs(paths.log_path) - if not os.path.exists(paths.log_path / "snapshots"): - os.makedirs(paths.log_path / "snapshots") + if not os.path.exists(paths_factory.snapshots_dir_path()): + os.makedirs(paths_factory.snapshots_dir_path()) # Generate a filename based on the current time filename = datetime.datetime.utcnow().strftime("%Y%m%dT%H%M%S.jpg") # Write the image to that file - cv2.imwrite(paths.log_path / "snapshots" / filename, snap) + cv2.imwrite(paths_factory.snapshot_path(filename), snap) # Return the saved file location - return paths.log_path / "/snapshots/" / filename + return paths_factory.snapshot_path(filename)