Skip to content

Latest commit

 

History

4 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 

Repository files navigation

Face Recognition using FaceNet and SVM

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.

🎯 Project Overview

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.

📊 Dataset

The dataset contains images of 9 different people organized into training and testing sets:

Classes (9 categories):

  • 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 Structure:

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]

🏗️ Model Architecture

The face recognition pipeline consists of three main components:

1. Face Detection - MTCNN

  • 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

2. Feature Extraction - FaceNet

  • 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

3. Classification - SVM

  • Support Vector Machine with normalization
  • Trained on FaceNet embeddings
  • Uses LabelEncoder for class mapping
  • High accuracy in distinguishing between the 9 individuals

🚀 Getting Started

Prerequisites

mtcnn
scikit-learn
tensorflow/keras
PIL (Pillow)
opencv-python
numpy
matplotlib
pickle

Installation

  1. Clone the repository:
git clone https://github.com/kenzn2/Face_Recognition.git
cd Face_Recognition
  1. Install required packages:
pip install mtcnn scikit-learn tensorflow pillow opencv-python numpy matplotlib
  1. Download the FaceNet model:
    • Place facenet_keras.h5 in the model/ directory

Usage

Training the Model

# 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 features

Making Predictions

from 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)

Face Recognition Pipeline

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]

📁 Project Structure

├── 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)

🔧 Key Features

  • 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

📈 Model Performance

  • 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

🎨 Data Processing

  • 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

🧪 Testing

The notebook includes comprehensive testing:

  • Individual image testing with visualization
  • Performance evaluation on test dataset
  • Prediction confidence analysis
  • Visual verification of results

🤝 Contributing

  1. Fork the repository
  2. Create a feature branch (git checkout -b feature/improvement)
  3. Commit your changes (git commit -am 'Add new feature')
  4. Push to the branch (git push origin feature/improvement)
  5. Create a Pull Request

📄 License

This project is open source and available under the MIT License.

🙏 Acknowledgments

  • 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

📞 Contact

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.

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages