Skip to main content

Face Detection For Python

This package implements parts of Google®'s MediaPipe models in pure Python (with a little help from Numpy and PIL) without Protobuf graphs and with minimal dependencies (just TF Lite and Pillow).

Models and Examples

The package provides the following models:

  • Face Detection

Face detection example

  • Face Landmark Detection

Face landmark example

  • Iris Landmark Detection

Iris landmark example

  • Iris recoloring example

Iris recoloring example

Motivation

The package doesn't use the graph approach implemented by MediaPipe and is therefore not as flexible. It is, however, somewhat easier to use and understand and more accessible to recreational programming and experimenting with the pretrained ML models than the rather complex MediaPipe framework.

Here's how face detection works and an image like shown above can be produced:

from fdlite import FaceDetection, FaceDetectionModel
from fdlite.render import Colors, detections_to_render_data, render_to_image 
from PIL import Image

image = Image.open('group.jpg')
detect_faces = FaceDetection(model_type=FaceDetectionModel.BACK_CAMERA)
faces = detect_faces(image)
if not len(faces):
    print('no faces detected :(')
else:
    render_data = detections_to_render_data(faces, bounds_color=Colors.GREEN)
    render_to_image(render_data, image).show()

While this example isn't that much simpler than the MediaPipe equivalent, some models (e.g. iris detection) aren't available in the Python API.

Note that the package ships with five models:

  • FaceDetectionModel.FRONT_CAMERA - a smaller model optimised for selfies and close-up portraits; this is the default model used
  • FaceDetectionModel.BACK_CAMERA - a larger model suitable for group images and wider shots with smaller faces
  • FaceDetectionModel.SHORT - a model best suited for short range images, i.e. faces are within 2 metres from the camera
  • FaceDetectionModel.FULL - a model best suited for mid range images, i.e. faces are within 5 metres from the camera
  • FaceDetectionModel.FULL_SPARSE - a model best suited for mid range images, i.e. faces are within 5 metres from the camera

The FaceDetectionModel.FULL and FaceDetectionModel.FULL_SPARSE models are equivalent in terms of detection quality. They differ in that the full model is a dense model whereas the sparse model runs up to 30% faster on CPUs. On a GPU, both models exhibit similar runtime performance. In addition, the dense full model has slightly better Recall, whereas the sparse model features a higher Precision.

If you don't know whether the image is a close-up portrait or you get no detections with the default model, try using the BACK_CAMERA-model instead.

Installation

The latest release version is available in PyPI and can be installed via:

pip install -U face-detection-tflite

The package can be also installed from source by navigating to the folder containing setup.py and running

pip install .

from a shell or command prompt.

Metadata

Release files for face-detection-tflite 0.6.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 face-detection-tflite 0.6.0
File Size Uploaded
face-detection-tflite-0.6.0.tar.gz 7.3 MB Details

Built distribution (wheel)

Table of built distributions (wheels) for face-detection-tflite 0.6.0
File Interpreter ABI Platform
face_detection_tflite-0.6.0-py3-none-any.whl Python 3 none any Details

Total release size: 14.2 MB

Release files / face-detection-tflite-0.6.0.tar.gz

Download URL face-detection-tflite-0.6.0.tar.gz
Size 7.3 MB
Tags Source
SHA-256 checksum
How to use checksums
7cb9ce9994b8353fffc963e12a155af6c6c6187ae5b7312fddf33fe7c6e48f11
BLAKE2b-256 checksum
How to use checksums
90b7ea63b3707e23591c95ff050835acf34538ea888a3d85d04466828e8654ad
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.9.7

Release files / face_detection_tflite-0.6.0-py3-none-any.whl

Download URL face_detection_tflite-0.6.0-py3-none-any.whl
Size 6.9 MB
Tags Python 3
SHA-256 checksum
How to use checksums
1eeb3d6a51dffc43116af273f3209c6aa61d873986ba22f81f76b9ae7a4fe1ce
BLAKE2b-256 checksum
How to use checksums
4860108c6c287763d6067c60d196f7dd9bf397dcb8a3dd425a004ca858b08966
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.9.7

Release history Release notifications | RSS feed

This release

0.6.0 This release

2 release files

0.5.1

2 release files

0.5.0

2 release files

0.4.0

2 release files

0.3.0

2 release files

0.2.0

2 release files

0.1.0

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