File size: 2,974 Bytes
dce1dbf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
---
license: apache-2.0
library_name: keras
pipeline_tag: image-classification
tags:
  - emotion-detection
  - facial-expression-recognition
  - image-classification
  - keras
  - tensorflow
  - efficientnetv2
---

# Facial Emotion Recognition β€” Model

Detects the emotion expressed in a photo of a human face.

Facial emotion classification model in Keras 3 format (`best_model.keras`).
Given a cropped face image it outputs probabilities over 7 emotion classes.

- **Maintainer of this repository**: Mamd1
- **Repository**: `Mamd1/emotion-detection-model`
- **Demo Space**: `Mamd1/emotion-detection-app`
- **License**: Apache-2.0

## Attribution

These weights were **not trained by the maintainer of this repository**. They are
redistributed from the original Apache-2.0 licensed release by **Ali Ahmed**
(`ali-ahmed-ai-developer/emotion_detection_model` on the Hugging Face Hub).
Apache-2.0 permits redistribution and modification provided the license and
attribution are preserved β€” this notice satisfies that requirement. Do not
remove it.

## Classes

Index order matters β€” the output vector is in exactly this order:

| Index | Class    |
|-------|----------|
| 0     | angry    |
| 1     | disgust  |
| 2     | fear     |
| 3     | happy    |
| 4     | neutral  |
| 5     | sad      |
| 6     | surprise |

## Model details

- **Backbone**: EfficientNetV2-S (ImageNet pre-trained)
- **Head**: GlobalAveragePooling2D β†’ BatchNorm β†’ Dropout β†’ Dense(512) β†’ BatchNorm β†’ Dropout β†’ Dense(7)
- **Input shape**: `(380, 380, 3)`, float32, values in `[0, 255]` (EfficientNetV2 rescales internally)
- **Parameters**: ~21M
- **File**: `best_model.keras` (~343 MB, stored via Git LFS)

## Preprocessing

The reference application converts the image to grayscale, then back to RGB
(so R=G=B across 3 channels), resizes to 380x380, and passes raw `[0, 255]`
float32 values. Matching this preprocessing is required for accurate results.

## Usage

```python
import numpy as np
import tensorflow as tf
from PIL import Image
from huggingface_hub import hf_hub_download

CLASSES = ['angry', 'disgust', 'fear', 'happy', 'neutral', 'sad', 'surprise']

path = hf_hub_download(
    repo_id="Mamd1/emotion-detection-model",
    filename="best_model.keras",
)
model = tf.keras.models.load_model(path)

image = Image.open("face.jpg").convert("L").convert("RGB").resize((380, 380))
x = np.expand_dims(np.array(image, dtype=np.float32), axis=0)

probs = model.predict(x)[0]
print(dict(zip(CLASSES, probs.tolist())))
```

## Limitations

- Trained on tightly cropped faces. Wide shots, small faces, or busy backgrounds
  degrade accuracy significantly β€” crop to the face first.
- Facial expression recognition is not a reliable measure of a person's actual
  internal emotional state. Do not use for consequential decisions about people
  (hiring, security screening, medical or psychological assessment).
- Accuracy varies across demographic groups and lighting conditions.