Facial Emotion Recognition: Building an AI That Smiles Back
The Artificial Emotion Engine reads faces, gestures and voice tone through a webcam and answers through a 3D avatar. Here is how its facial emotion recognition pipeline works, and how accurate it really is.

Facial emotion recognition is the core of the Artificial Emotion Engine, a deep-learning project built by ByteMuni author Deepak Kumar. It reads a person's face, hands, posture and tone of voice through a webcam and microphone, then answers through a 3D avatar named Arjun. On 209 real faces, it read every happy face correctly and 64.9% of all seven expressions.
This article covers what the engine does, how its pipeline turns video into expressions, which neural networks it uses, how accurate it is on real faces, what broke during the build, and where facial emotion recognition should and should not be used. All accuracy numbers come from the project's own evaluation scripts, run by ByteMuni on 28 November 2026.
What is the Artificial Emotion Engine?
The Artificial Emotion Engine (AEE) is an experimental AI companion that runs in a web browser with a small Python service on the same computer. It watches for visible signals, such as a smile, a raised brow, a wave or a nod, and reacts the way a person might. Smile and Arjun smiles back. Look sad and he asks if you want to talk.
One design rule shapes everything: the engine reads visible signals, not feelings. A smile is a muscle movement, and no camera can see what someone actually feels. The project's documentation says every label is phrased as "appears" or "looks" and carries a confidence score. Arjun's own emotions are a simulation built to communicate clearly.
| The user does this | Arjun responds |
|---|---|
| Smiles or laughs | Smiles back or laughs along, then asks "What's so funny?" |
| Looks surprised | Raises his brows: "Whoa! What happened?" |
| Frowns or scolds him | Stays calm, looks a little hurt, asks if something is wrong |
| Looks sad for a few seconds | "Do you want to talk about it?" |
| Waves, does namaste, offers a handshake | Waves back, folds his hands, reaches out to shake |
| Nods or shakes the head | Nods along, or tilts his head to understand |
| Raises their voice, then pauses | "You sound upset. I'm listening." (never mid-sentence) |
| Says "dance karo" or "salute" | Performs a bhangra or a salute |
Users can talk to Arjun in Hindi, English or Hinglish, and he answers in the same language. He also notices actions beyond the face: eating, a phone call, a yawn, or turning away to talk to someone else. In that last case he waits quietly instead of answering words that were not meant for him.
How does facial emotion recognition work in the engine?
The system has three layers. The browser sees, a local Python engine reads and "feels", and the 3D avatar answers. The most important choice was to run computer vision inside the browser. Two Web Workers, background threads in the browser, run Google's MediaPipe models on the laptop's GPU through WebGL, with a CPU fallback.

