Commit 5d0be5d9 authored by Kyle Anderson's avatar Kyle Anderson

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.
parent 6dd51d98
...@@ -33,8 +33,10 @@ def determine_identity(face_encoding, known_faces) -> str: ...@@ -33,8 +33,10 @@ def determine_identity(face_encoding, known_faces) -> str:
recognized_users = {} recognized_users = {}
for (user_id, user_encodings) in known_faces.items(): for (user_id, user_encodings) in known_faces.items():
matches = face_recognition.compare_faces(user_encodings, face_encoding) matches = face_recognition.compare_faces(user_encodings, face_encoding)
# Count the number of occurrences of true. count = matches.count(True)
recognized_users[user_id] = matches.count(True) if count > 0:
# Count the number of occurrences of true.
recognized_users[user_id] = count
matched_user = "" matched_user = ""
if len(recognized_users) > 0: if len(recognized_users) > 0:
...@@ -70,8 +72,37 @@ def draw_rectangles_and_user_ids(image_frame, conversion: float, box_user_id_map ...@@ -70,8 +72,37 @@ def draw_rectangles_and_user_ids(image_frame, conversion: float, box_user_id_map
common.display_frame(image_frame) 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, 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. """Attempts to recognize a user.
Returns the ID of the user if identified, or None if no users are identified.""" Returns the ID of the user if identified, or None if no users are identified."""
recognized_users_count = {} recognized_users_count = {}
...@@ -86,30 +117,12 @@ def recognize_user(known_faces: dict, encoding_model: str = "hog", image_flip: i ...@@ -86,30 +117,12 @@ def recognize_user(known_faces: dict, encoding_model: str = "hog", image_flip: i
if image_flip is not None: if image_flip is not None:
image_frame = cv2.flip(image_frame, image_flip) image_frame = cv2.flip(image_frame, image_flip)
# Convert input from BGR to RGB recognized_user_ids = run_face_recognition(image_frame, known_faces, encoding_model=encoding_model,
cv2.cvtColor(image_frame, cv2.COLOR_BGR2RGB) draw_rectangles=draw_rectangles)
# Resize image to width of 750 PX to speed up processing. for user_id in recognized_user_ids:
rgb_image = imutils.resize(image_frame, width=750) if user_id not in recognized_users_count:
r = image_frame.shape[1] / float(rgb_image.shape[1]) recognized_users_count[user_id] = 0
recognized_users_count[user_id] += 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)
# Now check if we have already positively identified a user enough times # Now check if we have already positively identified a user enough times
recognized_users = check_recognized_users(recognized_users_count) recognized_users = check_recognized_users(recognized_users_count)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment