-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Making face recognition system independent of dataset. Major refactor…
…ing. Former-commit-id: ebeed61
- Loading branch information
Showing
26 changed files
with
126 additions
and
226 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
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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
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,2 @@ | ||
from .face_recogniser import FaceRecogniser | ||
from .face_features_extractor import FaceFeaturesExtractor |
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,27 @@ | ||
import torch | ||
from facenet_pytorch import MTCNN, InceptionResnetV1 | ||
from torchvision import transforms | ||
from . import preprocessing | ||
from facenet_pytorch.models.utils.detect_face import extract_face | ||
|
||
|
||
class FaceFeaturesExtractor: | ||
def __init__(self): | ||
self.aligner_preprocess = transforms.Compose([preprocessing.ExifOrientationNormalize()]) | ||
self.aligner = MTCNN(prewhiten=False, keep_all=True) | ||
self.facenet_preprocess = transforms.Compose([preprocessing.Whitening()]) | ||
self.facenet = InceptionResnetV1(pretrained='vggface2').eval() | ||
|
||
def extract_features(self, img): | ||
bbs, _ = self.aligner.detect(self.aligner_preprocess(img)) | ||
if bbs is None: | ||
# if no face is detected | ||
return None, None | ||
|
||
faces = torch.stack([extract_face(img, bb) for bb in bbs]) | ||
embeddings = self.facenet(faces).detach().numpy() | ||
|
||
return bbs, embeddings | ||
|
||
def __call__(self, img): | ||
return self.extract_features(img) |
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,41 @@ | ||
from collections import namedtuple | ||
|
||
Prediction = namedtuple('Prediction', 'id name confidence') | ||
Face = namedtuple('Face', 'top_prediction bb all_predictions') | ||
BoundingBox = namedtuple('BoundingBox', 'left top right bottom') | ||
|
||
|
||
def top_prediction(idx_to_class, probs): | ||
top_label = probs.argmax() | ||
return Prediction(id=top_label, name=idx_to_class[top_label], confidence=probs[top_label]) | ||
|
||
|
||
def to_predictions(idx_to_class, probs): | ||
return [Prediction(id=i, name=idx_to_class[i], confidence=prob) for i, prob in enumerate(probs)] | ||
|
||
|
||
class FaceRecogniser: | ||
def __init__(self, feature_extractor, classifier, idx_to_class): | ||
self.feature_extractor = feature_extractor | ||
self.classifier = classifier | ||
self.idx_to_class = idx_to_class | ||
|
||
def recognise_faces(self, img): | ||
bbs, embeddings = self.feature_extractor(img) | ||
if bbs is None: | ||
# if no faces are detected | ||
return [] | ||
|
||
predictions = self.classifier.predict_proba(embeddings) | ||
|
||
return [ | ||
Face( | ||
top_prediction=top_prediction(self.idx_to_class, probs), | ||
bb=BoundingBox(left=bb[0], top=bb[1], right=bb[2], bottom=bb[3]), | ||
all_predictions=to_predictions(self.idx_to_class, probs) | ||
) | ||
for bb, probs in zip(bbs, predictions) | ||
] | ||
|
||
def __call__(self, img): | ||
return self.recognise_faces(img) |
File renamed without changes.
Binary file not shown.
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.
Oops, something went wrong.