0
0
Fork 0
mirror of https://github.com/boltgolt/howdy.git synced 2024-09-19 09:51:19 +02:00

Put all path variables into a separate module.

This makes it easier for downstream packagers to customize where howdy installs
its files.
This commit is contained in:
Gleb Popov 2023-03-30 21:55:11 +03:00 committed by Sayafdine Said
parent c17a834a52
commit 488176adae
No known key found for this signature in database
GPG key ID: 7567D43648C6E2F4
12 changed files with 61 additions and 56 deletions

View file

@ -8,6 +8,7 @@ import json
import configparser import configparser
import builtins import builtins
import numpy as np import numpy as np
import paths
from recorders.video_capture import VideoCapture from recorders.video_capture import VideoCapture
from i18n import _ from i18n import _
@ -26,39 +27,36 @@ except ImportError as err:
# OpenCV needs to be imported after dlib # OpenCV needs to be imported after dlib
import cv2 import cv2
# Define the absolute path to the config directory
config_path = "/etc/howdy"
# Test if at lest 1 of the data files is there and abort if it's not # Test if at lest 1 of the data files is there and abort if it's not
if not os.path.isfile(config_path + "/dlib-data/shape_predictor_5_face_landmarks.dat"): if not os.path.isfile(paths.dlib_data_dir + "shape_predictor_5_face_landmarks.dat"):
print(_("Data files have not been downloaded, please run the following commands:")) print(_("Data files have not been downloaded, please run the following commands:"))
print("\n\tcd " + config_path + "/dlib-data") print("\n\tcd " + paths.dlib_data_dir)
print("\tsudo ./install.sh\n") print("\tsudo ./install.sh\n")
sys.exit(1) sys.exit(1)
# Read config from disk # Read config from disk
config = configparser.ConfigParser() config = configparser.ConfigParser()
config.read(config_path + "/config.ini") config.read(paths.config_dir + "/config.ini")
use_cnn = config.getboolean("core", "use_cnn", fallback=False) use_cnn = config.getboolean("core", "use_cnn", fallback=False)
if use_cnn: if use_cnn:
face_detector = dlib.cnn_face_detection_model_v1(config_path + "/dlib-data/mmod_human_face_detector.dat") face_detector = dlib.cnn_face_detection_model_v1(paths.dlib_data_dir + "mmod_human_face_detector.dat")
else: else:
face_detector = dlib.get_frontal_face_detector() face_detector = dlib.get_frontal_face_detector()
pose_predictor = dlib.shape_predictor(config_path + "/dlib-data/shape_predictor_5_face_landmarks.dat") pose_predictor = dlib.shape_predictor(paths.dlib_data_dir + "shape_predictor_5_face_landmarks.dat")
face_encoder = dlib.face_recognition_model_v1(config_path + "/dlib-data/dlib_face_recognition_resnet_model_v1.dat") face_encoder = dlib.face_recognition_model_v1(paths.dlib_data_dir + "dlib_face_recognition_resnet_model_v1.dat")
user = builtins.howdy_user user = builtins.howdy_user
# The permanent file to store the encoded model in # The permanent file to store the encoded model in
enc_file = config_path + "/models/" + user + ".dat" enc_file = paths.user_models_dir + user + ".dat"
# Known encodings # Known encodings
encodings = [] encodings = []
# Make the ./models folder if it doesn't already exist # Make the ./models folder if it doesn't already exist
if not os.path.exists(config_path + "/models"): if not os.path.exists(paths.user_models_dir):
print(_("No face model folder found, creating one")) print(_("No face model folder found, creating one"))
os.makedirs(config_path + "/models") os.makedirs(paths.user_models_dir)
# To try read a premade encodings file if it exists # To try read a premade encodings file if it exists
try: try:

View file

