A complete real-time face detection and recognition system built from scratch with Python, OpenCV and facenet-pytorch
. Features:
- Multi-face detection in live video (webcam)
- Automatic face crop capture for training
- Embedding extraction using a pretrained FaceNet (InceptionResnetV1)
- Real-time recognition by comparing embeddings against a template database
- “Known” vs. “Unknown” classification with adjustable threshold
face_recognition_system/
│
├── dataset/ # Generated at runtime (not committed)
│ └── joao/ # Training images per identity
│
├── detectors/ # (Optional) Custom detection modules
├── embeddings/ # (Optional) Custom embedding/extraction modules
├── utils/ # (Optional) Helper functions (logging, I/O, etc.)
│
├── .gitignore # Ignores venv, dataset, embeddings, caches
├── requirements.txt # Project dependencies
├── capture_faces.py # Capture face crops from original frame
├── detect_faces_mtcnn.py # Real-time detection test with MTCNN
├── extract_embeddings.py # Extracts and saves embeddings to `embeddings.pt`
├── recognize_faces.py # Real-time recognition using stored templates
└── README.md # This file
- Python 3.8+
- Git
- Integrated or USB webcam
- (Optional) CUDA-enabled GPU for faster inference
pip install -r requirements.txt
Your requirements.txt
might include:
opencv-python
torch
torchvision
facenet-pytorch
pillow
-
Clone the repo
git clone https://github.com/your-username/face_recognition_system.git cd face_recognition_system
-
Create and activate a virtual environment
python -m venv .venv source .venv/bin/activate # macOS/Linux .venv\Scripts\activate # Windows
-
Install dependencies
pip install -r requirements.txt
-
macOS SSL certificates (if you see SSL errors when downloading weights)
- Run Install Certificates.command located in
/Applications/Python 3.x/
.
- Run Install Certificates.command located in
python detect_faces_mtcnn.py
Opens your webcam and draws green boxes around all detected faces.
python capture_faces.py
- Captures 20 face crops (160×160) from the webcam into
dataset/<identity>/
. - Press
ESC
to stop early.
python extract_embeddings.py
- Loads images from
dataset/
and extracts 512-dim embeddings with FaceNet. - Saves a dictionary of tensors to
embeddings.pt
.
python recognize_faces.py
- Loads
embeddings.pt
, computes the average template per identity. - Detects faces, extracts embeddings and compares to templates.
- Displays identity (green) or “Unknown” (red) with distance score.
Sensitivity threshold
Edit at the top of recognize_faces.py
:
threshold = 0.8 # 0.6 = stricter, 1.0 = more lenient
- Change
person_name
incapture_faces.py
(and createdataset/<name>/
). - Run the script to capture faces for the new identity.
- Run
extract_embeddings.py
again to updateembeddings.pt
. - Now
recognize_faces.py
will recognize the new name as well.
- Alignment: use facial landmarks (eyes, nose) to align and normalize faces before embedding.
- Cosine similarity: swap Euclidean distance for
F.cosine_similarity()
for greater robustness. - Logging: record recognition events to CSV for later analysis.
- GUI: consider Streamlit or Flask for a simple web interface.
This project is licensed under the MIT License. See LICENSE for details.
- Fork this repository.
- Create a feature branch:
git checkout -b my-feature
. - Commit your changes:
git commit -m "Add my feature"
. - Push to the branch:
git push origin my-feature
. - Open a Pull Request.
João Victor Tavares Esteves
✉️ joaovtesteves2002@gmail.com
🌐 GitHub