A professional deep learning project that detects facial mood expressions (Happy vs Sad) from images using a Convolutional Neural Network (CNN). Features real-time prediction via camera or file upload with an intuitive GUI.
- π― High-Accuracy Classification - 80%+ accuracy on binary mood detection (Happy/Sad)
- πΈ Real-Time Camera Detection - Live video feed with instant mood prediction
- π File Upload Support - Predict mood from image files (JPG, PNG, BMP)
- π₯οΈ Modern GUI - Clean dark/light theme interface with CustomTkinter
- π GPU Optimized - Automatic GPU detection and memory management
- π Data Augmentation - Prevents overfitting on small datasets
- π§ Easy Training - Simple notebook-based training pipeline
- π TensorBoard Logging - Monitor training metrics in real-time
| Technology | Version | Purpose |
|---|---|---|
| Python | 3.10+ | Core programming language |
| TensorFlow | 2.16+ | Deep learning framework |
| Keras | 3.0+ | Neural network API |
| OpenCV | 4.8+ | Image processing & camera capture |
| CustomTkinter | 5.0+ | Modern GUI framework |
| Matplotlib | 3.7+ | Training visualization |
| Pillow | 10.0+ | Image display |
Image-Classification/
βββ README.md # Project documentation
βββ LICENSE # MIT License
βββ .gitignore # Git ignore patterns
βββ requirements.txt # Python dependencies
βββ pyproject.toml # Packaging metadata
βββ Dockerfile # Docker image definition
β
βββ src/ # Core package
β βββ __init__.py
β βββ cli.py # CLI: train / predict
β βββ train.py # Training pipeline (scriptable)
β βββ inference.py # Inference utilities (MoodPredictor)
β βββ gui/ # GUI package
β βββ app.py # GUI application implementation
β
βββ notebooks/ # Notebooks for experiments
β βββ train.ipynb # Training notebook
β
βββ models/ # Trained models
β βββ mood.h5 # Trained binary classifier
β
βββ mood/ # Dataset
β βββ happy/ # happy images
β βββ sad/ # sad images
β
βββ logs/ # TensorBoard logs
- Python 3.10+
- pip package manager
- (Optional) NVIDIA GPU
The raw dataset (mood/) and pre-trained model weights (models/mood.h5) are not included in the public repository for size or privacy reasons. If these directories or files are not present in your clone and you need access, please contact the repository owner at 3bsalam0@gmail.com or open a GitHub issue to request access. You can also recreate the dataset locally by placing images in mood/happy/ and mood/sad/ and training a model with python -m src.cli train.
# 1. Clone repository
git clone https://github.com/3bsalam-1/Image-Classification.git
cd Image-Classification
# 2. Create virtual environment
python -m venv venv
venv\Scripts\activate # Windows
source venv/bin/activate # macOS/Linux
# 3. Install dependencies
pip install -r requirements.txt
# 4. (Optional) Enable GPU support
pip install tensorflow[and-cuda]Option A: Jupyter Notebook (Recommended)
jupyter notebook notebooks/train.ipynbExecute all cells for interactive training with visualizations and experiments.
Option B: CLI / Python Script
# Using the CLI (recommended):
python -m src.cli train --epochs 50
# Or run the training script directly:
python src/train.pyExpected Output:
Dataset loaded: 9 batches
Starting training...
Epoch 1/50 - loss: 0.689, accuracy: 0.562, val_loss: 0.683, val_accuracy: 0.583
...
=== Test Set Results ===
Precision: 0.85
Recall: 0.78
Accuracy: 0.81
Loss: 0.4921
# Run GUI locally (module entrypoint)
# Alternative: run the file directly (supported)
# python src/gui/app.py
python -m src.gui.appRequirements: The GUI requires customtkinter (already listed in requirements.txt); install with pip install customtkinter if you plan to use the GUI.
Features:
- πΈ Live camera detection
- π Image file selection
- π Happy / π’ Sad classification
- Dark/Light theme support
from tensorflow.keras.models import load_model
import cv2
import numpy as np
# Load model
model = load_model('models/mood.h5')
# Predict from image
image = cv2.imread('image.jpg')
image_resized = cv2.resize(image, (256, 256)) / 255.0
prediction = model.predict(np.expand_dims(image_resized, 0))[0][0]
mood = 'Happy' if prediction <= 0.5 else 'Sad'
confidence = (1 - prediction) if prediction <= 0.5 else prediction
print(f"Mood: {mood}, Confidence: {confidence:.2%}")Input: 256Γ256 RGB Image
β
[Data Augmentation]
RandomFlip(0.5) + Rotation(0.15) + Zoom(0.15)
β
[Conv Block 1] 32 filters β MaxPool β Dropout(0.25)
[Conv Block 2] 64 filters β MaxPool β Dropout(0.25)
[Conv Block 3] 128 filters β MaxPool β Dropout(0.25)
β
[Dense 1] 512 units β Dropout(0.4)
[Dense 2] 256 units β Dropout(0.4)
[Dense 3] 128 units β Dropout(0.3)
β
[Output] 1 unit β Sigmoid
β
Probability: [0, 1]
| Parameter | Value | Reason |
|---|---|---|
| Loss | BinaryCrossentropy | Binary classification |
| Activation (Output) | Sigmoid | Probability in [0,1] |
| Optimizer | Adam (lr=0.0005) | Stable convergence |
| Regularization | L2(0.0001) | Prevents overfitting |
| Data Augmentation | Yes | Small dataset (440 images) |
| Class Weights | {0: 1.1, 1: 0.9} | Balance classes |
| Early Stopping | Patience=7 | Avoid overfitting |
| Max Epochs | 50 | With early stopping |
| Metric | Value |
|---|---|
| Accuracy | 81.0% |
| Precision | 0.85 |
| Recall | 0.78 |
| Loss (BCE) | 0.4921 |
| Aspect | Value |
|---|---|
| Total Images | 440 |
| Happy | 214 (49%) |
| Sad | 226 (51%) |
| Balance Ratio | 1.06:1 β |
| Image Size | 256Γ256 |
| Corruption | 0% β |
| Split | 70/20/10 |
pip install tensorflow[and-cuda]
# Or with conda:
conda install tensorflow-gpupip install -r requirements.txt# Train first using the CLI or script
python -m src.cli train
# or
python src/train.pypip install customtkinter --upgrade- Grant camera access in OS settings
- Try different USB port for external camera
- Place happy images in
mood/happy/ - Place sad images in
mood/sad/ - Retrain:
python -m src.cli train
Supported formats: JPG, PNG, BMP
| Component | CPU | GPU |
|---|---|---|
| Training Time/Epoch | 1-5 min | 5-15 sec |
| Speedup | 1x | 10-50x β |
MIT License - see LICENSE file for details.
Ahmed Abdulsalam
- GitHub: @3bsalam-1
- Email: 3bsalam0@gmail.com
- TensorFlow - Deep learning framework
- Keras - Neural networks API
- OpenCV - Computer vision
- CustomTkinter - Modern GUI
- Matplotlib - Visualization
- Pandas - Data processing
β Star this repo if you find it helpful!
Made with β€οΈ by Ahmed Abdulsalam