Skip to main content

A package for out of the box face recognition learning

Project description

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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pl_face_recognition-0.1.0.tar.gz (5.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pl_face_recognition-0.1.0-py3-none-any.whl (8.1 kB view details)

Uploaded Python 3

File details

Details for the file pl_face_recognition-0.1.0.tar.gz.

File metadata

  • Download URL: pl_face_recognition-0.1.0.tar.gz
  • Upload date:
  • Size: 5.5 kB
  • Tags: Source
  • Uploaded using 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

File hashes

Hashes for pl_face_recognition-0.1.0.tar.gz
Algorithm Hash digest
SHA256 915eb84890d8ee7fda84964ac12a8af4832b839f5106cd97a4ca45261024e9bd
MD5 b341d5be0447eae3769029a648c3e8af
BLAKE2b-256 ba75fe5086a0bbca04431fb36c5da7315390a404eb3d0ad2e50cfd706b1160eb

See more details on using hashes here.

File details

Details for the file pl_face_recognition-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: pl_face_recognition-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 8.1 kB
  • Tags: Python 3
  • Uploaded using 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

File hashes

Hashes for pl_face_recognition-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 2ccf07444792f9d35250772e8246d495e5c32b78c23d40881c71d1998cd8c99c
MD5 5e6171b24dc67c197c3ee5f09688edf8
BLAKE2b-256 d022cf1f0ef1ff73b99ee03c1188e1ce08dfda86b5e556d0eb458bd0216039ed

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page