Skip to main content

A package wrapping the FaceNet embedding model

Project description

keras-facenet

This is a simple wrapper around this wonderful implementation of FaceNet. I wanted something that could be used in other applications, that could use any of the four trained models provided in the linked repository, and that took care of all the setup required to get weights and load them. I prefer using Keras wherever possible because of its API, so I used the example provided here and implemented it as part of the code.

Enough background -- so how are you supposed to use this?

Installing

pip install keras-facenet

Usage

To get the embeddings for cropped images of some faces, you can do something like the following.

from keras_facenet import FaceNet
embedder = FaceNet()

# images is a list of images, each as an
# np.ndarray of shape (H, W, 3).
embeddings = embedder.embeddings(images)

keras-facenet expects you to provide cropped images of faces and does not ship with a face detector. You can use another library of your choice to get those lovely cropped images. I provide two examples below.

Using keras-facenet with mira

mira is another package I developed to do simple object detection. Install it using pip install mira

Then you can use mira to extract faces with the built-in MTCNN model.

from mira.detectors import MTCNN
from keras_facenet import FaceNet

detector = MTCNN()
embedder = FaceNet()

faces = detector.detect(image)
embeddings = embedder.embeddings([
    face.selection.extract(image) for face in faces
])

Using keras-facenet with face_recognition

face_recognition is a fantastic all-in-one package for face detection and recognition. Anecdotally, I find that its face detection model is not quite as good as MTCNN and that the embeddings are not quite as good as FaceNet. but you can use its detection model with FaceNet as follows.

from face_recognition import face_location
from keras_facenet import 

embedder = FaceNet()

faces = face_locations(image)
embeddings = embedder.embeddings([
    image[t:b, l:r] for for t, r, b, l in faces
])

Logging

To see what's going on under the hood, set logging to view INFO logs. If using in a Jupyter notebook, you can use the following.

import logging

logging.basicConfig()
log = logging.getLogger()
log.setLevel('INFO')

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

keras-facenet-0.1a3.tar.gz (10.1 kB view details)

Uploaded Source

Built Distribution

keras_facenet-0.1a3-py3-none-any.whl (10.8 kB view details)

Uploaded Python 3

File details

Details for the file keras-facenet-0.1a3.tar.gz.

File metadata

  • Download URL: keras-facenet-0.1a3.tar.gz
  • Upload date:
  • Size: 10.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.6.3 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.7.1

File hashes

Hashes for keras-facenet-0.1a3.tar.gz
Algorithm Hash digest
SHA256 188e6b89f74be0f69a006d7569880e9c315142fd092bcef836eed0d7b4bae8cc
MD5 637f44ca1b6578cfdfd329eaa647bb47
BLAKE2b-256 49ebe3b24e28ca2f18a9add48263670936d8098e22be142a373f3e14e556ab75

See more details on using hashes here.

File details

Details for the file keras_facenet-0.1a3-py3-none-any.whl.

File metadata

  • Download URL: keras_facenet-0.1a3-py3-none-any.whl
  • Upload date:
  • Size: 10.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.6.3 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.7.1

File hashes

Hashes for keras_facenet-0.1a3-py3-none-any.whl
Algorithm Hash digest
SHA256 e1094a990f5cae664e752a39653e9a26c7193926973dac060862c57ed04be9e3
MD5 f6d73a65e6ccb9eb90dd4e7c6c77bf59
BLAKE2b-256 83f04eb3ccbbb5f0cec73be37186b5a7892d16c7446bba137e90405f4d3554d7

See more details on using hashes here.

Supported by

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