@ -4,21 +4,20 @@
import os import os
import sys import sys
import builtins import builtins
import paths
from i18n import _ from i18n import _
# Get the full path to this file
path = "/etc/howdy/models"
# Get the passed user # Get the passed user
user = builtins.howdy_user user = builtins.howdy_user
# Check if the models folder is there # Check if the models folder is there
if not os.path.exists(path): if not os.path.exists(paths.user_models_dir):
print(_("No models created yet, can't clear them if they don't exist")) print(_("No models created yet, can't clear them if they don't exist"))
sys.exit(1) sys.exit(1)
# Check if the user has a models file to delete # Check if the user has a models file to delete
if not os.path.isfile(path + "/" + user + ".dat"): if not os.path.isfile(paths.user_models_dir + user + ".dat"):
print(_("{} has no models or they have been cleared already").format(user)) print(_("{} has no models or they have been cleared already").format(user))
sys.exit(1) sys.exit(1)
@ -34,5 +33,5 @@ if not builtins.howdy_args.y:
sys.exit(1) sys.exit(1)
# Delete otherwise # Delete otherwise
os.remove(path + "/" + user + ".dat") os.remove(paths.user_models_dir + user + ".dat")
print(_("\nModels cleared")) print(_("\nModels cleared"))

View file

@ -3,6 +3,7 @@
# Import required modules # Import required modules
import os import os
import subprocess import subprocess
import paths
from i18n import _ from i18n import _
@ -19,4 +20,4 @@ elif os.path.isfile("/etc/alternatives/editor"):
editor = "/etc/alternatives/editor" editor = "/etc/alternatives/editor"
# Open the editor as a subprocess and fork it # Open the editor as a subprocess and fork it
subprocess.call([editor, "/etc/howdy/config.ini"]) subprocess.call([editor, paths.config_dir + "config.ini"])

View file

@ -6,11 +6,12 @@ import os
import builtins import builtins
import fileinput import fileinput
import configparser import configparser
import paths
from i18n import _ from i18n import _
# Get the absolute filepath # Get the absolute filepath
config_path = os.path.dirname("/etc/howdy") + "/config.ini" config_path = os.path.dirname(paths.config_dir) + "/config.ini"
# Read config from disk # Read config from disk
config = configparser.ConfigParser() config = configparser.ConfigParser()

View file

@ -6,21 +6,20 @@ import os
import json import json
import time import time
import builtins import builtins
import paths
from i18n import _ from i18n import _
# Get the absolute path and the username
path = "/etc/howdy"
user = builtins.howdy_user user = builtins.howdy_user
# Check if the models file has been created yet # Check if the models file has been created yet
if not os.path.exists(path + "/models"): if not os.path.exists(paths.user_models_dir):
print(_("Face models have not been initialized yet, please run:")) print(_("Face models have not been initialized yet, please run:"))
print("\n\tsudo howdy -U " + user + " add\n") print("\n\tsudo howdy -U " + user + " add\n")
sys.exit(1) sys.exit(1)
# Path to the models file # Path to the models file
enc_file = path + "/models/" + user + ".dat" enc_file = paths.user_models_dir + user + ".dat"
# Try to load the models file and abort if the user does not have it yet # Try to load the models file and abort if the user does not have it yet
try: try:

View file

@ -5,11 +5,10 @@ import sys
import os import os
import json import json
import builtins import builtins
import paths
from i18n import _ from i18n import _
# Get the absolute path and the username
path = "/etc/howdy"
user = builtins.howdy_user user = builtins.howdy_user
# Check if enough arguments have been passed # Check if enough arguments have been passed
@ -22,13 +21,13 @@ if not builtins.howdy_args.arguments:
sys.exit(1) sys.exit(1)
# Check if the models file has been created yet # Check if the models file has been created yet
if not os.path.exists(path + "/models"): if not os.path.exists(paths.user_models_dir):
print(_("Face models have not been initialized yet, please run:")) print(_("Face models have not been initialized yet, please run:"))
print("\n\thowdy add\n") print("\n\thowdy add\n")
sys.exit(1) sys.exit(1)
# Path to the models file # Path to the models file
enc_file = path + "/models/" + user + ".dat" enc_file = paths.user_models_dir + user + ".dat"
# Try to load the models file and abort if the user does not have it yet # Try to load the models file and abort if the user does not have it yet
try: try:
@ -72,7 +71,7 @@ if not found:
# Remove the entire file if this encoding is the only one # Remove the entire file if this encoding is the only one
if len(encodings) == 1: if len(encodings) == 1:
os.remove(path + "/models/" + user + ".dat") os.remove(paths.user_models_dir + user + ".dat")
print(_("Removed last model, howdy disabled for user")) print(_("Removed last model, howdy disabled for user"))
else: else:
# A place holder to contain the encodings that will remain # A place holder to contain the encodings that will remain

