Last active
April 15, 2020 13:46
-
-
Save KevinPatel04/13e1b0271f40bed77b9db1e161d65a5e to your computer and use it in GitHub Desktop.
Measure Distance Between Two Images Blog
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# importing required libraries | |
import cv2 | |
import face_recognition | |
import os | |
import numpy as np | |
# initialize the empty list of name label | |
known_face_names = [] | |
# initialize the empty list for storing the encoding of each face | |
known_face_encodings = [] | |
# initialize the folder path consisting of training dataset | |
training_dataset_path = 'Training data/' | |
# read all the image file with extension .JPG present under the training_dataset_path | |
training_dataset = filter(lambda x: x.endswith('.jpg'), os.listdir(training_dataset_path)) | |
# Sort in alphabetical order | |
training_dataset = sorted(training_dataset) | |
# define function for generating known face encodings | |
def generate_known_face_encodings(): | |
# Get full paths to training images | |
paths_to_training_images = [training_dataset_path + x for x in training_dataset] | |
for index,image_file in enumerate(paths_to_training_images): | |
# Get list of names of people by eliminating the .JPG extension from image filenames | |
known_face_names.append(image_file[image_file.rindex('/')+1:][:-4]) | |
# read image file | |
image = cv2.imread(image_file) | |
# detect face from image and get the face encoding | |
face_encoding = face_recognition.face_encodings(image)[0] | |
# append this into known_face_encodings | |
known_face_encodings.append(face_encoding) | |
print("Training Started...") | |
generate_known_face_encodings() | |
print("Training Completed...") | |
# load the image to be detected | |
image_to_recognize = cv2.imread('modi-obama-1.jpg') | |
# locate all faces from the image | |
all_face_locations = face_recognition.face_locations(image_to_recognize, number_of_times_to_upsample = 2 ,model="cnn") | |
# generate encoding for all detected faces | |
all_face_encodings = face_recognition.face_encodings(image_to_recognize,all_face_locations) | |
for face_location, face_encoding in zip(all_face_locations,all_face_encodings): | |
# split the tuple | |
top_pos, right_pos, bottom_pos, left_pos = face_location | |
# find the distance of current encoding with all known encodings | |
face_distances = face_recognition.face_distance(known_face_encodings, face_encoding) | |
for i,face_distance in enumerate(face_distances): | |
print("\nThe calculated distance is {:.2} from sample image {}".format(face_distance, known_face_names[i])) | |
# print matching percentage | |
print("The matching percentage is {}% against the sample image {}".format(round((1-float(face_distance))*100, 2), known_face_names[i])) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# load the image to be detected | |
image_to_recognize = cv2.imread('modi-obama-1.jpg') | |
# locate all faces from the image | |
all_face_locations = face_recognition.face_locations(image_to_recognize, number_of_times_to_upsample = 2 ,model="cnn") | |
# generate encoding for all detected faces | |
all_face_encodings = face_recognition.face_encodings(image_to_recognize,all_face_locations) | |
for face_location, face_encoding in zip(all_face_locations,all_face_encodings): | |
# split the tuple | |
top_pos, right_pos, bottom_pos, left_pos = face_location | |
# find the distance of current encoding with all known encodings | |
face_distances = face_recognition.face_distance(known_face_encodings, face_encoding) | |
for i,face_distance in enumerate(face_distances): | |
print("\nThe calculated distance is {:.2} from sample image {}".format(face_distance, known_face_names[i])) | |
# print matching percentage | |
print("The matching percentage is {}% against the sample image {}".format(round((1-float(face_distance))*100, 2), known_face_names[i])) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# importing required libraries | |
import cv2 | |
import face_recognition | |
import os | |
import numpy as np |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# initialize the empty list of name label | |
known_face_names = [] | |
# initialize the empty list for storing the encoding of each face | |
known_face_encodings = [] | |
# initialize the folder path consisting of training dataset | |
training_dataset_path = 'Training data/' | |
# read all the image file with extension .JPG present under the training_dataset_path | |
training_dataset = filter(lambda x: x.endswith('.jpg'), os.listdir(training_dataset_path)) | |
# Sort in alphabetical order | |
training_dataset = sorted(training_dataset) | |
# define function for generating known face encodings | |
def generate_known_face_encodings(): | |
# Get full paths to training images | |
paths_to_training_images = [training_dataset_path + x for x in training_dataset] | |
for index,image_file in enumerate(paths_to_training_images): | |
# Get list of names of people by eliminating the .JPG extension from image filenames | |
known_face_names.append(image_file[image_file.rindex('/')+1:][:-4]) | |
# read image file | |
image = cv2.imread(image_file) | |
# detect face from image and get the face encoding | |
face_encoding = face_recognition.face_encodings(image)[0] | |
# append this into known_face_encodings | |
known_face_encodings.append(face_encoding) | |
print("Training Started...") | |
generate_known_face_encodings() | |
print("Training Completed...") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment