Skip to main content

MTCNN - Multitask Cascaded Convolutional Networks for Face Detection and Alignment

PyPI version Documentation Status Test Status Pylint Check PyPI Downloads

Overview

Example

MTCNN is a robust face detection and alignment library implemented for Python >= 3.10 and TensorFlow >= 2.12, designed to detect faces and their landmarks using a multitask cascaded convolutional network. This library improves on the original implementation by offering a complete refactor, simplifying usage, improving performance, and providing support for batch processing.

This library is ideal for applications requiring face detection and alignment, with support for both bounding box and landmark prediction.

Installation

MTCNN can be installed via pip:

pip install mtcnn

MTCNN requires Tensorflow >= 2.12. This external dependency can be installed manually or automatically along with MTCNN via:

pip install mtcnn[tensorflow]

Usage Example

from mtcnn import MTCNN
from mtcnn.utils.images import load_image

# Create a detector instance
detector = MTCNN(device="CPU:0")

# Load an image
image = load_image("ivan.jpg")

# Detect faces in the image
result = detector.detect_faces(image)

# Display the result
print(result)

Output example:

[
    {
        "box": [277, 90, 48, 63],
        "keypoints": {
            "nose": (303, 131),
            "mouth_right": (313, 141),
            "right_eye": (314, 114),
            "left_eye": (291, 117),
            "mouth_left": (296, 143)
        },
        "confidence": 0.9985
    }
]

Models Overview

MTCNN uses a cascade of three networks to detect faces and facial landmarks:

  • PNet (Proposal Network): Scans the image and proposes candidate face regions.
  • RNet (Refine Network): Refines the face proposals from PNet.
  • ONet (Output Network): Detects facial landmarks (eyes, nose, mouth) and provides a final refinement of the bounding boxes.

All networks are implemented using TensorFlow’s functional API and optimized to avoid unnecessary operations, such as transpositions, ensuring faster and more efficient execution.

Documentation

The full documentation for this project is available at Read the Docs.

Citation

If you use this library for your research or projects, please consider citing the original work:

@article{7553523,
    author={K. Zhang and Z. Zhang and Z. Li and Y. Qiao}, 
    journal={IEEE Signal Processing Letters}, 
    title={Joint Face Detection and Alignment Using Multitask Cascaded Convolutional Networks}, 
    year={2016}, 
    volume={23}, 
    number={10}, 
    pages={1499-1503}, 
    keywords={Benchmark testing;Computer architecture;Convolution;Detectors;Face;Face detection;Training;Cascaded convolutional neural network (CNN);face alignment;face detection}, 
    doi={10.1109/LSP.2016.2603342}, 
    ISSN={1070-9908}, 
    month={Oct}
}

You may also reference the original GitHub repository that this project was based on (including the networks weights):
Original MTCNN Implementation by Kaipeng Zhang

And the FaceNet's implementation that served as inspiration: Facenet's MTCNN implementation

About this project

The code for this project was created to standardize face detection and provide an easy-to-use framework that helps the research community push the boundaries of AI knowledge. Learn more about the author of this code on Iván de Paz Centeno's website

If you find this project useful, please consider supporting it through GitHub Sponsors.

Sponsor

Your support will help cover costs related to improving the codebase, adding new features, and providing better documentation.

License

This project is licensed under the MIT License.

Release files for mtcnn 1.0.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 mtcnn 1.0.0
File Size Uploaded
mtcnn-1.0.0.tar.gz 1.9 MB Details

Built distribution (wheel)

Table of built distributions (wheels) for mtcnn 1.0.0
File Interpreter ABI Platform
mtcnn-1.0.0-py3-none-any.whl Python 3 none any Details

Total release size: 3.8 MB

Release files / mtcnn-1.0.0.tar.gz

Download URL mtcnn-1.0.0.tar.gz
Size 1.9 MB
Tags Source
SHA-256 checksum
How to use checksums
08428bf8e1ae9827d43a40bb0246b57f2239e3572d3742f472ae9924896c6419
BLAKE2b-256 checksum
How to use checksums
6737b0f60411b6a37dcd5122bbe05c9aa45f271bcc8129caa45ee1012251905d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.10.15

Release files / mtcnn-1.0.0-py3-none-any.whl

Download URL mtcnn-1.0.0-py3-none-any.whl
Size 1.9 MB
Tags Python 3
SHA-256 checksum
How to use checksums
0a96b4868e56db9ae984449519642be6dba03240e608a67e928ebb47833e9144
BLAKE2b-256 checksum
How to use checksums
dc7e0b2b688a9e2d353a661b617b12d00d9af29f877b57c8e4a3cbe447483b46
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.10.15

Release history Release notifications | RSS feed

This release

1.0.0 This release

2 release files

0.1.1

2 release files

0.1.0

3 release files

0.0.9

1 release file

0.0.8

1 release file

0.0.7

1 release file

0.0.6

1 release file

0.0.5

1 release file

0.0.4

1 release file

0.0.3

1 release file

0.0.2

1 release file

0.0.1

1 release file

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