From 59e622c17db527c68d33c19f73f2c294c5be03c7 Mon Sep 17 00:00:00 2001 From: dmig Date: Sun, 18 Nov 2018 13:56:52 +0700 Subject: [PATCH] minor code readability changes --- src/cli.py | 2 +- src/cli/add.py | 12 +++++------- src/compare.py | 23 +++++++++++------------ 3 files changed, 17 insertions(+), 20 deletions(-) diff --git a/src/cli.py b/src/cli.py index 7f8a79c..2c5d877 100755 --- a/src/cli.py +++ b/src/cli.py @@ -15,7 +15,7 @@ except: user = os.environ.get("SUDO_USER") # If that fails, try to get the direct user -if user == "root" or user == None: +if user == "root" or user is None: env_user = getpass.getuser().strip() # If even that fails, error out diff --git a/src/cli/add.py b/src/cli/add.py index 243d478..4fe11d8 100644 --- a/src/cli/add.py +++ b/src/cli/add.py @@ -5,9 +5,9 @@ import time import os import sys import json -import cv2 import configparser import builtins +import cv2 # Try to import face_recognition and give a nice error if we can't # Add should be the first point where import issues show up @@ -55,12 +55,12 @@ print("Adding face model for the user " + user) label = "Initial model" # If models already exist, set that default label -if len(encodings) > 0: +if encodings: label = "Model #" + str(len(encodings) + 1) # Keep de default name if we can't ask questions if builtins.howdy_args.y: - print("Using default label \"" + label + "\" because of -y flag") + print('Using default label "%s" because of -y flag' % (label, )) else: # Ask the user for a custom label label_in = input("Enter a label for this new model [" + label + "]: ") @@ -119,11 +119,9 @@ while frames < 60: enc = face_recognition.face_encodings(frame) # If we've found at least one, we can continue - if len(enc) > 0: + if enc: break - -# If 0 faces are detected we can't continue -if len(enc) == 0: +else: print("No face detected, aborting") sys.exit(1) diff --git a/src/compare.py b/src/compare.py index 5b24c22..dca8a88 100644 --- a/src/compare.py +++ b/src/compare.py @@ -42,17 +42,16 @@ dark_tries = 0 # Try to load the face model from the models folder try: models = json.load(open(os.path.dirname(os.path.abspath(__file__)) + "/models/" + user + ".dat")) + + # Put all models together into 1 array + encodings = [model["data"] for model in models] except FileNotFoundError: sys.exit(10) # Check if the file contains a model -if len(models) < 1: +if not encodings: sys.exit(10) -# Put all models together into 1 array -for model in models: - encodings += model["data"] - # Add the time needed to start the script timings.append(time.time()) @@ -153,7 +152,7 @@ while True: if end_report: 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(" %s: %dms" % (label, round((timings[1 + offset] - timings[offset]) * 1000))) print("Time spent") print_timing("Starting up", 0) @@ -162,19 +161,19 @@ while True: print_timing("Searching for known face", 3) print("\nResolution") - print(" Native: " + str(height) + "x" + str(width)) - print(" Used: " + str(scale_height) + "x" + str(scale_width)) + print(" Native: %dx%d" % (height, width)) + print(" Used: %dx%d" % (scale_height, scale_width)) # Show the total number of frames and calculate the FPS by deviding it by the total scan time - print("\nFrames searched: " + str(frames) + " (" + str(round(float(frames) / (timings[4] - timings[3]), 2)) + " fps)") - print("Dark frames ignored: " + str(dark_tries)) - print("Certainty of winning frame: " + str(round(match * 10, 3))) + print("\nFrames searched: %d (%.2f fps)" % (frames, frames / (timings[4] - timings[3]))) + print("Dark frames ignored: %d " % (dark_tries, )) + print("Certainty of winning frame: %.3f" % (match * 10, )) # Catch older 3-encoding models if not match_index in models: match_index = 0 - print("Winning model: " + str(match_index) + " (\"" + models[match_index]["label"] + "\")") + print("Winning model: %d (\"%s\")" % (match_index, models[match_index]["label"])) # End peacefully stop(0)