facenet-pytorch provides pretrained PyTorch models for face detection (MTCNN) and facial recognition (InceptionResnetV1). It simplifies the process of integrating robust face analysis capabilities into Python applications, offering an easy-to-use API for tasks like detecting faces, extracting facial embeddings, and preparing faces for classification. The library is actively maintained, with regular updates to support newer PyTorch versions and address community feedback.
pip install facenet-pytorchVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to use `facenet-pytorch` to detect a face in an image using `MTCNN` and then compute its 512-dimensional embedding using `InceptionResnetV1`. It sets up a PyTorch device (GPU if available, otherwise CPU) and initializes both models. It includes a placeholder for image loading and handles cases where no face is detected.
Ensure your `torch` version is between 2.2.0 and 2.3.0 (`<2.3.0,>=2.2.0`) and `torchvision` is between 0.17.0 and 0.18.0 (`<0.18.0,>=0.17.0`). Consider using a virtual environment and installing `torch` and `torchvision` first, then `facenet-pytorch`.
Upgrade to `facenet-pytorch` version 2.5.3 or newer, where this issue was addressed.
Always convert your image data to a `PIL.Image` object before passing it to `MTCNN` or `InceptionResnetV1`. E.g., `Image.fromarray(numpy_array)` or `Image.open(image_path)`.
While this is often an upstream issue in `facenet-pytorch` itself or its dependencies, users should keep their `numpy` version updated and monitor for future releases that address this warning.
Ensure the package is correctly installed: `pip install facenet-pytorch`. Verify your virtual environment is activated and the import path is `from facenet_pytorch import ...`.
Use a fresh virtual environment. Install `torch` and `torchvision` first with specific compatible versions (e.g., `pip install torch==2.2.0 torchvision==0.17.0`), then install `facenet-pytorch`. Consult `facenet-pytorch`'s PyPI page or GitHub for exact dependency ranges for your version.
Verify the input image contains detectable faces and is properly loaded. Check `MTCNN` parameters like `min_face_size`, `thresholds`, and `image_size` if faces are very small or unusual. Inspect the image processing pipeline to ensure the image is not empty or corrupted. Handle `None` return from `mtcnn()` gracefully.