Deep Face Analysis Framework for Face Recognition and Demography
Project description
deepface
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.
Face Recognition
Verify function under the DeepFace interface is used for face recognition.
from deepface import DeepFace
result = DeepFace.verify("img1.jpg", "img2.jpg")
print("Is verified: ", result["verified"])
Modern face recognition pipelines consist of 4 stages: detect, align, represent and verify. Deepface handles all these common stages in the background.
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']
]
result = DeepFace.verify(dataset)
Face recognition models
Face recognition can be handled by different models. Currently, VGG-Face
, Google FaceNet
, OpenFace
and Facebook DeepFace
models are supported in deepface. 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.
vggface_result = DeepFace.verify("img1.jpg", "img2.jpg") #default is VGG-Face
#vggface_result = DeepFace.verify("img1.jpg", "img2.jpg", model_name = "VGG-Face") #identical to the line above
facenet_result = DeepFace.verify("img1.jpg", "img2.jpg", model_name = "Facenet")
openface_result = DeepFace.verify("img1.jpg", "img2.jpg", model_name = "OpenFace")
deepface_result = DeepFace.verify("img1.jpg", "img2.jpg", model_name = "DeepFace")
VGG-Face has the highest accuracy score but it is not convenient for real time studies because of its complex structure. FaceNet is a complex model as well. On the other hand, OpenFace has a close accuracy score but it performs the fastest. That's why, OpenFace is much more convenient for real time studies.
Similarity
These models actually find the vector embeddings of faces. Decision of verification is based on the distance between vectors. 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.
result = DeepFace.verify("img1.jpg", "img2.jpg", model_name = "VGG-Face", distance_metric = "cosine")
result = DeepFace.verify("img1.jpg", "img2.jpg", model_name = "VGG-Face", distance_metric = "euclidean")
result = DeepFace.verify("img1.jpg", "img2.jpg", model_name = "VGG-Face", distance_metric = "euclidean_l2")
Facial Attribute Analysis
Deepface also offers facial attribute analysis including age
, gender
, emotion
(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") #passing nothing as 2nd argument will find everything
#demography = DeepFace.analyze("img4.jpg", ['age', 'gender', 'race', 'emotion']) #identical to the line above
#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"])
Stream
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")
Installation
The easiest way to install deepface is to download it from PyPI.
pip install deepface
Alternatively, you can directly download the source code from this repository. GitHub repo might be newer than the PyPI version.
git clone https://github.com/serengil/deepface.git
cd deepface
pip install -e .
Initial tests are run for Python 3.5.5 on Windows 10 but this is an OS-independent framework. Even though pip handles to install dependent libraries, the framework basically needs the dependencies defined in the requirements. You might need the specified library requirements if you install the source code from scratch.
Playlist
Deepface is mentioned in this youtube playlist.
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.
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
Built Distribution
File details
Details for the file deepface-0.0.11.tar.gz
.
File metadata
- Download URL: deepface-0.0.11.tar.gz
- Upload date:
- Size: 20.1 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
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1110c385fde8764f4cb890bca605ca230a81cf5b1d2257a7f0bdbb45aec31342 |
|
MD5 | 1a27bde53f5668a220ac64b15b0cc769 |
|
BLAKE2b-256 | b30f4925ff6bfaa3363b2a5fc6ac70b64dc4ce4ed1a6742e0c00585d1ae5c102 |
File details
Details for the file deepface-0.0.11-py3-none-any.whl
.
File metadata
- Download URL: deepface-0.0.11-py3-none-any.whl
- Upload date:
- Size: 26.6 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
Algorithm | Hash digest | |
---|---|---|
SHA256 | 00214b2fb591026f31e5053fd86a097a54ca976b546d0e942a70a87d3d68af6e |
|
MD5 | a1274276d4036661d2b7a5f97f916a0c |
|
BLAKE2b-256 | 4260588f04fd3d17e9dbbf6896b108621f58b6d09e7b5ca80ad50f94a3509469 |