From 99dc6aaa04d6a597e73349ff55cd396069a8fc1b Mon Sep 17 00:00:00 2001 From: Kokilarathan Sivarajah <60232524+kokilarathan-siva01@users.noreply.github.com> Date: Thu, 11 May 2023 13:47:16 +0100 Subject: [PATCH 1/3] Improving the performance of facial recognition v1 --- face_recognition/Face_recognition_Updated.py | 24 ++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 face_recognition/Face_recognition_Updated.py diff --git a/face_recognition/Face_recognition_Updated.py b/face_recognition/Face_recognition_Updated.py new file mode 100644 index 000000000..09d9c1707 --- /dev/null +++ b/face_recognition/Face_recognition_Updated.py @@ -0,0 +1,24 @@ +import numpy as np +import cv2 + +img = cv2.imread('test_image.jpg') + +gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) + +face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml') + +faces = face_cascade.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=5) + +encodings = np.load('face_encodings.npy') +names = np.load('face_names.npy') + +for (x, y, w, h) in faces: + face_gray = gray[y:y+h, x:x+w] + face_resized = cv2.resize(face_gray, encoding_model.input_shape[:2]) + encoding = encoding_model.encode(face_resized) + distances = np.linalg.norm(encodings - encoding, axis=1) + min_distance_index = np.argmin(distances) + if distances[min_distance_index] < 0.6: + print(names[min_distance_index]) + else: + print("Unknown") From d36bb2031bf320c29260e526bfff531fd95c68e9 Mon Sep 17 00:00:00 2001 From: Kokilarathan Sivarajah <60232524+kokilarathan-siva01@users.noreply.github.com> Date: Thu, 11 May 2023 13:49:38 +0100 Subject: [PATCH 2/3] v2 - comments and improved --- face_recognition/Face_recognition_Updated.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/face_recognition/Face_recognition_Updated.py b/face_recognition/Face_recognition_Updated.py index 09d9c1707..eb9807d2a 100644 --- a/face_recognition/Face_recognition_Updated.py +++ b/face_recognition/Face_recognition_Updated.py @@ -1,23 +1,37 @@ import numpy as np import cv2 +# Load the image img = cv2.imread('test_image.jpg') +# Convert the image to grayscale gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) +# Load the pre-trained face detection classifier face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml') +# Detect faces in the image faces = face_cascade.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=5) +# Load the pre-trained face encoding model +encoding_model = MyFaceEncodingModel() + +# Load the pre-stored face encodings and corresponding names encodings = np.load('face_encodings.npy') names = np.load('face_names.npy') +# Loop through each detected face for (x, y, w, h) in faces: + # Extract the face ROI from the grayscale image face_gray = gray[y:y+h, x:x+w] + # Resize the face ROI to match the expected input size of the face encoding model face_resized = cv2.resize(face_gray, encoding_model.input_shape[:2]) + # Encode the face ROI using the face encoding model encoding = encoding_model.encode(face_resized) + # Compute the Euclidean distance between the encoding of the detected face and all stored encodings distances = np.linalg.norm(encodings - encoding, axis=1) min_distance_index = np.argmin(distances) + # Check if the closest match is below a certain threshold (e.g., 0.6) if distances[min_distance_index] < 0.6: print(names[min_distance_index]) else: From a3017cdb1c68bef99fd4adc5cd970c4f5a13309d Mon Sep 17 00:00:00 2001 From: Kokilarathan Sivarajah <60232524+kokilarathan-siva01@users.noreply.github.com> Date: Thu, 11 May 2023 13:53:33 +0100 Subject: [PATCH 3/3] Final improvement, top performance v3 instead of looping through all stored encodings for each detected face, this code first computes the encodings of all detected faces and then computes the Euclidean distance between the encodings of all detected faces and all stored encodings at once using NumPy broadcasting. This approach is likely to be much faster than the previous approach for large datasets. --- face_recognition/Face_recognition_Updated.py | 26 ++++++++++++-------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/face_recognition/Face_recognition_Updated.py b/face_recognition/Face_recognition_Updated.py index eb9807d2a..6c855a788 100644 --- a/face_recognition/Face_recognition_Updated.py +++ b/face_recognition/Face_recognition_Updated.py @@ -20,19 +20,25 @@ encodings = np.load('face_encodings.npy') names = np.load('face_names.npy') -# Loop through each detected face +# Encode all faces in the input image using the face encoding model +encodings_img = [] for (x, y, w, h) in faces: - # Extract the face ROI from the grayscale image face_gray = gray[y:y+h, x:x+w] - # Resize the face ROI to match the expected input size of the face encoding model face_resized = cv2.resize(face_gray, encoding_model.input_shape[:2]) - # Encode the face ROI using the face encoding model encoding = encoding_model.encode(face_resized) - # Compute the Euclidean distance between the encoding of the detected face and all stored encodings - distances = np.linalg.norm(encodings - encoding, axis=1) - min_distance_index = np.argmin(distances) - # Check if the closest match is below a certain threshold (e.g., 0.6) - if distances[min_distance_index] < 0.6: - print(names[min_distance_index]) + encodings_img.append(encoding) +encodings_img = np.array(encodings_img) + +# Compute the Euclidean distance between the encodings of all detected faces and all stored encodings +distances = np.linalg.norm(encodings - encodings_img[:, np.newaxis], axis=-1) + +# Find the closest matching encoding for each detected face +min_distance_indices = np.argmin(distances, axis=0) +min_distances = distances[min_distance_indices, np.arange(len(min_distance_indices))] + +# Print the names corresponding to the closest matching encodings +for i, distance in enumerate(min_distances): + if distance < 0.6: + print(names[min_distance_indices[i]]) else: print("Unknown")