This project applies machine learning to detect facial emotions in Alzheimer's patients, with the goal of aiding in their care and understanding their emotional states.
It now includes a zero-interaction desktop application: a webcam continuously watches the patient's face, detects their emotional state in real time, and automatically plays mood-appropriate music. The patient never needs to press a button or operate any interface — a caregiver starts the app once and it runs in the background.
A live demo runs entirely in the browser — face detection, emotion recognition, mood smoothing, and generative music, all client-side. No video ever leaves your device.
Test it locally right now:
python -m http.server 8765 --directory docs
then open http://localhost:8765/demo.html, click ▶ Start, and allow camera access. Smile, frown, or look surprised and watch the mood (and music) follow. "Demo mode" is on by default so mood changes happen within seconds; unchecking it uses the slower, care-setting timings.
This project is Case 001 on the Evidence Board portfolio, which lives in its own repository (jelendu.github.io).
Live demo (no install): https://jelendu.github.io/face-emotion-detection-alzheimers/demo.html — served by this repo's GitHub Pages (Settings → Pages → branch main, folder /docs).
webcam ──> face detection ──> emotion classification ──> mood smoothing ──> music playback
(YuNet, OpenCV) (FER+ ONNX model) (majority vote (pygame,
+ hold time) crossfade)
- Face detection — each analyzed frame goes through YuNet, a lightweight modern face detector. The largest face (the patient, closest to camera) is used.
- Emotion classification — the face crop is classified by FER+, an ONNX model trained on the FER-2013 dataset family (the same emotion vocabulary used by the
ferlibrary in the original research notebook). It outputs 8 emotions: neutral, happiness, surprise, sadness, anger, disgust, fear, contempt. - Mood smoothing — raw per-frame predictions flicker, so the mood is decided by a majority vote over a 20-second sliding window, and once the music changes it is held for at least 45 seconds. Music transitions use a 4-second crossfade so they are never jarring.
- Music playback — the detected mood selects a folder of music, and tracks from that folder play continuously in shuffled order.
The music mirrors the detected emotion:
| Detected emotion | Music category | Style |
|---|---|---|
| happiness, surprise | happy |
upbeat, bouncy |
| neutral, contempt | calm |
gentle ambient background |
| sadness | sad |
slow, melancholy piano |
| anger, disgust | rock |
driving rock, distorted guitar + drums |
| fear | horror |
eerie drones and unsettling tones |
The mapping, timings, and volume are all adjustable in config.py (desktop) and at the top of docs/demo.html (web demo) — e.g. if you prefer calming music for an agitated patient rather than rock, it's a two-line change.
Requires Python 3.10+ and a webcam.
pip install -r requirements.txt python setup_assets.py # downloads the two ONNX models (~35 MB) and # generates built-in ambient music for every mood
python app.py # headless — recommended for care settings python app.py --preview # shows a camera window with the detected emotion (for testing)
Stop with Ctrl+C (or q in the preview window).
Familiar, personally meaningful music is especially effective for people with Alzheimer's. Drop MP3/OGG/WAV files into the mood folders:
music/
happy/ ← upbeat favourites
calm/ ← easy background listening
sad/ ← slow, wistful songs
rock/ ← energetic rock tracks
horror/ ← eerie, suspenseful pieces
The synthesized ambient tracks created by setup_assets.py make the app fully functional out of the box, but the patient's own favourites from their youth will always work better.
| File | Purpose |
|---|---|
docs/demo.html |
Browser demo — face-api.js + Web Audio, fully client-side |
docs/index.html |
Redirect to the Evidence Board portfolio (moved to its own repo) |
app.py |
Main application — capture loop, zero-interaction operation |
emotion_detector.py |
YuNet face detection + FER+ emotion inference |
mood_mapper.py |
Sliding-window mood smoothing and hold logic |
music_player.py |
Crossfading, category-based music playback |
config.py |
All tunable settings (mapping, timings, volume) |
setup_assets.py |
One-time setup: model download + built-in music generation |
Face_emotion_Detection_FER.ipynb |
Original research notebook from the Samsung fellowship (FER library experiments and EfficientNet data-augmentation work) |
The Jupyter notebook contains the original exploration: data-augmentation transforms for an EfficientNet pipeline (excluding ColorJitter to avoid lighting bias) and emotion detection experiments with the fer library on static images. The desktop app uses the FER+ ONNX model instead of the fer pip package because it covers the same emotion set while avoiding the heavy TensorFlow dependency, making it fast and reliable on ordinary CPUs.
This project welcomes feedback, corrections, and collaborations to improve and expand its impact.