Create README.md
Browse files
README.md
ADDED
@@ -0,0 +1,100 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
---
|
2 |
+
license: mit
|
3 |
+
library_name: sklearn
|
4 |
+
---
|
5 |
+
|
6 |
+
# svm_emo
|
7 |
+
|
8 |
+
## Model Description
|
9 |
+
svm_emo combines histogram of oriented gradient feature extraction with a linear support vector machine to predict emotional face expressions from single frame images.
|
10 |
+
|
11 |
+
## Model Details
|
12 |
+
- **Model Type**: Support Vector Machine (SVM)
|
13 |
+
- **Framework**: sklearn
|
14 |
+
|
15 |
+
## Model Sources
|
16 |
+
- **Repository**: [GitHub Repository](https://github.com/cosanlab/py-feat)
|
17 |
+
- **Paper**: [Py-feat: Python facial expression analysis toolbox](https://link.springer.com/article/10.1007/s42761-023-00191-4)
|
18 |
+
|
19 |
+
## Citation
|
20 |
+
If you use the svm_emo model in your research or application, please cite the following paper:
|
21 |
+
|
22 |
+
Cheong, J.H., Jolly, E., Xie, T. et al. Py-Feat: Python Facial Expression Analysis Toolbox. Affec Sci 4, 781–796 (2023). https://doi.org/10.1007/s42761-023-00191-4
|
23 |
+
|
24 |
+
```
|
25 |
+
@article{cheong2023py,
|
26 |
+
title={Py-feat: Python facial expression analysis toolbox},
|
27 |
+
author={Cheong, Jin Hyun and Jolly, Eshin and Xie, Tiankang and Byrne, Sophie and Kenney, Matthew and Chang, Luke J},
|
28 |
+
journal={Affective Science},
|
29 |
+
volume={4},
|
30 |
+
number={4},
|
31 |
+
pages={781--796},
|
32 |
+
year={2023},
|
33 |
+
publisher={Springer}
|
34 |
+
}
|
35 |
+
```
|
36 |
+
|
37 |
+
## Example Useage
|
38 |
+
|
39 |
+
```python
|
40 |
+
import numpy as np
|
41 |
+
from skops.io import dump, load, get_untrusted_types
|
42 |
+
from huggingface_hub import hf_hub_download
|
43 |
+
|
44 |
+
class EmoSVMClassifier:
|
45 |
+
def __init__(self, **kwargs) -> None:
|
46 |
+
self.weights_loaded = False
|
47 |
+
|
48 |
+
def load_weights(self, scaler_full=None, pca_model_full=None, classifiers=None):
|
49 |
+
|
50 |
+
self.scaler_full = scaler_full
|
51 |
+
self.pca_model_full = pca_model_full
|
52 |
+
self.classifiers = classifiers
|
53 |
+
self.weights_loaded = True
|
54 |
+
|
55 |
+
def pca_transform(self, frame, scaler, pca_model, landmarks):
|
56 |
+
if not self.weights_loaded:
|
57 |
+
raise ValueError('Need to load weights before running pca_transform')
|
58 |
+
else:
|
59 |
+
transformed_frame = pca_model.transform(scaler.transform(frame))
|
60 |
+
return np.concatenate((transformed_frame, landmarks), axis=1)
|
61 |
+
|
62 |
+
def detect_emo(self, frame, landmarks, **kwargs):
|
63 |
+
"""
|
64 |
+
Note that here frame is represented by hogs
|
65 |
+
"""
|
66 |
+
if not self.weights_loaded:
|
67 |
+
raise ValueError('Need to load weights before running detect_au')
|
68 |
+
else:
|
69 |
+
landmarks = np.concatenate(landmarks)
|
70 |
+
landmarks = landmarks.reshape(-1, landmarks.shape[1] * landmarks.shape[2])
|
71 |
+
|
72 |
+
pca_transformed_full = self.pca_transform(frame, self.scaler_full, self.pca_model_full, landmarks)
|
73 |
+
emo_columns = ["anger", "disgust", "fear", "happ", "sad", "sur", "neutral"]
|
74 |
+
|
75 |
+
pred_emo = []
|
76 |
+
for keys in emo_columns:
|
77 |
+
emo_pred = self.classifiers[keys].predict(pca_transformed_full)
|
78 |
+
pred_emo.append(emo_pred)
|
79 |
+
|
80 |
+
pred_emos = np.array(pred_emo).T
|
81 |
+
return pred_emos
|
82 |
+
|
83 |
+
# Load model and weights
|
84 |
+
emotion_model = EmoSVMClassifier()
|
85 |
+
model_path = hf_hub_download(repo_id="py-feat/svm_emo", filename="svm_emo_classifier.skops")
|
86 |
+
unknown_types = get_untrusted_types(file=model_path)
|
87 |
+
loaded_model = load(model_path, trusted=unknown_types)
|
88 |
+
emotion_model.load_weights(scaler_full=loaded_model.scaler_full,
|
89 |
+
pca_model_full=loaded_model.pca_model_full,
|
90 |
+
classifiers=loaded_model.classifiers)
|
91 |
+
|
92 |
+
# Test model
|
93 |
+
frame = "path/to/your/test_image.jpg" # Replace with your loaded image
|
94 |
+
landmarks = np.array([...]) # Replace with your landmarks data
|
95 |
+
pred = emotion_model.detect_emo(frame, landmarks)
|
96 |
+
print(pred)
|
97 |
+
|
98 |
+
```
|
99 |
+
|
100 |
+
|