This project implements a Convolutional Neural Network (CNN) using TensorFlow/Keras for the binary classification of facial emotions (Happy and Sad). It features a robust real-time pipeline that combines the speed of traditional computer vision for face localization with the accuracy of deep learning for final classification.
The core objective of this project was to compare and contrast two approaches to computer vision:
- Deep Learning (CNN): Training a custom model to learn complex emotional features directly from raw image data, providing high classification accuracy.
- Traditional Computer Vision (OpenCV/Haar Cascades): Utilizing pre-trained Haar Cascades for fast, efficient face localization—a critical preprocessing step—and contrasting its ability to perform the final classification task (e.g., simple smile detection vs. nuanced emotion classification).
The final solution is a hybrid model, leveraging the strengths of both: Haar Cascades for speed and the custom CNN for accuracy.
- Custom CNN Architecture: A sequential model trained from scratch for binary image classification.
- Real-Time Detection: Utilizes the device camera to perform live emotion classification.
- Hybrid Pipeline: Employs OpenCV's Haar Cascades for immediate face detection, then crops and feeds the localized face into the CNN for prediction.
- Performance: Achieves high accuracy (e.g., 90%+) on the validation set.
- Robust Preprocessing: Handles OpenCV's BGR-to-RGB color space conversion for seamless integration with TensorFlow/Keras.
The custom CNN is built with TensorFlow's Keras API. The architecture is designed to capture spatial hierarchies in the facial data:
- Input Layer: Expects 256x256 RGB images.
- Convolutional Blocks: Multiple
Conv2Dlayers withReLUactivation andMaxPooling2Dfor feature extraction. - Regularization: Includes
Dropoutlayers to prevent overfitting. - Output Layer: A final
Denselayer withSigmoidactivation for binary classification (0 = Happy, 1 = Sad).
The model was trained for 20 Epochs and utilized an EarlyStopping callback to monitor validation loss, preventing overfitting.
| Metric | Training Result | Validation Result |
|---|---|---|
| Final Accuracy | 98.7% | 93.4% |
| Final Loss | 0.05 | 0.21 |
(Insert a plot of the training history here, showing Loss and Accuracy over epochs)
- Python (3.8+)
- TensorFlow / Keras: Deep Learning framework for model building and training.
- OpenCV (
cv2): Used for real-time video capture, face detection (Haar Cascades), and image preprocessing. - NumPy: Numerical operations.
- Jupyter Notebook: Environment for development and visualization (
Emotion_Detection_CNN.ipynb).
Ensure you have Python installed.
-
Clone the Repository:
git clone https://github.com/ahmedayad0168/Emotion-Detection.git cd Emotion-Detection -
Install Dependencies:
pip install tensorflow opencv-python numpy jupyter matplotlib
This project requires a dataset of Happy and Sad facial images, organized in subdirectories (/data/happy, /data/sad).
The model was trained on a dataset similar to the one available on Kaggle:
The entire workflow, from data loading to real-time execution, is contained within the notebook.
- Launch Jupyter:
jupyter notebook
- Run the Notebook: Open and execute all cells in
img_classifier.ipynb. - Real-Time Demo: The final cell will load the saved model (
new_model.h5), initialize your camera, and display the live emotion detection feed.- Press
qorESCto exit the camera feed.
- Press