Skip to main content

Face Recognition

This library provides out-of-the box facial recognition training and inference based on Facenet and Pytorch

Important Requirements

python >= 3.6
torch >=1.0.0

Quick start

Install with pip.

pip install pl-face-recognition

This project was designed to minimize the amount of false positives, for that it requires two sets of data, one with pictures of the people you want to recognize, and other with examples of other random people (a public dataset like lfw should suffice).

So given a dataset structure like this (we follow the torchvision ImageFolder structure, for both sets of data separately):

dataset/
__good_faces/
____hugh_jackman/
______hugh_1.jpeg
____ryan_reynolds/
______ryan_1.jpeg
__bad_faces/
____some_random_person/
______random_person_1.jpeg
____some_random_other_person/
______dude.jpeg

You may do insta train and inference like this:

from face_recognition.face_recognizers import OneNeighborRecognizer
from PIL import Image

IMAGES_PATH = './dataset/good_faces'
ADVERSARY_IMAGES_PATH = './dataset/bad_faces'
TEST_IMAGE_PATH = './ryan_2.jpeg'


clf = OneNeighborRecognizer() # finds a match via 1 closest neighbor
clf.fit(IMAGES_PATH, ADVERSARY_IMAGES_PATH)
test = [Image.open(TEST_IMAGE_PATH)] # predict receives an array/batch of images

predicted_id = clf.predict(test)

print(predicted_id)
print(clf.ids_to_class(predicted_id))

>> 1
>> 'ryan_reynolds'

Custom behavior

The library works by doing nearest neighbor search on FaceNet generated embeddings of the known images. If you want to have direct access to the embeddings you can do so via the FaceSpace class:

from face_recognition.face_space import FaceSpace

IMAGES_PATH = './dataset/good_faces'
TEST_IMAGE_PATH = './ryan_2.jpeg'
test = [Image.open(TEST_IMAGE_PATH)]

fs = FaceSpace()
embeddings, class_names = fs.get_embeddings(IMAGES_PATH) # get embeddings from a dataset

image_embeddings = fs.get_embedding_from_images(test) # or from array of images

The library will provide different methods for using the embeddings for classification. For know it only has the OneNeighborClassifier class:

from face_recognition.classifiers import OneNeighborClassifier
classifier = OneNeighborClassifier()
classifier.fit(embeddings, labels) # labels are integer indexes for target classes and -1 for adversarial
classifier.predict(other_embeddings)

You may implement your own distance based classifiers by inheriting from classifiers.DistanceClassifier and defining the predict method.

from face_recognition.classifiers import DistanceClassifier
import random

class StupidClassifier(DistanceClassifier):

    def predict(self, target_embeddings):
        '''
        here you define how to get the correct labels by somehow comparing
        self.embeddings and target_embeddings
        See OneNeighborClassifier for an example
        '''
        return  [random.choice(self.labels) for _ in range(len(target_embeddings))] # and you return labels

Contributing

If you want to add functionality, fork the repo and feel free to open a pull request.

Credits

Thank you contributors!

Platanus

face-recognition is maintained by platanus.

License

face-recognition is © 2020 platanus, spa. It is free software and may be redistributed under the terms specified in the LICENSE file.

Release files for pl-face-recognition 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pl-face-recognition 0.1.0
File Size Uploaded
pl_face_recognition-0.1.0.tar.gz 5.5 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for pl-face-recognition 0.1.0
File Interpreter ABI Platform
pl_face_recognition-0.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 13.6 kB

Release files / pl_face_recognition-0.1.0.tar.gz

Download URL pl_face_recognition-0.1.0.tar.gz
Size 5.5 kB
Tags Source
SHA-256 checksum
How to use checksums
915eb84890d8ee7fda84964ac12a8af4832b839f5106cd97a4ca45261024e9bd
BLAKE2b-256 checksum
How to use checksums
ba75fe5086a0bbca04431fb36c5da7315390a404eb3d0ad2e50cfd706b1160eb
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.1.1 pkginfo/1.5.0.1 requests/2.21.0 setuptools/47.1.1 requests-toolbelt/0.9.1 tqdm/4.46.0 CPython/3.7.5

Release files / pl_face_recognition-0.1.0-py3-none-any.whl

Download URL pl_face_recognition-0.1.0-py3-none-any.whl
Size 8.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
2ccf07444792f9d35250772e8246d495e5c32b78c23d40881c71d1998cd8c99c
BLAKE2b-256 checksum
How to use checksums
d022cf1f0ef1ff73b99ee03c1188e1ce08dfda86b5e556d0eb458bd0216039ed
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.1.1 pkginfo/1.5.0.1 requests/2.21.0 setuptools/47.1.1 requests-toolbelt/0.9.1 tqdm/4.46.0 CPython/3.7.5

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files

0.0.2

2 release files

0.0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page