From 32d523b727c583cc78d6d84dde0631d171b87541 Mon Sep 17 00:00:00 2001 From: slashtechno <77907286+slashtechno@users.noreply.github.com> Date: Fri, 27 Oct 2023 11:23:44 -0500 Subject: [PATCH] Add `--face-confidence-threshold` --- wyzely_detect/__main__.py | 4 ++-- wyzely_detect/utils/cli_args.py | 16 +++++++++++++--- wyzely_detect/utils/utils.py | 9 ++++----- 3 files changed, 19 insertions(+), 10 deletions(-) diff --git a/wyzely_detect/__main__.py b/wyzely_detect/__main__.py index d65215c..3c46edd 100644 --- a/wyzely_detect/__main__.py +++ b/wyzely_detect/__main__.py @@ -83,7 +83,7 @@ def main(): # May be better to check every iteration, but this also works if path_to_faces_exists: if face_details := utils.recognize_face( - path_to_directory=path_to_faces, run_frame=run_frame + path_to_directory=path_to_faces, run_frame=run_frame, min_confidence=args.face_confidence_threshold ): plot_boxes.append(face_details) objects_and_peoples = notify.thing_detected( @@ -132,7 +132,7 @@ def main(): # print("---") # Now do stuff (if conf > 0.5) - if conf < args.confidence_threshold or ( + if conf < args.object_confidence_threshold or ( class_id not in args.detect_object and args.detect_object != [] ): # If the confidence is too low diff --git a/wyzely_detect/utils/cli_args.py b/wyzely_detect/utils/cli_args.py index 8964cad..5c064c6 100644 --- a/wyzely_detect/utils/cli_args.py +++ b/wyzely_detect/utils/cli_args.py @@ -20,8 +20,10 @@ def set_argparse(): epilog=":)", ) - # required='RUN_SCALE' not in os.environ, + # One important thing to consider is that most function parameters are optional and have a default value + # However, with argparse, those are never used since a argparse always passes something, even if it's None + argparser.add_argument( "--run-scale", # Set it to the env RUN_SCALE if it isn't blank, otherwise set it to 0.25 @@ -53,7 +55,7 @@ def set_argparse(): ) argparser.add_argument( - "--confidence-threshold", + "--object-confidence-threshold", default=os.environ["CONFIDENCE_THRESHOLD"] if "CONFIDENCE_THRESHOLD" in os.environ and os.environ["CONFIDENCE_THRESHOLD"] != "" @@ -61,7 +63,15 @@ def set_argparse(): type=float, help="The confidence threshold to use", ) - + argparser.add_argument( + "--face-confidence-threshold", + default=os.environ["FACE_CONFIDENCE_THRESHOLD"] + if "FACE_CONFIDENCE_THRESHOLD" in os.environ + and os.environ["FACE_CONFIDENCE_THRESHOLD"] != "" + else 0.3, + type=float, + help="The confidence (currently cosine similarity) threshold to use for face recognition", + ) argparser.add_argument( "--faces-directory", default=os.environ["FACES_DIRECTORY"] diff --git a/wyzely_detect/utils/utils.py b/wyzely_detect/utils/utils.py index 5288359..17592e3 100644 --- a/wyzely_detect/utils/utils.py +++ b/wyzely_detect/utils/utils.py @@ -68,6 +68,7 @@ def recognize_face( path_to_directory: Path = Path("faces"), # opencv image run_frame: np.ndarray = None, + min_confidence: float = 0.3, ) -> np.ndarray: """ Accepts a path to a directory of images of faces to be used as a refference @@ -149,15 +150,13 @@ def recognize_face( "y2": df.iloc[-1]["source_y"] + df.iloc[-1]["source_h"], } # After some brief testing, it seems positive matches are > 0.3 - distance = df.iloc[-1]["ArcFace_cosine"] - # TODO: Make this a CLI argument - if distance < 0.3: + cosine_similarity = df.iloc[-1]["ArcFace_cosine"] + if cosine_similarity < min_confidence: return None - # if 0.5 < distance < 0.7: # label = "Unknown" to_return = dict(label=label, **coordinates) print( - f"Confindence: {distance}, filname: {path_to_image.name}, to_return: {to_return}" + f"Cosine similarity: {cosine_similarity}, filname: {path_to_image.name}, to_return: {to_return}" ) return to_return