A Python project to detect facial emotions from images using a simple machine learning model (Decision Tree Classifier) and display results in a user-friendly Tkinter GUI.
This project uses grayscale facial images to train a model that classifies them into one of the 7 basic human emotions:
- 😠 Anger
- 😒 Contempt
- 🤢 Disgust
- 😨 Fear
- 😀 Happiness
- 😢 Sadness
- 😲 Surprise
It provides a GUI for uploading a facial image and viewing the predicted emotion.
Place your emotion image folders in the ml project directory as follows:
ml project/
├── anger/
├── contempt/
├── disgust/
├── fear/
├── happiness/
├── sadness/
└── surprise/
Each folder should contain .jpg/.png/etc. images corresponding to that emotion.
Install the following Python libraries:
pip install opencv-python scikit-learn numpy joblib Pillow- Ensure your dataset is placed in the proper folder structure.
- Run the Python script:
python main.py- A GUI window will open. Click "Upload Image" to test an image from your system.
-
Decision Tree Classifier trained on 48x48 grayscale facial images
-
Preprocessing includes:
- Grayscale conversion
- Resizing
- Flattening images for training
-
Uses Tkinter GUI for easy image upload and prediction
-
Automatically saves the trained model as
emotion_model.pkl -
Handles non-image and unreadable files gracefully
- Upload any image containing a face
- The GUI displays the image and the predicted emotion
At the end of training, the console will display:
- ✅ Accuracy Score
- 📋 Classification Report
Example:
Accuracy: 0.81
Classification Report:
precision recall f1-score support
happiness 0.89 0.85 0.87 52
anger 0.78 0.82 0.80 50
...
emotions = {
0: 'anger',
1: 'contempt',
2: 'disgust',
3: 'fear',
4: 'happiness',
5: 'sadness',
6: 'surprise'
}- Upgrade model to Convolutional Neural Network (CNN) for better accuracy
- Add webcam live detection
- Include data augmentation
- Save predictions or logs to a file
Aditya10045 Made with ❤️ and Python
This project is open-source and free to use for educational or personal purposes.
