How datamoo-ai/deepface works
Purpose and Entry Points
DeepFace is a Python facial-recognition and facial-attribute framework. Its public library facade is deepface/DeepFace.py, exported through deepface/__init__.py. Primary operations are verification, database search, embedding representation, face extraction, demographic analysis, and webcam streaming.
A separate REST deployment path begins at deepface/api/src/app.py and deepface/api/src/api.py; container startup is defined by Dockerfile and entrypoint.sh.
Public-API Orchestration
deepface/DeepFace.py presents static, user-facing operations and delegates to task modules rather than embedding model logic directly:
• verification: deepface/modules/verification.py
• identification/search: deepface/modules/recognition.py
• embedding generation: deepface/modules/representation.py
• attributes: deepface/modules/demography.py
This is the principal boundary between callers and the internal pipeline.
Shared Face Processing Pipeline
Detection is the common front end for recognition and analysis. deepface/modules/detection.py selects a detector, produces face regions/landmarks, aligns faces when enabled, and can invoke liveness checks. deepface/modules/preprocessing.py resizes and normalizes detected crops to model-specific input requirements.
Flow: image path/URL/base64/array → image decoding utilities → detection/alignment → padded, normalized face crops → task-specific inference. Core image and filesystem concerns are centralized in deepface/commons/image_utils.py and deepface/commons/folder_utils.py.
Pluggable Model Layer
deepface/modules/modeling.py is the model-selection/factory boundary. It supplies task models to callers and isolates supported model implementations:
• recognition model base and implementations: deepface/models/FacialRecognition.py, deepface/models/facial_recognition/ArcFace.py, deepface/models/facial_recognition/VGGFace.py
• detector base and backends: deepface/models/Detector.py, deepface/models/face_detection/RetinaFace.py, deepface/models/face_detection/OpenCv.py
• demographic models: deepface/models/Demography.py, deepface/models/demography/Age.py
The package wraps external ML ecosystems such as TensorFlow/Keras, OpenCV, Dlib, MTCNN, MediaPipe, RetinaFace, YOLO, and InsightFace-derived models.
Recognition and Search Flows
Representation converts each detected face into an embedding. Verification compares two embeddings using configured distance metrics and thresholds. Recognition searches a local image database: it derives/stores embeddings in a pickle cache and compares an input embedding against that cache. These responsibilities reside in deepface/modules/representation.py, deepface/modules/verification.py, and deepface/modules/recognition.py.
Database indexing/vector services are explicitly outside the repository’s core: integrations with vector indexes/databases are expected to be composed by consumers for large-scale search.
Attributes, Liveness, and Streaming
Demographic inference applies independent age, gender, race, and emotion predictors to detected faces in deepface/modules/demography.py. Optional anti-spoofing uses deepface/models/spoofing/FasNet.py during face processing. deepface/modules/streaming.py drives webcam frames through detection, recognition, and demographic analysis with temporal focus/display behavior.
REST Service and Runtime Boundary
The API layer maps HTTP requests into the same DeepFace operations; routing/service separation is under deepface/api/src/modules/core/routes.py and deepface/api/src/modules/core/service.py. It accepts uploads and image references/encodings, exposing library functionality without a browser UI. The documented production runtime is Gunicorn, optionally packaged in Docker; scripts under scripts/service.sh and scripts/dockerize.sh operate those deployment modes.
Main entry points: DeepFace.py Public facade · representation.py Representation · detection.py Detection and alignment · modeling.py Model factory
Components 19
DeepFace library
- Public facade · Static user-facing operations.
deepface/DeepFace.py - Verification · Compares two face embeddings.
deepface/modules/verification.py - Recognition search · Searches local image databases.
deepface/modules/recognition.py - Representation · Generates face embeddings.
deepface/modules/representation.py - Demographic analysis · Runs age, gender, race, and emotion inference.
deepface/modules/demography.py - Webcam streaming · Processes webcam frames with temporal display behavior.
deepface/modules/streaming.py - Embedding cache · Stores local database embeddings for search.
deepface/modules/recognition.py
Face pipeline
- Image utilities · Decodes paths, URLs, base64, and arrays.
deepface/commons/image_utils.py - Detection and alignment · Selects detectors, extracts faces and landmarks, and optionally checks liveness.
deepface/modules/detection.py - Crop preprocessing · Resizes and normalizes crops for model inputs.
deepface/modules/preprocessing.py
Model layer
- Model factory · Supplies task-specific model implementations.
deepface/modules/modeling.py - Recognition models · Includes ArcFace and VGGFace implementations.
deepface/models/FacialRecognition.py - Detector backends · Includes RetinaFace and OpenCV backends.
deepface/models/Detector.py - Demography models · Includes independent attribute predictors.
deepface/models/Demography.py - Anti-spoofing model · Optional FasNet liveness inference.
deepface/models/spoofing/FasNet.py
REST deployment
- REST application
deepface/api/src/app.py - HTTP routes · Maps uploads and image references to services.
deepface/api/src/modules/core/routes.py - API service · Invokes the shared DeepFace operations.
deepface/api/src/modules/core/service.py - Container startup · Packages and starts the REST service.
Dockerfile
Connections 26
- Public facade → Verification: verify
- Public facade → Recognition search: find
- Public facade → Representation: represent
- Public facade → Demographic analysis: analyze
- Public facade → Webcam streaming: stream
- Verification → Representation: embeddings
- Recognition search → Representation: query embedding
- Recognition search → Embedding cache: read/write
- Representation → Image utilities: decode input
- Representation → Detection and alignment: detect face
- Demographic analysis → Detection and alignment: detect face
- Detection and alignment → Crop preprocessing: aligned crops
- Detection and alignment → Model factory: request detector
- Detection and alignment → Anti-spoofing model: optional liveness
- Representation → Model factory: request recognizer
- Demographic analysis → Model factory: request predictors
- Model factory → Recognition models: select
- Model factory → Detector backends: select
- Model factory → Demography models: select
- Webcam streaming → Detection and alignment: frames
- Webcam streaming → Recognition search: identify
- Webcam streaming → Demographic analysis: analyze
- REST application → HTTP routes: registers
- HTTP routes → API service: dispatch
- API service → Public facade: library calls
- Container startup → REST application: serves via Gunicorn