File size: 3,622 Bytes
57f77d3
 
970e53f
 
57f77d3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
95
96
97
98
99
100
101
102
---
license: mit
library_name: py-feat
pipeline_tag: image-feature-extraction
---

# svm_emo

## Model Description
svm_emo combines histogram of oriented gradient feature extraction with a linear support vector machine to predict emotional face expressions from single frame images. 

## Model Details
- **Model Type**: Support Vector Machine (SVM)
- **Framework**: sklearn

## Model Sources
- **Repository**: [GitHub Repository](https://github.com/cosanlab/py-feat)
- **Paper**: [Py-feat: Python facial expression analysis toolbox](https://link.springer.com/article/10.1007/s42761-023-00191-4)

## Citation
If you use the svm_emo model in your research or application, please cite the following paper:

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

```
@article{cheong2023py,
  title={Py-feat: Python facial expression analysis toolbox},
  author={Cheong, Jin Hyun and Jolly, Eshin and Xie, Tiankang and Byrne, Sophie and Kenney, Matthew and Chang, Luke J},
  journal={Affective Science},
  volume={4},
  number={4},
  pages={781--796},
  year={2023},
  publisher={Springer}
}
```

## Example Useage

```python
import numpy as np
from skops.io import dump, load, get_untrusted_types
from huggingface_hub import hf_hub_download

class EmoSVMClassifier:
    def __init__(self, **kwargs) -> None:
        self.weights_loaded = False
        
    def load_weights(self, scaler_full=None, pca_model_full=None, classifiers=None):

        self.scaler_full = scaler_full
        self.pca_model_full = pca_model_full
        self.classifiers = classifiers
        self.weights_loaded = True

    def pca_transform(self, frame, scaler, pca_model, landmarks):
        if not self.weights_loaded:
            raise ValueError('Need to load weights before running pca_transform')
        else:
            transformed_frame = pca_model.transform(scaler.transform(frame))
            return np.concatenate((transformed_frame, landmarks), axis=1)      

    def detect_emo(self, frame, landmarks, **kwargs):
        """
        Note that here frame is represented by hogs
        """
        if not self.weights_loaded:
            raise ValueError('Need to load weights before running detect_au')
        else:
            landmarks = np.concatenate(landmarks)
            landmarks = landmarks.reshape(-1, landmarks.shape[1] * landmarks.shape[2])

            pca_transformed_full = self.pca_transform(frame, self.scaler_full, self.pca_model_full, landmarks)
            emo_columns = ["anger", "disgust", "fear", "happ", "sad", "sur", "neutral"]
    
            pred_emo = []
            for keys in emo_columns:
                emo_pred = self.classifiers[keys].predict(pca_transformed_full)
                pred_emo.append(emo_pred)
    
            pred_emos = np.array(pred_emo).T
            return pred_emos            

# Load model and weights
emotion_model = EmoSVMClassifier()
model_path = hf_hub_download(repo_id="py-feat/svm_emo", filename="svm_emo_classifier.skops")
unknown_types = get_untrusted_types(file=model_path)
loaded_model = load(model_path, trusted=unknown_types)
emotion_model.load_weights(scaler_full=loaded_model.scaler_full, 
                           pca_model_full=loaded_model.pca_model_full, 
                           classifiers=loaded_model.classifiers)

# Test model
frame = "path/to/your/test_image.jpg"  # Replace with your loaded image
landmarks = np.array([...])  # Replace with your landmarks data
pred = emotion_model.detect_emo(frame, landmarks)
print(pred)

```