This project builds a deep learning model using a pre-trained EfficientNet architecture to classify human facial expressions. The model aims to recognize emotions such as happiness, sadness, anger, fear, and more.
- Project Overview
- Project Structure
- Installation
- Data Preparation
- Model Training
- Evaluation
- Real-Time Emotion Prediction
- Usage
- Acknowledgments
This project leverages the Keras deep learning library and EfficientNetB0 as a pre-trained model for emotion classification from facial images. Using data augmentation and transfer learning, the model is trained to classify images into several emotional categories. The final model can optionally be used for real-time emotion recognition via a webcam.
- data: Directory containing the dataset of face images with labels.
- main.py: Main script containing the model, data preprocessing, training, and evaluation code.
- best_model.h5: File where the best model's weights are saved during training.
-
Clone the repository and navigate into the project directory:
git clone <repository-url> cd <repository-directory>
-
Install the required Python packages:
pip install -r requirements.txt
-
(Optional) If you’re running the project on Google Colab, you can upload your dataset and configure paths accordingly.
-
Place the images in a directory structure like:
/data ├── emotion1 │ ├── img1.jpg │ ├── img2.jpg │ └── ... ├── emotion2 │ ├── img3.jpg │ ├── img4.jpg │ └── ... └── ...
-
Define the data directory in the script:
data_dir = '/path-to-data-directory'
-
The function
convert_img_to_df
converts the image dataset into a DataFrame, where each entry contains the file path and label.
- The images are augmented using Keras'
ImageDataGenerator
, which performs data augmentation and rescaling. - EfficientNetB0 is used as a base model to leverage pre-trained weights. A few additional layers are added on top, including a Dense layer for classification.
- The model is compiled with
categorical_crossentropy
loss and trained using anEarlyStopping
andModelCheckpoint
callback to save the best model.
After training, the model is evaluated on a test set. The script provides:
- Training history visualization of accuracy and loss.
- Classification report detailing precision, recall, and F1-score for each class.
The project includes optional code for real-time emotion recognition using a webcam. To use this feature:
- Uncomment the code in the
main.py
file under "Real-Time Prediction." - Run the model with a connected webcam. The model will predict and display the emotion on the live webcam feed.
Note: OpenCV is required for accessing the webcam, which may need to be installed via pip install opencv-python
.
To run the code:
python main.py
Or in Google Colab:
- Upload
main.py
and dataset files. - Run the cells in sequence to preprocess, train, and evaluate the model.
- This project uses EfficientNetB0 pre-trained model for transfer learning.
- OpenCV library is used for real-time video capture.
This README provides an overview and usage instructions for setting up, running, and evaluating the model. Adjust paths as needed based on your environment setup.