-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
121 additions
and
20 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
include face_compare/weights/facenet_weights.h5 |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
#!/usr/bin/env python3 | ||
import cv2 | ||
import argparse | ||
import numpy as np | ||
from pathlib import Path | ||
|
||
from face_compare.images import get_face | ||
from face_compare.model import facenet_model, img_to_encoding | ||
|
||
def run(image_one, image_two, save_dest=None): | ||
# Load images | ||
face_one = get_face(cv2.imread(str(image_one), 1)) | ||
face_two = get_face(cv2.imread(str(image_two), 1)) | ||
|
||
# Optionally save cropped images | ||
if save_dest is not None: | ||
print(f'Saving cropped images in {save_dest}.') | ||
cv2.imwrite(str(save_dest.joinpath('face_one.png')), face_one) | ||
cv2.imwrite(str(save_dest.joinpath('face_two.png')), face_two) | ||
|
||
# load model | ||
model = facenet_model(input_shape=(3, 96, 96)) | ||
|
||
# Calculate embedding vectors | ||
embedding_one = img_to_encoding(face_one, model) | ||
embedding_two = img_to_encoding(face_two, model) | ||
|
||
dist = np.linalg.norm(embedding_one - embedding_two) | ||
print(f'Distance between two images is {dist}') | ||
if dist > 0.7: | ||
print('These images are of two different people!') | ||
else: | ||
print('These images are of the same person!') | ||
|
||
|
||
if __name__ == '__main__': | ||
ap = argparse.ArgumentParser(description='Face Comparison Tool') | ||
|
||
ap.add_argument('--image-one', dest='image_one', type=Path, required=True, help='Input Image One') | ||
ap.add_argument('--image-two', dest='image_two', type=Path, required=True, help='Input Image Two') | ||
ap.add_argument('-s', '--save-to', dest='save_dest', type=Path, help='Optionally save the cropped faces on disk. Input directory to save them to') | ||
args = ap.parse_args() | ||
|
||
run(args.image_one, args.image_two, args.save_dest) |
File renamed without changes.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import cv2 | ||
|
||
def get_face(img): | ||
'''Crops image to only include face plus a border''' | ||
height, width, channels = img.shape | ||
face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + "haarcascade_frontalface_default.xml") | ||
face_box = face_cascade.detectMultiScale(img) | ||
# Get dimensions of bounding box | ||
x, y, w, h = tuple(map(tuple, face_box))[0] | ||
# Calculate padding as segmentation is too tight. | ||
pad_w = int(w/2.5) | ||
pad_h = int(h/2.5) | ||
# Get co-ordinates of crop | ||
x1 = max(0, x-pad_w) | ||
y1 = max(0, y-pad_h) | ||
x2 = min(width, x+w+pad_w) | ||
y2 = min(height, y+h+pad_h) | ||
# Crop image | ||
cropped = img[y1:y2,x1:x2] | ||
return cropped |
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
Empty file.
File renamed without changes.
File renamed without changes.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,26 @@ | ||
#!/usr/bin/env python3 | ||
'''Use this to install module''' | ||
from setuptools import setup, find_packages | ||
|
||
setup( | ||
name='face_compare', | ||
version='1.0.0', | ||
description='Compare if two faces are from the same person.', | ||
author='Matt Lyon', | ||
author_email='[email protected]', | ||
python_requires='>=3.7', | ||
license='MIT License', | ||
packages=find_packages(), | ||
install_requires=[ | ||
'tensorflow', | ||
'keras', | ||
'opencv-python' | ||
], | ||
classifiers=[ | ||
'Programming Language :: Python', | ||
'Operating System :: Unix', | ||
'Operating System :: MacOS', | ||
], | ||
scripts=['bin/compare_faces.py'], | ||
include_package_data=True | ||
) |