Skip to content

Instantly share code, notes, and snippets.

@KevinPatel04
Last active April 27, 2020 12:08
Show Gist options
  • Save KevinPatel04/0829107c50b5b5a2fe365311de49e3c2 to your computer and use it in GitHub Desktop.
Save KevinPatel04/0829107c50b5b5a2fe365311de49e3c2 to your computer and use it in GitHub Desktop.
Face Recognition From Image Blog
# TOLERANCE defines the tolerance for face comparisons
# The lower the number - the stricter the comparison
# To avoid false matches, use lower value
# To avoid false negatives (i.e. faces of the same person doesn't match), use higher value
# 0.5-0.6 works well
TOLERANCE = 0.50
# This function returns the name of the person whose image matches with the given face (or 'Unknown Face')
# face_encoding is the face encoding of the face we are looking for
def find_match(face_encoding):
# see if the face is any match(es) for the known face(s)
# Compute the face distance to get a list of True/False values indicating whether or not there's a match
all_matches = (np.linalg.norm(known_face_encodings - face_encoding, axis=1) <= TOLERANCE).tolist()
# initialize name string as Unknown Face
name = 'Unknown Face'
# if a match was found in known_face_encodings, use the first one.
if True in all_matches:
first_match_index = all_matches.index(True)
name = known_face_names[first_match_index]
# Return the name of the first match
return name
•
├── images
│   ├── training
│   │   ├── Barack Obama.jpg
│   │   ├── Narendra Modi.jpg
│   │   ├── Boris Johnson.jpg
│   │   ├── Donald Trump.jpg
│   │   └── Justin Trudeau.jpg
│   └── testing
│       ├── modi-obama-1.jpg
│       ├── modi-obama-2.jpg
│       ├── trump-modi.jpg
│       └── trump-obama.png
├── models
│   ├── mmod_human_face_detector.dat
│   ├── shape_predictor_68_face_landmarks.dat
│   └── dlib_face_recognition_resnet_model_v1.dat
├── dlib-face-recognition-image.py
└── face-recognition-from-image.py
# convert dlib's bounding box object to (top_pos, right_pos, bottom_pos, left_pos) tuple
def dlib_box_to_bb(face_location):
left_pos = face_location.left()
top_pos = face_location.top()
right_pos = face_location.right()
bottom_pos = face_location.bottom()
# return face location tupple
return top_pos, right_pos, bottom_pos, left_pos
# read and load an image file
image_to_recognize = cv2.imread('images/testing/modi-obama-2.jpg')
# apply face detector and localize all human faces from the image
# if HOG face detector is used
# all_face_locations = hog_face_detector(image_to_recognize, 1)
# if CNN face detector is used
all_face_locations = cnn_face_detector(image_to_recognize, 1)
for index, face_location in enumerate(all_face_locations):
# if HOG face detector is used
# face_location = face_location
# if CNN face detector is used
face_location = face_location.rect
# align the face and calculate face pos using dlib's shape predictor
face_pose = shape_predictor(image_to_recognize, face_location)
# compute encodings and save as numpy array
face_encoding = np.array(dlib_face_recognition_model.compute_face_descriptor(image_to_recognize, face_pose, 1))
name = find_match(face_encoding)
# convert dlib's bounding box object to (top_pos, right_pos, bottom_pos, left_pos)
# if HOG face detector is used
# top_pos, right_pos, bottom_pos, left_pos = dlib_box_to_bb(face_location)
# if CNN face detector is used
top_pos, right_pos, bottom_pos, left_pos = dlib_box_to_bb(face_location)
# draw rectangle around face
cv2.rectangle(image_to_recognize, (left_pos,top_pos), (right_pos,bottom_pos), (0,0,255), 2)
# disply the name of age as text along the face
font = cv2.FONT_HERSHEY_SIMPLEX
cv2.putText(image_to_recognize, name, (left_pos,bottom_pos+15), font, 0.5, (0,0,255), 1)
# show image
cv2.imshow("Image Face Recognition",image_to_recognize)
# only for google colab
# cv2_imshow(image_to_recognize)
# importing required libraries
import cv2
import face_recognition
import os
# only for google colab
# from google.colab.patches import cv2_imshow
# 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 = 'images/training/'
# 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
# see if the face is any match(es) for the known face(s)
all_matches = face_recognition.compare_faces(known_face_encodings,face_encoding)
# initialize a name string as unknown face
name = "Unknown Face"
# if a match was found in known_face_encodings, use the first one.
if True in all_matches:
first_match_index = all_matches.index(True)
name = known_face_names[first_match_index]
# draw rectangle around face
cv2.rectangle(image_to_recognize, (left_pos,top_pos), (right_pos,bottom_pos), (0,0,255), 2)
# disply the name of age as text along the face
font = cv2.FONT_HERSHEY_SIMPLEX
cv2.putText(image_to_recognize, name, (left_pos,bottom_pos+15), font, 0.5, (0,0,255), 1)
# show image
cv2.imshow("Image Face Recognition",image_to_recognize)
# only for google colab
# cv2_imshow(image_to_recognize)
# inport required libraries
import cv2
import dlib
import os
import numpy as np
# only for google colab
# from google.colab.patches import cv2_imshow
# initialize face detector model
# dlib's HOG detector
hog_face_detector = dlib.get_frontal_face_detector()
# dlib's CNN detector
detector_weights = 'models/mmod_human_face_detector.dat'
cnn_face_detector = dlib.cnn_face_detection_model_v1(detector_weights)
# initialize dlib shape predictor
predictor_path = 'models/shape_predictor_68_face_landmarks.dat'
shape_predictor = dlib.shape_predictor(predictor_path)
# initialize dlib face recognition
recognition_weigths = 'models/dlib_face_recognition_resnet_model_v1.dat'
dlib_face_recognition_model = dlib.face_recognition_model_v1(recognition_weigths)
# 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 = 'images/training/'
# 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):
# read image file
image = cv2.imread(image_file)
# apply face detector and localize human faces from the image
# if HOG face detector is used
# all_face_locations = hog_face_detector(image, 1)
# if CNN face detector is used
all_face_locations = cnn_face_detector(image, 1)
# verify that each training image contains only 1 face
if len(all_face_locations) != 1:
print(image_file,', must contain only 1 face while training.')
continue
#--------------------------------------------------------------------------
# generate encodings
#--------------------------------------------------------------------------
# convert all_face_locations[0] to dlib rectangle object
# if HOG face detector is used
# face_location = all_face_locations[0]
# if CNN face detector is used
face_location = all_face_locations[0].rect
# align the face and calculate face pos using dlib's shape predictor
face_pose = shape_predictor(image, face_location)
# compute encodings and save as numpy array
face_encoding = np.array(dlib_face_recognition_model.compute_face_descriptor(image, face_pose, 1))
# append to known_face_encodings[]
known_face_encodings.append(face_encoding)
# 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])
#--------------------------------------------------------------------------
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