View file

@ -5,11 +5,12 @@ import sys
import os import os
import builtins import builtins
import fileinput import fileinput
import paths
from i18n import _ from i18n import _
# Get the absolute filepath # Get the absolute filepath
config_path = os.path.dirname("/etc/howdy") + "/config.ini" config_path = os.path.dirname(paths.config_dir) + "/config.ini"
# Check if enough arguments have been passed # Check if enough arguments have been passed
if len(builtins.howdy_args.arguments) < 2: if len(builtins.howdy_args.arguments) < 2:

View file

@ -5,15 +5,14 @@ import os
import configparser import configparser
import datetime import datetime
import snapshot import snapshot
import paths
from recorders.video_capture import VideoCapture from recorders.video_capture import VideoCapture
from i18n import _ from i18n import _
path = "/etc/howdy"
# Read the config # Read the config
config = configparser.ConfigParser() config = configparser.ConfigParser()
config.read(path + "/config.ini") config.read(paths.config_dir + "config.ini")
# Start video capture # Start video capture
video_capture = VideoCapture(config) video_capture = VideoCapture(config)

View file

@ -10,6 +10,7 @@ import time
import dlib import dlib
import cv2 import cv2
import numpy as np import numpy as np
import paths
from i18n import _ from i18n import _
from recorders.video_capture import VideoCapture from recorders.video_capture import VideoCapture
@ -19,7 +20,7 @@ path = "/etc/howdy"
# Read config from disk # Read config from disk
config = configparser.ConfigParser() config = configparser.ConfigParser()
config.read(path + "/config.ini") config.read(paths.config_dir + "config.ini")
if config.get("video", "recording_plugin", fallback="opencv") != "opencv": 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")) print(_("Howdy has been configured to use a recorder which doesn't support the test command yet, aborting"))
@ -59,20 +60,20 @@ use_cnn = config.getboolean('core', 'use_cnn', fallback=False)
if use_cnn: if use_cnn:
face_detector = dlib.cnn_face_detection_model_v1( face_detector = dlib.cnn_face_detection_model_v1(
path + "/dlib-data/mmod_human_face_detector.dat" paths.dlib_data_dir + "mmod_human_face_detector.dat"
) )
else: else:
face_detector = dlib.get_frontal_face_detector() face_detector = dlib.get_frontal_face_detector()
pose_predictor = dlib.shape_predictor(path + "/dlib-data/shape_predictor_5_face_landmarks.dat") pose_predictor = dlib.shape_predictor(paths.dlib_data_dir + "shape_predictor_5_face_landmarks.dat")
face_encoder = dlib.face_recognition_model_v1(path + "/dlib-data/dlib_face_recognition_resnet_model_v1.dat") face_encoder = dlib.face_recognition_model_v1(paths.dlib_data_dir + "dlib_face_recognition_resnet_model_v1.dat")
encodings = [] encodings = []
models = None models = None
try: try:
user = builtins.howdy_user user = builtins.howdy_user
models = json.load(open(path + "/models/" + user + ".dat")) models = json.load(open(paths.user_models_dir + user + ".dat"))
for model in models: for model in models:
encodings += model["data"] encodings += model["data"]

View file

@ -23,6 +23,7 @@ import subprocess
import snapshot import snapshot
import numpy as np import numpy as np
import _thread as thread import _thread as thread
import paths
# Allow imports from the local howdy folder # Allow imports from the local howdy folder
sys.path.append('/lib/security/howdy') sys.path.append('/lib/security/howdy')
@ -48,22 +49,22 @@ def init_detector(lock):
global face_detector, pose_predictor, face_encoder global face_detector, pose_predictor, face_encoder
# Test if at lest 1 of the data files is there and abort if it's not # Test if at lest 1 of the data files is there and abort if it's not
if not os.path.isfile(PATH + "/dlib-data/shape_predictor_5_face_landmarks.dat"): if not os.path.isfile(paths.dlib_data_dir + "shape_predictor_5_face_landmarks.dat"):
print(_("Data files have not been downloaded, please run the following commands:")) print(_("Data files have not been downloaded, please run the following commands:"))
print("\n\tcd " + PATH + "/dlib-data") print("\n\tcd " + paths.dlib_data_dir)
print("\tsudo ./install.sh\n") print("\tsudo ./install.sh\n")
lock.release() lock.release()
exit(1) exit(1)
# Use the CNN detector if enabled # Use the CNN detector if enabled
if use_cnn: if use_cnn:
face_detector = dlib.cnn_face_detection_model_v1(PATH + "/dlib-data/mmod_human_face_detector.dat") face_detector = dlib.cnn_face_detection_model_v1(paths.dlib_data_dir + "mmod_human_face_detector.dat")
else: else:
face_detector = dlib.get_frontal_face_detector() face_detector = dlib.get_frontal_face_detector()
# Start the others regardless # Start the others regardless
pose_predictor = dlib.shape_predictor(PATH + "/dlib-data/shape_predictor_5_face_landmarks.dat") pose_predictor = dlib.shape_predictor(paths.dlib_data_dir + "shape_predictor_5_face_landmarks.dat")
face_encoder = dlib.face_recognition_model_v1(PATH + "/dlib-data/dlib_face_recognition_resnet_model_v1.dat") face_encoder = dlib.face_recognition_model_v1(paths.dlib_data_dir + "dlib_face_recognition_resnet_model_v1.dat")
# Note the time it took to initialize detectors # Note the time it took to initialize detectors
timings["ll"] = time.time() - timings["ll"] timings["ll"] = time.time() - timings["ll"]
@ -103,9 +104,6 @@ def send_to_ui(type, message):
if len(sys.argv) < 2: if len(sys.argv) < 2:
exit(12) exit(12)
# Get the absolute path to the config directory
PATH = "/etc/howdy"
# The username of the user being authenticated # The username of the user being authenticated
user = sys.argv[1] user = sys.argv[1]
# The model file contents # The model file contents
@ -129,7 +127,7 @@ face_encoder = None
# Try to load the face model from the models folder # Try to load the face model from the models folder
try: try:
models = json.load(open(PATH + "/models/" + user + ".dat")) models = json.load(open(paths.user_models_dir + user + ".dat"))
for model in models: for model in models:
encodings += model["data"] encodings += model["data"]
@ -142,7 +140,7 @@ if len(models) < 1:
# Read config from disk # Read config from disk
config = configparser.ConfigParser() config = configparser.ConfigParser()
config.read(PATH + "/config.ini") config.read(paths.config_dir + "config.ini")
# Get all config values needed # Get all config values needed
use_cnn = config.getboolean("core", "use_cnn", fallback=False) use_cnn = config.getboolean("core", "use_cnn", fallback=False)

12
howdy/src/paths.py Normal file
View file

@ -0,0 +1,12 @@
# Define the absolute path to the config directory
config_dir = "/etc/howdy/"
# Define the absolute path to the DLib models data directory
dlib_data_dir = config_dir + "/dlib-data/"
# Define the absolute path to the Howdy user models directory
user_models_dir = config_dir + "/models/"
# Define path to any howdy logs
log_path = "/var/log/howdy"

View file

@ -49,19 +49,16 @@ def generate(frames, text_lines):
line_number += 1 line_number += 1
# Define path to any howdy logs
log_path = "/var/log/howdy"
# Made sure a snapshot folder exist # Made sure a snapshot folder exist
if not os.path.exists(log_path): if not os.path.exists(paths.log_path):
os.makedirs(log_path) os.makedirs(paths.log_path)
if not os.path.exists(log_path + "/snapshots"): if not os.path.exists(paths.log_path + "/snapshots"):
os.makedirs(log_path + "/snapshots") os.makedirs(paths.log_path + "/snapshots")
# Generate a filename based on the current time # Generate a filename based on the current time
filename = datetime.datetime.utcnow().strftime("%Y%m%dT%H%M%S.jpg") filename = datetime.datetime.utcnow().strftime("%Y%m%dT%H%M%S.jpg")
# Write the image to that file # Write the image to that file
cv2.imwrite(log_path + "/snapshots/" + filename, snap) cv2.imwrite(paths.log_path + "/snapshots/" + filename, snap)
# Return the saved file location # Return the saved file location
return log_path + "/snapshots/" + filename return paths.log_path + "/snapshots/" + filename