Commit a5745728 authored by Kyle Anderson's avatar Kyle Anderson

Small refactorings

Just did some small refactorings on some of the files.
parent 92ff71a8
...@@ -38,21 +38,20 @@ def determine_identity(face_encoding, known_faces): ...@@ -38,21 +38,20 @@ def determine_identity(face_encoding, known_faces):
"""Determines the most likely identity of a single face. Returns the user id.""" """Determines the most likely identity of a single face. Returns the user id."""
matches = face_recognition.compare_faces( matches = face_recognition.compare_faces(
known_faces["encodings"], face_encoding) known_faces["encodings"], face_encoding)
matched_user = None matched_user = ''
matched_user_id_count = {}
# If there is at least one match to a face in the database, figure out which one it is. # If there is at least one match to a face in the database, figure out which one it is.
if True in matches: if True in matches:
matched_user_id_count = {}
matched_users = [user_index for ( matched_users = [user_index for (
user_index, is_match) in enumerate(matches) if is_match] user_index, is_match) in enumerate(matches) if is_match]
for i in matched_users: for i in matched_users:
user_id: str = known_faces["user_ids"][i] user_id: str = known_faces["user_ids"][i]
matched_user_id_count[user_id] = matched_user_id_count.get( matched_user_id_count[user_id] = matched_user_id_count.get(user_id, 0) + 1
user_id, 0) + 1
matched_user: str = max(matched_user_id_count, matched_user: str = max(matched_user_id_count,
keys=matched_user_id_count.get()) key=matched_user_id_count.get)
return matched_user return matched_user
...@@ -65,6 +64,7 @@ def check_recognized_users(recognized_user_counts): ...@@ -65,6 +64,7 @@ def check_recognized_users(recognized_user_counts):
recognized_users.append(user_id) recognized_users.append(user_id)
return recognized_users return recognized_users
def draw_rectanges_and_user_ids(image_frame, conversion: float, boxes, user_ids: list): def draw_rectanges_and_user_ids(image_frame, conversion: float, boxes, user_ids: list):
"""Draws the rectangles and user_ids onto the video stream so anyone viewing the stream could see them.""" """Draws the rectangles and user_ids onto the video stream so anyone viewing the stream could see them."""
for ((top, right, bottom, left), user_id) in zip(boxes, user_ids): for ((top, right, bottom, left), user_id) in zip(boxes, user_ids):
...@@ -80,6 +80,7 @@ def draw_rectanges_and_user_ids(image_frame, conversion: float, boxes, user_ids: ...@@ -80,6 +80,7 @@ def draw_rectanges_and_user_ids(image_frame, conversion: float, boxes, user_ids:
cv2.putText(image_frame, user_id, (left, y), cv2.FONT_HERSHEY_PLAIN, 0.75, (0, 255, 0), 2) cv2.putText(image_frame, user_id, (left, y), cv2.FONT_HERSHEY_PLAIN, 0.75, (0, 255, 0), 2)
display_frame(image_frame) display_frame(image_frame)
def display_frame(frame): def display_frame(frame):
"""Displays the frame to the user.""" """Displays the frame to the user."""
cv2.imshow("Frame", frame) cv2.imshow("Frame", frame)
...@@ -98,8 +99,8 @@ def recognize_user(): ...@@ -98,8 +99,8 @@ def recognize_user():
# Determine the time at which we will time out. Equal to current time + timeout. # Determine the time at which we will time out. Equal to current time + timeout.
timeout_time: float = time.time() + TIMEOUT timeout_time: float = time.time() + TIMEOUT
while (time.time() < timeout_time and not user_recognized): while time.time() < timeout_time and not user_recognized:
# Read a image_frame from the videostream. # Read a image_frame from the video stream.
image_frame = video_stream.read() image_frame = video_stream.read()
# Convert input from BGR to RGB # Convert input from BGR to RGB
...@@ -130,7 +131,7 @@ def recognize_user(): ...@@ -130,7 +131,7 @@ def recognize_user():
break break
recognized_user = max(recognized_users_count, recognized_user = max(recognized_users_count,
keys=recognized_users_count.get()) key=recognized_users_count.get)
if recognized_users_count[recognized_user] < MIN_USER_RECOGNITION_COUNT: if recognized_users_count[recognized_user] < MIN_USER_RECOGNITION_COUNT:
recognized_user = None recognized_user = None
return recognized_user return recognized_user
......
"""
Creates a facial recognition profile for a new user.
"""
"""
Creates a facial recongnition profile for a new user.
"""
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