Skip to main content

Django app for face recognition authentication using DeepFace

Project description

Django DeepFace

A Django app for face recognition authentication using DeepFace and pgvector.

PyPI version Python Support Django Support License: MIT AI-assisted

Features

  • 🔐 Face recognition authentication alongside traditional password authentication
  • 📸 Capture face images via webcam or file upload
  • 🚀 Fast face matching using pgvector similarity search
  • 👤 Support for multiple face images per user (up to 4)
  • 🎨 Modern, responsive UI with Bootstrap 5
  • 🔒 Secure storage and processing of biometric data

Requirements

  • Python 3.8+
  • Django 4.2+
  • PostgreSQL with pgvector extension
  • A working webcam (for face capture features)

Installation

  1. Install the package (recommended with uv):
# Using uv (recommended)
uv pip install django-deepface

# Or using pip
pip install django-deepface
  1. Install system dependencies for face recognition:
# On Ubuntu/Debian
sudo apt-get update
sudo apt-get install -y libgl1-mesa-glx libglib2.0-0
sudo apt-get install -y libhdf5-dev libhdf5-serial-dev
sudo apt-get install -y python3-h5py
sudo apt-get install -y libopenblas-dev

# On macOS
brew install cmake
brew install hdf5
  1. Install tf-keras (required for TensorFlow 2.19.0+):
uv pip install tf-keras
# or
pip install tf-keras
  1. Set up PostgreSQL with pgvector:
# Install pgvector extension
sudo apt-get install postgresql-14-pgvector  # Adjust version as needed

# Create extension in your database
psql -U postgres -d your_database -c "CREATE EXTENSION IF NOT EXISTS vector;"

Quick Start

  1. Add django_deepface to your INSTALLED_APPS:
INSTALLED_APPS = [
    ...
    'django_deepface',
    ...
]
  1. Configure your database to use PostgreSQL:
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql',
        'NAME': 'your_database',
        'USER': 'your_user',
        'PASSWORD': 'your_password',
        'HOST': 'localhost',
        'PORT': '5432',
    }
}
  1. Add URL patterns:
from django.urls import path, include

urlpatterns = [
    ...
    path('auth/', include('django_deepface.urls')),
    ...
]
  1. Run migrations:
python manage.py migrate django_deepface
  1. Configure media files settings:
MEDIA_URL = '/media/'
MEDIA_ROOT = BASE_DIR / 'media'
  1. Add media URL patterns (development only):
from django.conf import settings
from django.conf.urls.static import static

if settings.DEBUG:
    urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

Usage

User Registration with Face

  1. Navigate to /auth/profile/
  2. Upload up to 4 face images for better recognition accuracy
  3. Images are processed and stored as embeddings for fast matching

Face Login

  1. Navigate to /auth/login/
  2. Enter your username
  3. Check "Use Face Login"
  4. Allow webcam access and capture your face
  5. Click "Login"

Management Commands

Add face images from a directory tree:

python manage.py add_image_tree /path/to/faces --clear

Directory structure should be:

/path/to/faces/
├── username1/
│   ├── face1.jpg
│   └── face2.jpg
└── username2/
    └── face1.jpg

Configuration

Settings

# Maximum number of face images per user
DEEPFACE_MAX_FACES = 4

# Face recognition model (default: VGG-Face)
DEEPFACE_MODEL = "VGG-Face"

# Detection backend (default: retinaface)
DEEPFACE_DETECTOR = "retinaface"

# Similarity threshold (default: 0.3)
DEEPFACE_THRESHOLD = 0.3

Models

The app provides two main models:

  • UserProfile: Extends the User model (optional)
  • Identity: Stores face embeddings and images

API Reference

Views

  • face_login: Handle face-based authentication
  • profile_view: Manage user's face images
  • delete_face: Remove a specific face image

Forms

  • FaceLoginForm: Combined username/password/face login form
  • FaceImageUploadForm: Face image upload form

Development

Setting up development environment

# Clone the repository
git clone https://github.com/topiaruss/django-deepface.git
cd django-deepface

# Create virtual environment
python -m venv venv
source venv/bin/activate  # On Windows: venv\Scripts\activate

# Install development dependencies (recommended with uv)
uv pip install -e ".[dev]"
# or
pip install -e ".[dev]"

# Run tests
pytest

# Run with coverage
pytest --cov=django_deepface

Running tests

# Run all tests
pytest

# Run with coverage report
pytest --cov=django_deepface --cov-report=html

# Run specific test
pytest django_deepface/tests/test_views.py::TestProfileView

Contributing

  1. Fork the repository
  2. Create your feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'Add some amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

Security Considerations

  • Face embeddings are stored as vectors, not actual images
  • Always use HTTPS in production
  • Consider privacy regulations (GDPR, etc.) when storing biometric data
  • Implement proper access controls and audit logging
  • Regular security updates for dependencies

AI-Generated Code Disclosure

This project includes portions of code that were generated with the assistance of large language models—specifically , Claude 3.7 Sonnet, Claude 4 Opus. These tools were used to accelerate scaffolding, explore idiomatic patterns, and propose implementations for specific challenges.

All AI-generated code has been reviewed, integrated, and tested by the author. Transparency is important: this project makes no attempt to conceal the involvement of generative AI, and welcomes scrutiny and feedback.

If you're curious about the design, want to critique the use of AI in open-source development, or have experience with similar approaches, the author invites comments and contributions from both the AI and broader developer communities.

Your insights—technical, ethical, or otherwise—are welcome.

License

This project is licensed under the MIT License - see the LICENSE file for details.

Acknowledgments

  • DeepFace for face recognition
  • pgvector for similarity search
  • My friends in the Django and python community for soooo... much goodness

Support

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django_deepface-0.0.2.tar.gz (67.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

django_deepface-0.0.2-py3-none-any.whl (66.0 kB view details)

Uploaded Python 3

File details

Details for the file django_deepface-0.0.2.tar.gz.

File metadata

  • Download URL: django_deepface-0.0.2.tar.gz
  • Upload date:
  • Size: 67.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for django_deepface-0.0.2.tar.gz
Algorithm Hash digest
SHA256 87e610d5e0fd650af61f49696fbb337d54d50a7903a8b910336b7d5f3a9fdb47
MD5 6d1ee860868b415a053a065b68d642cc
BLAKE2b-256 8395f3a3a566030b7658010d7728c157920e60754dc885a2cd639c45fcefc49a

See more details on using hashes here.

Provenance

The following attestation bundles were made for django_deepface-0.0.2.tar.gz:

Publisher: ci.yml on topiaruss/django-deepface

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file django_deepface-0.0.2-py3-none-any.whl.

File metadata

File hashes

Hashes for django_deepface-0.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 2e4674a34f7eb8bebe0c26586d1a76f0219524bb7031612924dd87cfd274a287
MD5 b01675fd2d72d585b1d970fd39340c69
BLAKE2b-256 8d1aa8043e186b4fa75850ec32c14d4f1ff878d5e2e30665bb4f1642aae7d04a

See more details on using hashes here.

Provenance

The following attestation bundles were made for django_deepface-0.0.2-py3-none-any.whl:

Publisher: ci.yml on topiaruss/django-deepface

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page