From 5d0be5d9897175de5523067e22b79bcd7306db44 Mon Sep 17 00:00:00 2001 From: Kyle Anderson Date: Sun, 10 Nov 2019 10:57:29 -0500 Subject: [PATCH] Fix problem with authenticate user allowing anyone entry The authenticate user script was letting anyone in, so have now fixed that hopefully. Still awaiting final testing. --- authenticate_user.py | 67 ++++++++++++++++++++++++++------------------ 1 file changed, 40 insertions(+), 27 deletions(-) diff --git a/authenticate_user.py b/authenticate_user.py index 066a3a7..3f3c521 100644 --- a/authenticate_user.py +++ b/authenticate_user.py @@ -33,8 +33,10 @@ def determine_identity(face_encoding, known_faces) -> str: recognized_users = {} for (user_id, user_encodings) in known_faces.items(): matches = face_recognition.compare_faces(user_encodings, face_encoding) - # Count the number of occurrences of true. - recognized_users[user_id] = matches.count(True) + count = matches.count(True) + if count > 0: + # Count the number of occurrences of true. + recognized_users[user_id] = count matched_user = "" if len(recognized_users) > 0: @@ -70,8 +72,37 @@ def draw_rectangles_and_user_ids(image_frame, conversion: float, box_user_id_map common.display_frame(image_frame) +def run_face_recognition(frame, known_faces: dict, encoding_model: str = "hog", draw_rectangles: bool = False) -> list: + recognized_user_ids: list = [] + + # Convert input from BGR to RGB + cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) + # Resize image to width of 750 PX to speed up processing. + rgb_image = imutils.resize(frame, width=750) + r = frame.shape[1] / float(rgb_image.shape[1]) + + # Detect the location of each face and determine the boxes in which they lie + boxes = face_recognition.face_locations( + rgb_image, model=encoding_model) + # Compute the facial embeddings (the encoding) at + # each of the locations found in the previous line. + encodings = face_recognition.face_encodings(rgb_image, boxes) + + box_user_id_mapping = {} + for (i, encoding) in enumerate(encodings): + user_id: str = determine_identity(encoding, known_faces) + if user_id: + box_user_id_mapping[boxes[i]] = user_id + recognized_user_ids.append(user_id) + + if draw_rectangles: + draw_rectangles_and_user_ids(frame, r, box_user_id_mapping) + + return recognized_user_ids + + def recognize_user(known_faces: dict, encoding_model: str = "hog", image_flip: int = None, - draw_rectangles=False): + draw_rectangles: bool = False): """Attempts to recognize a user. Returns the ID of the user if identified, or None if no users are identified.""" recognized_users_count = {} @@ -86,30 +117,12 @@ def recognize_user(known_faces: dict, encoding_model: str = "hog", image_flip: i if image_flip is not None: image_frame = cv2.flip(image_frame, image_flip) - # Convert input from BGR to RGB - cv2.cvtColor(image_frame, cv2.COLOR_BGR2RGB) - # Resize image to width of 750 PX to speed up processing. - rgb_image = imutils.resize(image_frame, width=750) - r = image_frame.shape[1] / float(rgb_image.shape[1]) - - # Detect the location of each face and determine the boxes in which they lie - boxes = face_recognition.face_locations( - rgb_image, model=encoding_model) - # Compute the facial embeddings (the encoding) at - # each of the locations found in the previous line. - encodings = face_recognition.face_encodings(rgb_image, boxes) - - box_user_id_mapping = {} - for (i, encoding) in enumerate(encodings): - user_id: str = determine_identity(encoding, known_faces) - if user_id: - if user_id not in recognized_users_count: - recognized_users_count[user_id] = 0 - recognized_users_count[user_id] += 1 - box_user_id_mapping[boxes[i]] = user_id - - if draw_rectangles: - draw_rectangles_and_user_ids(image_frame, r, box_user_id_mapping) + recognized_user_ids = run_face_recognition(image_frame, known_faces, encoding_model=encoding_model, + draw_rectangles=draw_rectangles) + for user_id in recognized_user_ids: + if user_id not in recognized_users_count: + recognized_users_count[user_id] = 0 + recognized_users_count[user_id] += 1 # Now check if we have already positively identified a user enough times recognized_users = check_recognized_users(recognized_users_count) -- GitLab