Skip to main content

A Lightweight Face Recognition and Facial Attribute Analysis Framework (Age, Gender, Emotion, Race) for Python

Project description

deepface

Downloads

deepface is a lightweight facial analysis framework including face recognition and demography (age, gender, emotion and race) for Python. You can apply facial analysis with a few lines of code. It plans to bridge a gap between software engineering and machine learning studies.

Installation

The easiest way to install deepface is to download it from PyPI.

pip install deepface

Face Recognition

A modern face recognition pipeline consists of 4 common stages: detect, align, represent and verify. DeepFace handles all these common stages in the background.

Face Verification - Demo

Verification function under the DeepFace interface offers a single face recognition.

from deepface import DeepFace
result = DeepFace.verify("img1.jpg", "img2.jpg")
print("Is verified: ", result["verified"])

Each call of verification function builds a face recognition model from scratch and this is a costly operation. If you are going to verify multiple faces sequentially, then you should pass an array of faces to verification function to speed the operation up. In this way, complex face recognition models will be built once.

dataset = [
	['dataset/img1.jpg', 'dataset/img2.jpg'],
	['dataset/img1.jpg', 'dataset/img3.jpg']
]
resp_obj = DeepFace.verify(dataset)

Items of resp_obj might be unsorted when you pass multiple instances to verify function. Please check the item indexes in the response object.

Large scale face recognition - Demo

You can apply face recognition on a large scale data set as well. Face recognition requires to apply face verification multiple times. Herein, deepface offers an out-of-the-box find function to handle this action. Representations of faces photos in your database folder will be stored in a pickle file when find function is called once. Then, deepface just finds representation of the target image. In this way, finding an identity in a large scale data set will be performed in just seconds.

from deepface import DeepFace
import pandas as pd
df = DeepFace.find(img_path = "img1.jpg", db_path = "C:/workspace/my_db")
#dfs = DeepFace.find(img_path = ["img1.jpg", "img2.jpg"], db_path = "C:/workspace/my_db")

Supported face recognition models

deepface currently wraps the state-of-the-art face recognition models: VGG-Face , Google FaceNet, OpenFace, Facebook DeepFace and DeepID. The default configuration verifies faces with VGG-Face model. You can set the base model while verification as illustared below. Accuracy and speed show difference based on the performing model.

models = ["VGG-Face", "Facenet", "OpenFace", "DeepFace", "DeepID"]
for model in models:
   result = DeepFace.verify("img1.jpg", "img2.jpg", model_name = model)

Passing pre-built face recognition models

You can build a face recognition model once and pass this to verify function as well. This might be logical if you need to call verify function several times.

from deepface.basemodels import VGGFace, OpenFace, Facenet, FbDeepFace, DeepID
model = VGGFace.loadModel() #all face recognition models have loadModel() function in their interfaces
DeepFace.verify("img1.jpg", "img2.jpg", model_name = "VGG-Face", model = model)

Similarity

Face recognition models are regular convolutional neural networks and they are responsible to represent face photos as vectors. Decision of verification is based on the distance between vectors. We can classify pairs if its distance is less than a threshold.

Distance could be found by different metrics such as Cosine Similarity, Euclidean Distance and L2 form. The default configuration finds the cosine similarity. You can alternatively set the similarity metric while verification as demostratred below.

metrics = ["cosine", "euclidean", "euclidean_l2"]
for metric in metrics:
   result = DeepFace.verify("img1.jpg", "img2.jpg", model_name = "VGG-Face", distance_metric = metric)

Ensemble learning for face recognition - Demo

A face recognition task can be handled by several models and similarity metrics. We can combine the precictions of all of those models and metrics to improve the accuracy of a face recognition task. This offers a huge improvement on accuracy, precision and recall but it runs much slower than single models.

resp_obj = DeepFace.verify("img1.jpg", "img2.jpg", model_name = "Ensemble")
df = DeepFace.find(img_path = "img1.jpg", db_path = "my_db", model_name = "Ensemble")

Facial Attribute Analysis - Demo

Deepface also offers facial attribute analysis including age, gender, facial expression (including angry, fear, neutral, sad, disgust, happy and surprise)and race (including asian, white, middle eastern, indian, latino and black) predictions. Analysis function under the DeepFace interface is used to find demography of a face.

from deepface import DeepFace
demography = DeepFace.analyze("img4.jpg", actions = ['age', 'gender', 'race', 'emotion'])
#demographies = DeepFace.analyze(["img1.jpg", "img2.jpg", "img3.jpg"]) #analyzing multiple faces same time
print("Age: ", demography["age"])
print("Gender: ", demography["gender"])
print("Emotion: ", demography["dominant_emotion"])
print("Race: ", demography["dominant_race"])

Passing pre-built facial analysis models

You can build facial attribute analysis models once and pass these to analyze function as well. This might be logical if you need to call analyze function several times.

