MoodCam detects human emotions in real time using your webcam and a PyTorch-trained CNN model. This project implements a grayscale emotion recognition system with 7 emotion classes: angry, disgust, fear, happy, sad, surprise, and neutral.
-
Clone repo & open notebook
git clone <your-repo-url> cd moodcam
Open
notebook/01_moodcam_colab.ipynbin Google Colab -
Run setup cell (installs deps, checks data)
-
Train model
bash scripts/train.sh
-
Evaluate model
bash scripts/evaluate.sh
- Check accuracy at
artifacts/metrics/accuracy.txt - Check detailed metrics at
artifacts/metrics/classification_report.csv
- Check accuracy at
-
Generate predictions
bash scripts/infer_test.sh
- Output in
artifacts/submissions/submission.csv
- Output in
-
Run webcam demo locally
bash scripts/webcam.sh
Expected data structure:
data/
├── train/
│ ├── angry/
│ ├── disgust/
│ ├── fear/
│ ├── happy/
│ ├── sad/
│ ├── surprise/
│ └── neutral/
├── test/
│ └── *.jpg
├── train.csv
└── test_template.csv
bash scripts/train.shbash scripts/evaluate.shProduces:
artifacts/metrics/accuracy.txt- Overall accuracyartifacts/metrics/classification_report.csv- Per-class metrics
bash scripts/infer_test.shProduces:
artifacts/submissions/submission.csv- Test predictions
bash scripts/webcam.shStart the FastAPI backend:
uvicorn backend.fastapi_app:app --host 0.0.0.0 --port 8000-
POST /predict - Upload image and get emotion prediction
curl -X POST "http://localhost:8000/predict" \ -H "Content-Type: multipart/form-data" \ -F "file=@image.jpg"
Response:
{ "emotion": "happy", "confidence": 0.921, "all_emotions": { "angry": 0.01, "disgust": 0.02, "fear": 0.01, "happy": 0.92, "sad": 0.02, "surprise": 0.01, "neutral": 0.01 } } -
GET /classes - Get list of emotion classes
-
GET / - Health check
moodcam/
├── README.md
├── requirements.txt
├── config.yaml
├── data/
│ ├── train.csv
│ ├── test_template.csv
│ ├── train/ # class folders: angry/, disgust/, ...
│ └── test/ # test images (no labels)
├── notebook/
│ └── 01_moodcam_colab.ipynb
├── src/
│ ├── config.py
│ ├── data.py
│ ├── transforms.py
│ ├── model.py
│ ├── train.py
│ ├── evaluate.py
│ ├── infer.py
│ ├── webcam_app.py
│ └── utils.py
├── artifacts/
│ ├── checkpoints/
│ ├── logs/
│ ├── metrics/
│ │ ├── accuracy.txt
│ │ └── classification_report.csv
│ └── submissions/
│ └── submission.csv
├── scripts/
│ ├── train.sh
│ ├── evaluate.sh
│ ├── infer_test.sh
│ └── webcam.sh
├── backend/
│ └── fastapi_app.py
└── frontend/
└── (empty; will link to backend later)
- Backbone: ResNet18 with modified first conv layer for 1-channel grayscale input
- Input: 224x224 grayscale images
- Classes: 7 emotions (angry, disgust, fear, happy, sad, surprise, neutral)
- Training: Transfer learning with pretrained weights averaged for grayscale
- Grayscale Processing: End-to-end 1-channel processing for efficiency
- Transfer Learning: ResNet18 pretrained weights adapted for grayscale
- Real-time Inference: OpenCV webcam integration with face detection
- API Ready: FastAPI backend for frontend integration
- Colab Optimized: GPU acceleration with CPU fallback
- Epochs: 25 (configurable)
- Batch Size: 64
- Learning Rate: 1e-3 with AdamW optimizer
- Augmentation: Random horizontal flip, rotation (±10°)
- Early Stopping: 6 epochs patience
- Mixed Precision: Enabled for GPU training
- The system automatically falls back to CPU if CUDA is not available
- Training will be slower but functional
- OpenCV includes the required cascade file
- Path:
cv2.data.haarcascades + "haarcascade_frontalface_default.xml"
- Corrupted images are skipped with a warning
- Check image formats (supports common formats like JPG, PNG)
- Ensure webcam is not being used by another application
- Try different camera indices if multiple cameras are available
The following files are required for evaluation:
-
artifacts/metrics/accuracy.txtaccuracy: 0.8473 -
artifacts/metrics/classification_report.csvclass,precision,recall,f1-score,support angry,0.81,0.79,0.80,1000 disgust,0.70,0.68,0.69,500 ... macro avg,0.79,0.78,0.78,35000 weighted avg,0.83,0.84,0.83,35000
-
artifacts/submissions/submission.csvid,emotion test_001.jpg,happy test_002.jpg,neutral ...
-
notebook/01_moodcam_colab.ipynb- Complete training notebook
- Dataset: MoodCam Challenge (~35k grayscale images, 7 classes)
- Framework: PyTorch + OpenCV
- Model: ResNet18 with grayscale adaptation
- Author: [Your team name]
This project demonstrates modern deep learning techniques for emotion recognition, including transfer learning, grayscale optimization, and real-time inference capabilities.