Views
No views yet
@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}
}1import numpy as np
2from skops.io import dump, load, get_untrusted_types
3from huggingface_hub import hf_hub_download
4
5class EmoSVMClassifier:
6 def __init__(self, **kwargs) -> None:
7 self.weights_loaded = False
8
9 def load_weights(self, scaler_full=None, pca_model_full=None, classifiers=None):
10
11 self.scaler_full = scaler_full
12 self.pca_model_full = pca_model_full
13 self.classifiers = classifiers
14 self.weights_loaded = True
15
16 def pca_transform(self, frame, scaler, pca_model, landmarks):
17 if not self.weights_loaded:
18 raise ValueError('Need to load weights before running pca_transform')
19 else:
20 transformed_frame = pca_model.transform(scaler.transform(frame))
21 return np.concatenate((transformed_frame, landmarks), axis=1)
22
23 def detect_emo(self, frame, landmarks, **kwargs):
24 """
25 Note that here frame is represented by hogs
26 """
27 if not self.weights_loaded:
28 raise ValueError('Need to load weights before running detect_au')
29 else:
30 landmarks = np.concatenate(landmarks)
31 landmarks = landmarks.reshape(-1, landmarks.shape[1] * landmarks.shape[2])
32
33 pca_transformed_full = self.pca_transform(frame, self.scaler_full, self.pca_model_full, landmarks)
34 emo_columns = ["anger", "disgust", "fear", "happ", "sad", "sur", "neutral"]
35
36 pred_emo = []
37 for keys in emo_columns:
38 emo_pred = self.classifiers[keys].predict(pca_transformed_full)
39 pred_emo.append(emo_pred)
40
41 pred_emos = np.array(pred_emo).T
42 return pred_emos
43
44# Load model and weights
45emotion_model = EmoSVMClassifier()
46model_path = hf_hub_download(repo_id="py-feat/svm_emo", filename="svm_emo_classifier.skops")
47unknown_types = get_untrusted_types(file=model_path)
48loaded_model = load(model_path, trusted=unknown_types)
49emotion_model.load_weights(scaler_full=loaded_model.scaler_full,
50 pca_model_full=loaded_model.pca_model_full,
51 classifiers=loaded_model.classifiers)
52
53# Test model
54frame = "path/to/your/test_image.jpg" # Replace with your loaded image
55landmarks = np.array([...]) # Replace with your landmarks data
56pred = emotion_model.detect_emo(frame, landmarks)
57print(pred)
58