Face Recognition System with ESP32-CAM Integration
This project implements a real-time face recognition system that can work with both webcam and ESP32-CAM streams. It uses the face_recognition library for facial detection and recognition, with support for liveness detection using a trained deep learning model.
- Face detection and recognition using pre-encoded face embeddings
- ESP32-CAM integration for remote video streaming
- Liveness detection model (optional, can be disabled)
- Support for multiple known faces
- Real-time video processing
- Webcam (for local testing) or ESP32-CAM module
- Computer with Python 3.7+
Install the required Python packages:
pip install face_recognition opencv-python numpy pickle-mixin requests tensorflow scikit-learnNote: On Windows, you may need to install cmake and dlib separately:
pip install cmake
pip install dlibesp32_project/
├── encode_faces.py # Script to generate face encodings from dataset
├── Stream.py # Main script for ESP32-CAM face recognition
├── encodings.pickle # Saved face encodings (generated)
├── label_encoder.pickle # Label encoder for liveness model
├── liveness.model.h5 # Trained liveness detection model
├── dataset/ # Training images organized by person
│ ├── person1/
│ │ └── 1.jpg
│ ├── person2/
│ │ └── 1.jpeg
│ └── ...
└── README.md
- Create a
datasetfolder in the project root - Inside
dataset, create a subfolder for each person you want to recognize - Add face images (JPG/JPEG/PNG) to each person's folder
Example structure:
dataset/
├── kaushal/
│ └── 1.jpg
├── prashant/
│ └── 1.jpeg
└── veena/
└── 1.jpeg
Tips:
- Use clear, well-lit face images
- Multiple images per person improve accuracy
- Images should primarily contain the person's face
Run the encoding script to process your dataset:
python encode_faces.pyThis will:
- Process all images in the
datasetfolder - Generate 128-dimensional face encodings for each detected face
- Save the encodings to
encodings.pickle
If using ESP32-CAM:
- Flash your ESP32-CAM with camera streaming firmware
- Connect the ESP32-CAM to your WiFi network
- Note the IP address assigned to your ESP32-CAM
- Update the
ESP32_URLinStream.py:
ESP32_URL = 'http://YOUR_ESP32_IP/320x240.jpg'Common ESP32-CAM resolutions:
160x120.jpg(QQVGA)320x240.jpg(QVGA)640x480.jpg(VGA)800x600.jpg(SVGA)
For ESP32-CAM streaming:
python Stream.pyThe application will:
- Connect to the ESP32-CAM stream
- Detect faces in each frame
- Compare detected faces against known encodings
- Display the person's name or "Unknown" on the video feed
Controls:
- Press
qto quit the application
In Stream.py, you can adjust the recognition sensitivity:
if face_distances[best_index] < 0.5: # Lower = stricter matching
name = known_names[best_index]- Lower threshold (e.g., 0.4): More strict, fewer false positives
- Higher threshold (e.g., 0.6): More lenient, may increase false positives
The face_recognition library supports two detection models:
- HOG (Histogram of Oriented Gradients) - Faster, CPU-friendly (default)
- CNN (Convolutional Neural Network) - More accurate, GPU recommended
To change the model in encode_faces.py:
boxes = face_recognition.face_locations(rgb, model="cnn") # or "hog"The liveness detection feature is currently disabled in Stream.py. It was designed to distinguish between real faces and spoofed images/videos.
Uncomment the liveness code in Stream.py and ensure you have:
- A trained
liveness.model.h5file - The corresponding
label_encoder.pickle
The model expects input images of size (32, 32, 3).
See the face-recognition-with-liveness-web-login directory for training scripts and datasets.
- Ensure images are clear and well-lit
- Try using
model="cnn"for better detection - Check that the camera is working properly
- Add more training images per person
- Lower the recognition threshold (e.g., from 0.5 to 0.6)
- Ensure training images are similar to test conditions (lighting, angle)
- Verify the ESP32-CAM IP address is correct
- Check that both devices are on the same network
- Ensure the ESP32-CAM firmware is running properly
- Try pinging the ESP32-CAM IP address
- Use a lower resolution stream from ESP32-CAM
- Reduce the frame processing rate
- Use HOG model instead of CNN for face detection
- Ensure all dependencies are installed:
pip install -r requirements.txt - On Windows, install Visual C++ build tools if
dlibfails to install - Consider using a virtual environment to avoid conflicts
- Lower Resolution: Use smaller image sizes (320x240 instead of 640x480)
- Skip Frames: Process every 2nd or 3rd frame instead of every frame
- Detection Model: Use HOG instead of CNN if you don't have a GPU
- Limit Known Faces: Fewer known faces = faster comparison
This project is provided as-is for educational and research purposes.
- face_recognition library by Adam Geitgey
- ESP32-CAM community for camera streaming examples