Raw video never leaves the page. Only small numeric packets, about 1.5 KB each, travel over a WebSocket to an engine bound to 127.0.0.1, the computer's own address. Compared with streaming frames to a Python OpenCV process, this cuts latency and bandwidth and keeps the video private. The engine itself uses FastAPI, a Python web framework, with three layers whose dependencies point one way: perception, then emotion, then expression.
Reading a face takes seven steps:
Landmarks. MediaPipe Face Landmarker finds 478 points on the face, 52 "blendshapes" (scores for movements such as a smile, a brow raise or a blink) and the head angle. A One Euro filter smooths them, removing jitter without adding lag.
Face snapshot. About six times a second, the browser rotates the face upright using the eye corners, crops it and sends a 224×224 image to the local engine. It is analyzed in memory and never stored.
CNN. EmotionNet, a convolutional neural network (CNN), classifies the snapshot and its mirror image and averages the two. This trick is called test-time augmentation.
FACS rules. Rules based on the Facial Action Coding System, a standard catalog of facial muscle movements, score each expression. For example, a laugh is a cheek raise plus a lip-corner pull plus parted lips plus a jaw drop.
Fusion. The engine blends 65% CNN with 35% rules, weighted by image quality and how fresh the CNN reading is. The rules catch subtle sadness the CNN misses, and the CNN vetoes rule mistakes such as crying read as laughing.
Personalization. The engine learns how the user's relaxed face looks and measures every expression against it, not against an average face.
Smoothing. A label changes only when a new expression leads by a clear margin for a moment, so the reading does not flicker.
Personalization matters more than it sounds. Some people rest with low brows and look "angry" to a model trained on others. In one test, a user's relaxed face read 0.30 to 0.45 "sad" to the CNN, while their genuinely sad face read 0.60 to 0.78. Subtracting the resting level left too little signal, so the engine now also counts the rise in odds over that person's resting reading. On that user's sessions, clear sad moments were caught 89% of the time instead of 85%, according to the project's documentation.
Which deep-learning models power the Artificial Emotion Engine?
Five neural networks handle perception and speech. Most of the remaining logic is plain rules on purpose, because rules are explainable and instant. Readers new to these terms can start with ByteMuni's list of AI terms every student should know.
| Model | Network type | Job | Runs on |
|---|---|---|---|
| MediaPipe Face Landmarker | CNNs plus a small MLP | 478 face points, 52 blendshapes, head pose | Browser GPU (3.8 MB) |
EmotionNet: HSEmotion enet_b0_8_va_mtl | CNN (EfficientNet-B0) | 8 expression probabilities plus valence and arousal | Local CPU via ONNX Runtime (16 MB) |
| MediaPipe Gesture Recognizer | CNNs plus MLP | 21 points per hand and signs such as thumbs up | Browser GPU |
| MediaPipe Pose Landmarker (lite) | CNNs | 33 body points: shoulders, elbows, wrists | Browser GPU |
Whisper base.en (faster-whisper) | Transformer | Offline speech to text, English | Local CPU (about 145 MB) |
EmotionNet comes from the HSEmotion project, released under the Apache-2.0 license. Its EfficientNet-B0 model was trained on AffectNet, a dataset of real-world face photos labeled with expressions. ONNX Runtime, a fast inference engine for exported models, runs it on the CPU, so no special GPU is needed.
Conversation uses a language model. With an OpenAI key, the engine uses gpt-4.1-mini for replies, or the OpenAI Realtime API for a live voice call over WebRTC that answers in under a second. The classic chain of speech-to-text, reply and text-to-speech takes 2 to 4 seconds. Without a key, a local model through Ollama or built-in offline replies take over.
Why a small CNN beat bigger ones
ByteMuni ran the project's comparison script on 28 November 2026. It tests each CNN alone on the same 209 face crops, on a laptop CPU, including the mirror image:
| CNN | Mean accuracy | Time per face |
|---|---|---|
EfficientNet-B0 va_mtl (chosen) | 69.3% | 34 ms |
| EfficientNet-B2, 7 classes | 69.3% | 46 ms |
| EfficientNet-B0, fine-tuned on video (VGAF) | 68.9% | 24 ms |
| EfficientNet-B0, fine-tuned on video (AFEW) | 68.5% | 30 ms |
| EfficientNet-B2, 8 classes | 66.0% | 52 ms |
| MobileViT | 65.5% | 97 ms |
| MobileFaceNet | 43.6% | 9 ms |
The B2 model tied the B0 but ran slower. The project's notes add that an ensemble of three models gained at most two points for roughly four times the CPU, and none of them improved sadness. Bigger was slower here, not better.
How accurate is facial emotion recognition on real faces?
The full pipeline was tested on 209 faces from AffectNet-HQ, about 30 per expression. Each photo went through the app's own path: MediaPipe in a browser, then the engine's rules, CNN and fusion. ByteMuni re-ran the evaluation on 28 November 2026.

