This project implements a facial expression recognition system using various machine learning and deep learning techniques. The goal is to classify facial expressions into one of the seven emotions: angry, disgust, fear, happy, neutral, sad, and surprise.
- Python 3.x
- Git
- Jupyter Notebook
First, clone the repository to your local machine:
git clone https://github.com/Tharun-Ninja/facial-expression-recognition.git
cd facial-expression-recognitionCreate a virtual environment to manage your project dependencies:
python -m venv venv
# Activate the virtual environment
# On Windows
venv\Scripts\activate
# On macOS/Linux
source venv/bin/activateOnce the virtual environment is activated, install the required packages:
pip install -r requirements.txtOpen and run the tharun.ipynb file in Jupyter Notebook. If you encounter the following error:
TypeError: OneHotEncoder.__init__() got an unexpected keyword argument 'sparse'Fix it by navigating to the following file:
code .ml_env/lib/python3.12/site-packages/lazypredict/Supervised.pyChange line 98 from:
("encoding", OneHotEncoder(handle_unknown="ignore", sparse=False)),to:
("encoding", OneHotEncoder(handle_unknown="ignore", sparse_output=False)),Follow the instructions within the notebook to:
- Load the dataset.
- Preprocess the images.
- Extract features.
- Train the model.
- Evaluate its performance.
- FER2013 Dataset: Contains 35,887 grayscale images (48x48 pixels) labeled with seven emotions.
- Preprocessing: Includes resizing, normalization, and data augmentation (flipping, rotation, Gaussian noise).
- Pixel Intensity Distribution: Analyzed pixel intensity distributions.
- Correlation Heatmap: Identified relationships among pixel intensities.
- PCA: Applied Principal Component Analysis for dimensionality reduction.
- Normalization: Scaled images to the range [0, 1].
- Class Label Indexing: Assigned integer labels to emotion classes.
- Data Augmentation Techniques:
- Random rotation (-30° to 30°)
- Horizontal and vertical flips
- Gaussian noise
- Input Layer: 48x48 grayscale images.
- Convolutional Blocks:
- Block 1: Conv2D (32 filters, 3x3), BatchNormalization, MaxPooling2D, Dropout (0.25).
- Block 2: Conv2D (64 filters, 3x3), BatchNormalization, MaxPooling2D, Dropout (0.25).
- Block 3: Conv2D (128 filters, 3x3), BatchNormalization, MaxPooling2D, Dropout (0.25).
- Output Layer: Flattened features for classification.
- Features extracted using CNN were scaled with StandardScaler.
- Trained a Support Vector Machine (SVM) classifier on the scaled features.
- Extracted histogram of oriented gradients (HOG) features from images.
- Trained a Random Forest Classifier as a baseline model.
- Clustering approach to group facial expression data into seven clusters.
- Applied on HOG features after scaling for standalone classification.
- Utilized the pre-trained VGG16 model (excluding fully connected layers).
- Added custom dense layers for emotion classification.
- Feature Extraction: Used respective models to extract features from images.
- Feature Scaling: Normalized extracted features using StandardScaler.
- Model Training: Trained models on processed features.
- Precision, Recall, F1-score for each emotion class.
- Overall Accuracy and Confusion Matrix to assess performance.
| Model | Accuracy | Weighted F1-Score |
|---|---|---|
| CNN | 62% | 0.62 |
| CNN + SVM | 48% | 0.46 |
- Address class imbalance using advanced augmentation techniques or oversampling.
- Experiment with hybrid models combining CNN with ensemble classifiers.
- Incorporate more advanced transfer learning approaches, such as fine-tuning pre-trained models.
Feel free to fork the repository, make changes, and create pull requests. Contributions are welcome!