A Python-based facial recognition system that uses deep learning to detect, encode, and identify faces in images. The system employs a two-stage pipeline: encoding known faces and recognizing faces in new images.
This facial recognition system uses the face_recognition library (built on dlib's state-of-the-art face recognition) to create 128-dimensional face encodings and match them against a database of known faces.
The system consists of two main components:
This script processes a dataset of known faces and creates a database of face encodings.
Process:
- Image Loading: Scans the
data/directory organized by person name (each subdirectory represents a person) - Preprocessing:
- Converts images from BGR to RGB color space
- Resizes images to 448x448 pixels (2 × 224) for optimal detection
- Face Detection: Uses either CNN or HOG model to locate faces in images
- CNN model: More accurate, requires GPU with CUDA support
- HOG model: Faster, works on CPU
- Feature Extraction: Generates 128-dimensional face encodings for each detected face
- Serialization: Saves all encodings, names, and filenames to
encodings/encodings.pickle
Key Features:
- Supports multiple image formats (JPG, JPEG, PNG, BMP)
- Handles multiple faces per image
- Error handling for unreadable images or missing faces
- Configurable detection model (CNN/HOG)
This script identifies faces in new images by comparing them against the encoded database.
Process:
- Load Encodings: Retrieves the pre-computed face encodings from the pickle file
- Image Processing: Loads and converts the target image to RGB
- Face Detection: Locates all faces in the image using HOG model
- Face Encoding: Generates 128-dimensional encodings for detected faces
- Matching:
- Compares each face encoding against all known encodings
- Calculates Euclidean distance between encodings
- Uses configurable tolerance threshold (default: 0.5)
- Identification:
- Selects the best match with minimum distance
- Labels face as "Unknown" if no match meets tolerance threshold
- Visualization:
- Draws bounding boxes around detected faces
- Labels each face with the recognized name
Key Features:
- Adjustable tolerance for match strictness (lower = stricter)
- Distance-based matching for better accuracy
- Real-time visual feedback with bounding boxes
- Handles multiple faces in a single image
The system uses dlib's ResNet-based deep learning model which:
- Generates a unique 128-dimensional vector (encoding) for each face
- These encodings capture distinctive facial features
- Faces of the same person produce similar encodings
- Face matching uses Euclidean distance between encodings:
- Distance < 0.5: Strong match (same person)
- Distance 0.5-0.6: Possible match
- Distance > 0.6: Different person
✅ High Accuracy: Uses state-of-the-art deep learning models
✅ Flexible Detection: Support for both CPU (HOG) and GPU (CNN) processing
✅ Multi-Face Processing: Detects and recognizes multiple faces in a single image
✅ Tolerance Control: Adjustable matching threshold for precision tuning
✅ Visual Output: Displays results with labeled bounding boxes
✅ Scalable: Easily add new people by organizing images in folders
✅ Error Handling: Robust handling of corrupted images and edge cases
- Python 3.6+
- pip package manager
- (Optional) CUDA-enabled GPU for CNN model
# Clone the repository
git clone https://github.com/winterwidow/Facial-Recognition.git
cd Facial-Recognition
# Install dependencies
pip install -r requirements.txtopencv-python: Image processing and visualizationface_recognition: Face detection and encoding (wraps dlib)numpy: Numerical operationsimutils: Image manipulation utilities
Organize your training images in the data/ directory:
data/
├── person1/
│ ├── photo1.jpg
│ ├── photo2.jpg
│ └── photo3.png
├── person2/
│ ├── img1.jpg
│ └── img2.jpg
└── person3/
└── portrait.jpg
Each subdirectory name becomes the label for that person.
Run the encoding script to process your dataset:
python encode_faces.pyConfiguration Options (in encode_faces.py):
DATA_DIR: Directory containing person subdirectories (default: "data")OUT_DIR: Output directory for encodings (default: "encodings")MODEL: Detection model - "cnn" for GPU or "hog" for CPU
Output:
- Creates
encodings/encodings.picklecontaining all face data - Prints the number of faces successfully encoded
Modify the IMAGE_PATH in recognize.py to point to your test image, then run:
python recognize.pyConfiguration Options (in recognize.py):
IMAGE_PATH: Path to the image to analyzeTOLERANCE: Matching threshold (default:0.5)- Lower values = stricter matching
- Recommended range: 0.4-0.6
Output:
- Opens a window displaying the image with:
- Green bounding boxes around detected faces
- Labels showing recognized names or "Unknown"
- Press any key to close the window
| Model | Speed | Accuracy | Hardware |
|---|---|---|---|
| HOG | Fast | Good | CPU |
| CNN | Slower | Excellent | GPU (CUDA) |
Recommendation:
- Use HOG for quick processing or CPU-only systems
- Use CNN for maximum accuracy with GPU support
- Image Quality: Higher resolution images improve detection accuracy
- Lighting: Well-lit, front-facing photos work best
- Training Data: 3-5 varied images per person recommended
- Tolerance: Fine-tune based on your use case:
- Security applications: 0.4-0.45 (strict)
- General recognition: 0.5-0.55 (balanced)
- Lenient matching: 0.6+ (may increase false positives)
"No face found in [image]"
- Ensure the face is clearly visible and well-lit
- Try increasing image resolution
- Face may be at extreme angle
"Encodings file not found"
- Run
encode_faces.pyfirst to create the encodings database
Poor recognition accuracy
- Add more training images per person
- Adjust tolerance threshold
- Ensure training images have similar lighting/angles to test images
Facial-Recognition/
├── encode_faces.py # Creates face encoding database
├── recognize.py # Recognizes faces in images
├── requirements.txt # Python dependencies
├── data/ # Training images (organized by person)
├── encodings/ # Generated face encodings (pickle files)
└── README.md # This file
- Real-time video stream recognition
- Web interface for easier interaction
- Support for video file processing
- Database integration for larger datasets
- Confidence scores in recognition output
- Face detection API endpoint
This project is open source and available under the MIT License.
- Built with face_recognition library by Adam Geitgey
- Uses dlib's state-of-the-art face recognition model
- OpenCV for image processing