From 20974697429a2cd1a1d922bdefb9b2c5cc418bfc Mon Sep 17 00:00:00 2001 From: Andrew Villeneuve Date: Mon, 8 Jun 2020 20:55:26 -0700 Subject: [PATCH] Detect and report errors caused by incorrectly set dark_threshold --- src/compare.py | 25 +++++++++++++++++++++++-- src/pam.py | 4 ++++ 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/src/compare.py b/src/compare.py index c316fef..3bc9d03 100644 --- a/src/compare.py +++ b/src/compare.py @@ -66,6 +66,8 @@ user = sys.argv[1] models = [] # Encoded face models encodings = [] +# Amount of ignored 100% black frames +black_tries = 0 # Amount of ingnored dark frames dark_tries = 0 # Total amount of frames captured @@ -170,7 +172,9 @@ end_report = config.getboolean("debug", "end_report") # Start the read loop frames = 0 +valid_frames = 0 timings["fr"] = time.time() +dark_running_total = 0 while True: # Increment the frame count every loop @@ -178,6 +182,11 @@ while True: # Stop if we've exceded the time limit if time.time() - timings["fr"] > timeout: + average_darkness = dark_running_total / frames + if (dark_tries == valid_frames ): + print("All frames were too dark, please check dark_threshold in config") + print("Average darkness: " + str(dark_running_total / valid_frames) + ", Threshold: " + str(dark_threshold)) + stop(13) stop(11) # Grab a single frame of video @@ -202,9 +211,20 @@ while True: # All values combined for percentage calculation hist_total = np.sum(hist) - # If the image is fully black or the frame exceeds threshold, + # Calculate frame darkness + darkness = (hist[0] / hist_total * 100) + + # If the image is fully black due to a bad camera read, # skip to the next frame - if hist_total == 0 or (hist[0] / hist_total * 100 > dark_threshold): + if (hist_total == 0) or (darkness == 100): + black_tries += 1 + continue + + dark_running_total += darkness + valid_frames += 1 + # If the image exceeds darkness threshold due to subject distance, + # skip to the next frame + if (darkness > dark_threshold): dark_tries += 1 continue @@ -263,6 +283,7 @@ while True: # Show the total number of frames and calculate the FPS by deviding it by the total scan time print("\nFrames searched: %d (%.2f fps)" % (frames, frames / timings["fr"])) + print("Black frames ignored: %d " % (black_tries, )) print("Dark frames ignored: %d " % (dark_tries, )) print("Certainty of winning frame: %.3f" % (match * 10, )) diff --git a/src/pam.py b/src/pam.py index 3d44114..ea8ae16 100644 --- a/src/pam.py +++ b/src/pam.py @@ -50,6 +50,10 @@ def doAuth(pamh): # Status 12 means we aborted elif status == 12: return pamh.PAM_AUTH_ERR + # Status 13 means the image was too dark + elif status == 13: + pamh.conversation(pamh.Message(pamh.PAM_ERROR_MSG, "Face detection image too dark")) + return pamh.PAM_AUTH_ERR # Status 0 is a successful exit elif status == 0: # Show the success message if it isn't suppressed