MTCNN (Multi-task Cascaded Convolutional Networks)

1.0.0 · active · verified Sun Apr 12

The `mtcnn` library provides a Python implementation of the Multi-task Cascaded Convolutional Networks (MTCNN) for robust face detection and alignment. It is currently at version 1.0.0, supporting Python >= 3.10 and TensorFlow >= 2.12. Releases are infrequent, indicating a mature and stable codebase.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to load an image (using OpenCV, converting to RGB), initialize the MTCNN detector, and use `detect_faces` to find faces and their keypoints. The output `faces` is a list of dictionaries, where each dictionary contains the bounding box, confidence score, and facial keypoints.

import cv2
from mtcnn.mtcnn import MTCNN

# Example image (replace with your path or download one)
# For demonstration, we'll create a dummy image if file not found
try:
    img_path = 'sample_image.jpg' # Replace with a path to a real image
    img = cv2.imread(img_path)
    if img is None:
        # Create a blank image with a simple 'face' if sample_image.jpg not found
        print(f"Warning: '{img_path}' not found. Creating a dummy image.")
        img = 255 * (cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (50, 50)))
        img = cv2.cvtColor(img, cv2.COLOR_GRAY2BGR)
        # Add a simple rectangle to simulate a face
        cv2.rectangle(img, (100, 100), (200, 200), (0, 0, 255), 2)

except Exception as e:
    print(f"Error loading image or creating dummy: {e}")
    # Fallback to a completely black image if even dummy creation fails
    img = (255 * (cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (50, 50))))
    img = cv2.cvtColor(img, cv2.COLOR_GRAY2BGR)

# MTCNN expects RGB images, OpenCV loads BGR by default
img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)

# Initialize the MTCNN detector
detector = MTCNN()

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

# Print detected faces (each face is a dict with 'box', 'confidence', 'keypoints')
for face in faces:
    print(face)

# Optional: Draw bounding boxes and keypoints on the original image
# for face in faces:
#     x, y, width, height = face['box']
#     cv2.rectangle(img, (x, y), (x + width, y + height), (0, 255, 0), 2)
#     for key, value in face['keypoints'].items():
#         cv2.circle(img, value, 2, (0, 0, 255), 2)
# cv2.imshow('Detected Faces', img)
# cv2.waitKey(0)
# cv2.destroyAllWindows()

view raw JSON →