Add `--face-confidence-threshold`

This commit is contained in:
slashtechno 2023-10-27 11:23:44 -05:00
parent eedc2783c9
commit 32d523b727
Signed by: slashtechno
GPG Key ID: 8EC1D9D9286C2B17
3 changed files with 19 additions and 10 deletions

View File

@ -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

View File

@ -20,7 +20,9 @@ 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",
@ -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"]

View File

@ -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