A deep learning project for face recognition that combines FaceNet embeddings with Support Vector Machine (SVM) classification to recognize faces of 9 different people including celebrities and Vietnamese personalities.
This project implements a face recognition system that can accurately identify 9 different individuals. The system uses MTCNN for face detection, FaceNet for feature extraction, and SVM for classification, achieving high accuracy in recognizing faces from various photos.
The dataset contains images of 9 different people organized into training and testing sets:
- BillieEilish - International pop star
- BuiAnhTuan - Vietnamese singer
- ChiPu - Vietnamese actress and singer
- DonaldTrump - Former US President
- JustinBieber - International pop star
- NhietBa - Chinese actress
- SelenaGomez - International actress and singer
- SonTung - Vietnamese singer (Sơn Tùng M-TP)
- TaylorSwift - International pop star
dataset/
├── train/
│ ├── BillieEilish/ # 41 images
│ ├── BuiAnhTuan/ # ~12 images
│ ├── ChiPu/ # ~10 images
│ ├── DonaldTrump/ # ~31 images
│ ├── JustinBieber/ # ~7 images
│ ├── NhietBa/ # ~17 images
│ ├── SelenaGomez/ # ~5 images
│ ├── SonTung/ # ~4 images
│ └── TaylorSwift/ # ~16 images
└── test/
└── [test images for evaluation]
The face recognition pipeline consists of three main components:
- Multi-task CNN (MTCNN) for face detection and alignment
- Extracts face bounding boxes from input images
- Resizes faces to 160×160 pixels for FaceNet input
- Pre-trained FaceNet model (
facenet_keras.h5) - Converts face images into 128-dimensional embeddings
- Provides robust facial feature representations
- Model input: 160×160×3 RGB images
- Model output: 128-dimensional feature vectors
- Support Vector Machine with normalization
- Trained on FaceNet embeddings
- Uses LabelEncoder for class mapping
- High accuracy in distinguishing between the 9 individuals
mtcnn
scikit-learn
tensorflow/keras
PIL (Pillow)
opencv-python
numpy
matplotlib
pickle
- Clone the repository:
git clone https://github.com/kenzn2/Face_Recognition.git
cd Face_Recognition- Install required packages:
pip install mtcnn scikit-learn tensorflow pillow opencv-python numpy matplotlib- Download the FaceNet model:
- Place
facenet_keras.h5in themodel/directory
- Place
# Run the main training notebook
jupyter notebook face-recog.ipynb
# The training process includes:
# 1. Face detection and extraction using MTCNN
# 2. Feature extraction using FaceNet
# 3. SVM training on the extracted featuresfrom mtcnn.mtcnn import MTCNN
from sklearn.svm import SVC
import pickle
from keras.models import load_model
# Load models
svm_model = pickle.load(open('svm_model_faceRecog.sav', 'rb'))
faceNetModel = load_model('model/facenet_keras.h5')
# Test on new image
test_image('path/to/image.jpg', faceNetModel, svm_model, out_encoder, in_encoder)def extract_face(filename, required_size=(160,160)):
# Extract and preprocess face using MTCNN
detector = MTCNN()
image = Image.open(filename)
pixels = asarray(image)
results = detector.detect_faces(pixels)
# ... face extraction logic
return face_array
def get_embedding(model, face_pixels):
# Generate FaceNet embeddings
face_pixels = face_pixels.astype('float32')
mean, std = face_pixels.mean(), face_pixels.std()
face_pixels = (face_pixels - mean) / std
samples = expand_dims(face_pixels, axis=0)
yhat = model.predict(samples)
return yhat[0]├── face-recog.ipynb # Main training and testing notebook
├── README.md # Project documentation
├── svm_model_faceRecog.sav # Trained SVM model (generated after training)
├── dataset/ # Face images dataset
│ ├── train/ # Training images by person
│ └── test/ # Test images
├── model/
│ └── facenet_keras.h5 # Pre-trained FaceNet model
└── encoders/ # Label encoders (generated after training)
- Multi-stage Pipeline: Face detection → Feature extraction → Classification
- Robust Face Detection: MTCNN handles various face orientations and lighting
- Deep Feature Extraction: FaceNet provides discriminative 128D embeddings
- Efficient Classification: SVM with normalization for fast and accurate prediction
- Multi-cultural Dataset: Includes both international celebrities and Vietnamese personalities
- Real-time Prediction: Fast inference suitable for real-time applications
- Face Detection: MTCNN effectively detects faces in various conditions
- Feature Quality: FaceNet embeddings provide excellent discrimination between individuals
- Classification: SVM achieves high accuracy on the 9-class recognition task
- Generalization: Model performs well on test images not seen during training
- Face Detection: Automatic face localization and cropping
- Normalization: Pixel value normalization for consistent input
- Augmentation: Natural data augmentation through varied photo conditions
- Preprocessing: Face alignment and resizing to 160×160 pixels
The notebook includes comprehensive testing:
- Individual image testing with visualization
- Performance evaluation on test dataset
- Prediction confidence analysis
- Visual verification of results
- Fork the repository
- Create a feature branch (
git checkout -b feature/improvement) - Commit your changes (
git commit -am 'Add new feature') - Push to the branch (
git push origin feature/improvement) - Create a Pull Request
This project is open source and available under the MIT License.
- FaceNet: Thanks to the original FaceNet research for deep face embeddings
- MTCNN: Multi-task CNN for robust face detection
- Keras/TensorFlow: Deep learning framework
- Scikit-learn: Machine learning library for SVM implementation
For questions or suggestions, please open an issue or contact the repository owner.
Note: This model is trained for educational and research purposes. For production use, consider expanding the dataset and performing additional validation with diverse demographics and conditions.