| Expression | Correct (full pipeline) | Correct (CNN alone) |
|---|---|---|
| Happy | 100% | 93% |
| Surprise | 73% | 73% |
| Neutral | 70% | 40% |
| Sad | 67% | 73% |
| Disgust | 67% | 80% |
| Anger | 47% | 73% |
| Fear | 31% | 52% |
| Mean | 64.9% | 69.3% |
The pipeline scores lower than the CNN alone on photos, and that gap is deliberate. In live video calls, people lower their brows to concentrate or while talking. An early version labeled users "frowning" 57% of the time in a live call, according to the project's notes. A frown now needs the CNN's support, which cut false "frowning" on relaxed faces from 21 cases to 9 and raised correct "neutral" readings from 40% (CNN alone) to 70%.
The cost is visible in the anger and fear rows. The pipeline is tuned for a calm, friendly conversation on a live webcam, not for scoring well on still photos. Students building similar projects can apply the same discipline: keep the test faces separate from anything used for tuning, as ByteMuni explains in its guide to training a neural network without data leakage.
One caution on these numbers: each photo shows a different person, so an average resting face stands in for the personal baseline. On a live webcam, the engine learns the user's own face, which the photo test cannot measure.
What broke while building the engine, and how it was fixed
The project's architecture notes record several real failures. Each one is a lesson for anyone building facial emotion recognition on webcam video:
Laughs read as smiles. MediaPipe's
jawOpenscore stayed between 0.0 and 0.25 even in big open-mouthed laughs; the opening showed up asmouthLowerDown. Using both, 8 of 30 happy faces read as laughing instead of 2."Surprised" after every smile. The resting-face baseline learned whenever a value dipped below it. A smile narrows the eyes and dips the brows, so the relaxed face afterwards looked "surprised" for about a minute. The baseline now learns only while the face is calm, after a 4-second warm-up.
A second CNN made results worse. Blending in EfficientNet-B2 at 55% flattened the probabilities, because its outputs sit on a much smaller scale. Happiness on smiling faces fell from 0.79 to 0.53. It is now optional, temperature-calibrated and off by default.
Laptop cameras sit above the screen. People also tip their heads back to laugh, so head-angle limits had to widen or laughs were discarded as a poor view.
Battery saver slowed everything two to three times. Face tracking fell to 3 to 8 frames per second instead of about 15. The app now detects battery saver and shows a note.
The project has 231 automated tests covering perception, emotions, gestures, voice tone and conversation. All passed when ByteMuni ran them on 28 November 2026.
How the emotion engine and 3D avatar respond
Arjun's feelings are not a neural network. They come from a small dynamical model of seven emotions (happiness, sadness, curiosity, surprise, excitement, calmness and empathy) plus a slow mood. The user's expression, tone and gestures push targets, and each emotion rises and falls at its own speed.
He reacts at two speeds, as people do. A reflex layer echoes the user's expression almost at once: the project measured his face starting to react to a smile in 26 ms and to a frown in 51 ms on the running engine. The emotion label changes more slowly, about 0.4 seconds to "happy" after a smile and 0.75 seconds to "caring" after a frown, so his mood does not flip on every flicker.
Two safety rules live inside the engine. First, a frown is never mirrored back as anger; it is met with concern. Second, the language model cannot set Arjun's emotions. It only classifies what the user's words mean, and the engine maps that through its own table with a cap on every step. A test feeds in a hostile model output that tries to inject emotions, and the cap holds. This split, where a model suggests and the system decides, is a common pattern in AI system design.
The avatar is a rigged character from Microsoft's Rocketbox library (MIT license), rendered with three.js, a JavaScript 3D library, at about 60 frames per second. His face uses the 52 ARKit blendshapes, so the engine's face settings map straight onto it. Hand gestures are described as goals (where the wrist should be, which way the elbow and palm point) and solved with two-bone inverse kinematics, the same method games use to place arms. His lips follow the real audio: loudness opens the jaw, and the balance of low, middle and high frequencies picks the mouth shape.
Privacy, uses and limits of facial emotion recognition
| Data | Where it goes |
|---|---|
| Camera video | Never leaves the browser |
| Face numbers and a small face crop | Local engine only; analyzed in memory, never stored |
| Voice tone (pitch, loudness, rhythm) | Numbers only, to the local engine |
| Spoken or typed words | Local in offline mode; sent to OpenAI only when an API key is set |
| Chat memory | Off by default; stored locally if turned on, and "Forget" deletes it |
The local engine rejects connections from other websites, and the camera and microphone stay off until the user clicks. Phone access goes through a local HTTPS proxy that needs a private access key.
Possible uses, none of which the project has shipped as a product, include:
AI assistants that slow down or rephrase when a user looks confused.
Practice partners for presentations or interview answers that react visibly to the speaker.
Game characters that respond to a player's face and voice.
A teaching example of combining CNNs, rules, signal processing and 3D graphics in one real-time system, useful for students preparing for AI interview questions.
Displayed expressions vary with culture, neurodiversity, lighting and camera angle, so this technology should never be used to make hiring, grading, credit or policing decisions. The European Union's AI Act (Regulation (EU) 2024/1689) prohibits emotion-recognition systems in workplaces and schools, with narrow medical and safety exceptions. This applies in the EU and is general information, not legal advice. Facial emotion recognition also cannot detect lies, intent or mental health conditions.
My take on the Artificial Emotion Engine
My read is that the deep-learning models were the easy part. A downloaded CNN and MediaPipe in the browser reached about 69% on photos on day one. The hard work was everything around the models: learning each person's resting face, not mistaking concentration for anger, and choosing concern over mirroring when someone frowns. I would tell any team building emotion features to write down early that the system reads signals, not feelings. That one sentence decides what the product is allowed to claim.
The bottom line on facial emotion recognition
A webcam companion that reacts kindly and quickly is buildable today on a normal laptop with open models: MediaPipe for landmarks, a small EfficientNet CNN for expressions, explainable rules for fusion and a simple dynamical model for simulated feelings. Expect near-perfect smiles and weak fear detection, measure on real faces, personalize to each user, keep video local, and never use the readings to judge anyone.
Frequently asked questions
Can facial emotion recognition tell how someone really feels?
No. It estimates visible expressions such as a smile or a frown, not inner feelings. The Artificial Emotion Engine labels every reading as "appears" or "looks" with a confidence score.
Does the Artificial Emotion Engine need a GPU?
No special GPU is needed. MediaPipe runs on the laptop's built-in graphics through WebGL, with a CPU fallback, and the EmotionNet CNN runs on the CPU with ONNX Runtime.
Which CNN does the engine use for expressions?
It uses HSEmotion's EfficientNet-B0 model enet_b0_8_va_mtl, trained on AffectNet. It outputs eight expression probabilities plus valence and arousal from a 224×224 face crop.
Why is the full pipeline less accurate than the CNN alone on photos?
The pipeline damps frowns because people lower their brows to concentrate or talk on live calls. That cut false "frowning" on relaxed faces but lowered anger and fear scores on still photos.
Is the webcam video sent to OpenAI or any server?
No. Video stays in the browser. Only words are sent to OpenAI, and only when the user adds an API key; offline mode keeps everything on the computer.
Is emotion recognition legal to use at work or in schools?
In the European Union, the AI Act (Regulation (EU) 2024/1689) prohibits emotion-recognition systems in workplaces and schools, with narrow medical and safety exceptions. This is general information, not legal advice.
Concepts in this article
- Artificial intelligence
- Computer systems that perform tasks that normally need human intelligence.
- Classes
- Syntax for constructor functions and prototype inheritance, with fields and private members.
- Data leakage
- Information from the test set or the future reaching training, which makes results look better than they are.
- Deep learning
- Machine learning with many-layered neural networks.
- Neural networks
- Layers of weighted connections trained by backpropagation to learn patterns.
- Numbers
- Floating-point numbers, BigInt, NaN and precision pitfalls.
- Python
- A general-purpose language widely used for scripting, web back ends and machine learning.
- React
- A JavaScript library for building user interfaces from components.



