Skip to content

Commit

Permalink
First Commit
Browse files Browse the repository at this point in the history
  • Loading branch information
abhay6694 committed Feb 18, 2020
0 parents commit 1037318
Show file tree
Hide file tree
Showing 5 changed files with 886 additions and 0 deletions.
64 changes: 64 additions & 0 deletions FaceSimilarity.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
# face verification with the VGGFace2 model
import matplotlib.pyplot as plt
from PIL import Image
import numpy as np
from scipy.spatial.distance import cosine
from mtcnn.mtcnn import MTCNN
from utils import preprocess_input
from vggface import VGGFace
import sys


# extract a single face from a given photograph
def extract_face(filename, required_size=(224, 224)):
img = plt.imread(filename)
detector = MTCNN()
results = detector.detect_faces(img)
x1, y1, width, height = results[0]['box']
x2, y2 = x1 + width, y1 + height
# extract the face
face = img[y1:y2, x1:x2]
# resize pixels to the model size
image = Image.fromarray(face)
image = image.resize(required_size)
face_array = np.asarray(image)
return face_array


# extract faces and calculate face embeddings for a list of photo files
def get_embeddings(filenames):
# extract faces
faces = [extract_face(f) for f in filenames]
# convert into an array of samples
samples = np.asarray(faces, 'float32')
# prepare the face for the model, e.g. center pixels
samples = preprocess_input(samples, version=2)

model = VGGFace(model='resnet50', include_top=False, input_shape=(224, 224, 3), pooling='avg')
pred = model.predict(samples)
return pred


# determine if a candidate face is a match for a known face
def is_match(known_embedding, candidate_embedding, thresh=0.5):
# calculate distance between embeddings
score = cosine(known_embedding, candidate_embedding)

print('*****************************************************************')
print('Threshold for the face similarity score is 0.5')

if score <= thresh:
print('Face is a Match with score of %.3f' % score)
else:
print('Face is not a Match with score of %.3f' % score)

print('********************************************************************')


def main():
embeddings = get_embeddings([sys.argv[1], sys.argv[2]])
is_match(embeddings[0], embeddings[1])


if __name__ == '__main__':
main()
Loading

0 comments on commit 1037318

Please sign in to comment.