import json
from deepface.extendedmodels import Age, Gender, Race, Emotion
models = {}
models["emotion"] = Emotion.loadModel()
models["age"] = Age.loadModel()
models["gender"] = Gender.loadModel()
models["race"] = Race.loadModel()
DeepFace.analyze("img1.jpg", models=models)

Streaming and Real Time Analysis - Demo

You can run deepface for real time videos as well.

Calling stream function under the DeepFace interface will access your webcam and apply both face recognition and facial attribute analysis. Stream function expects a database folder including face images. VGG-Face is the default face recognition model and cosine similarity is the default distance metric similar to verify function. The function starts to analyze if it can focus a face sequantially 5 frames. Then, it shows results 5 seconds.

from deepface import DeepFace
DeepFace.stream("/user/database")

Even though face recognition is based on one-shot learning, you can use multiple face pictures of a person as well. You should rearrange your directory structure as illustrated below.

user
├── database
│   ├── Alice
│      ├── Alice1.jpg
│      ├── Alice2.jpg
│   ├── Bob
│      ├── Bob.jpg

BTW, you should use regular slash ( / ) instead of backslash ( \ ) in Windows OS while passing the path to stream function. E.g. DeepFace.stream("C:/User/Sefik/Desktop/database").

API - Demo

Deepface serves an API as well.

You can clone /api/api.py and pass it to python command as an argument. This will get a rest service up. In this way, you can call deepface from an external system such as mobile app or web.

python api.py

The both face recognition and facial attribute analysis are covered in the API. You are expected to call these functions as http post methods. Service endpoints will be http://127.0.0.1:5000/verify for face recognition and http://127.0.0.1:5000/analyze for facial attribute analysis. You should pass input images as base64 encoded string in this case. Here, you can find a postman project.

E-Learning

Deepface is mentioned in this playlist as video lectures. Subscribe the channel to stay up-to-date and be informed when a new lecture is added,

Disclaimer

Reference face recognition models have different type of licenses. This framework is just a wrapper for those models. That's why, licence types are inherited as well. You should check the licenses for the face recognition models before use.

Herein, OpenFace is licensed under Apache License 2.0. FB DeepFace and Facenet is licensed under MIT License. The both Apache License 2.0 and MIT license types allow you to use for commercial purpose.

On the other hand, VGG-Face is licensed under Creative Commons Attribution License. That's why, it is restricted to adopt VGG-Face for commercial use.

Support

There are many ways to support a project - starring⭐️ the GitHub repos is just one.

You can also support this project through Patreon.

Citation

Please cite deepface in your publications if it helps your research. Here is an example BibTeX entry:

@misc{serengil2020deepface,
  abstract = {A Lightweight Face Recognition and Facial Attribute Analysis Framework for Python},
  author={Serengil, Sefik Ilkin},
  title={deepface},
  url = {https://github.com/serengil/deepface},
  year={2020}
}

Licence

Deepface is licensed under the MIT License - see LICENSE for more details.

Logo is created by Adrien Coquet. Licensed under Creative Commons: By Attribution 3.0 License.

Project details


Release history Release notifications | RSS feed

Download files

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

Source Distribution

deepface-0.0.31.tar.gz (27.0 kB view details)

Uploaded Source

Built Distribution

deepface-0.0.31-py3-none-any.whl (32.7 kB view details)

Uploaded Python 3

File details

Details for the file deepface-0.0.31.tar.gz.

File metadata

  • Download URL: deepface-0.0.31.tar.gz
  • Upload date:
  • Size: 27.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/45.1.0 requests-toolbelt/0.9.1 tqdm/4.42.0 CPython/3.6.4

File hashes

Hashes for deepface-0.0.31.tar.gz
Algorithm Hash digest
SHA256 a1118f5ebb8cc3a18c63f4860c457b2234530d72b2f5d005f484e8032902d0de
MD5 e8d2acf84e5b5f017792026c8778823e
BLAKE2b-256 d1e530a4854a923191ab54abcfae058ae0b03bfbf01565b63ac9062bc8665acc

See more details on using hashes here.

File details

Details for the file deepface-0.0.31-py3-none-any.whl.

File metadata

  • Download URL: deepface-0.0.31-py3-none-any.whl
  • Upload date:
  • Size: 32.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/45.1.0 requests-toolbelt/0.9.1 tqdm/4.42.0 CPython/3.6.4

File hashes

Hashes for deepface-0.0.31-py3-none-any.whl
Algorithm Hash digest
SHA256 938e4e30cdbce17c28ec7661d8ee76a300105dd8dba0b2069e92392207cf2803
MD5 3c8d3a13e6b0e61122d74778e73d543d
BLAKE2b-256 e487bf0143fac47bb4ec1a35db271babfde59393f8515a54922dd4233afa8709

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