-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: replaced our model with built-in pyfeat classifier. (#70)
- Loading branch information
Showing
3 changed files
with
53 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,43 +1,43 @@ | ||
import logging | ||
import pickle | ||
|
||
from pandas import DataFrame | ||
from pkg_resources import resource_filename | ||
from sklearn.svm import SVC # type: ignore[import-untyped] | ||
from feat import Detector # type: ignore | ||
from feat.utils import FEAT_EMOTION_COLUMNS # type: ignore | ||
import numpy as np | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
SVM_MODEL_PATH = resource_filename(__name__, "models/svm_model.pkl") | ||
SCALER_MODEL_PATH = resource_filename(__name__, "models/scaler.pkl") | ||
LABEL_ENCODER_MODEL_PATH = resource_filename(__name__, "models/label_encoder.pkl") | ||
PYFEAT_EMOTIONS_TO_EMOTIONS = { | ||
"neutral": "neutral", | ||
"anger": "angry", | ||
"happy": "happy", | ||
"sadness": "sad", | ||
} | ||
|
||
|
||
class EmotionDetector: | ||
"""The EmotionDetector is responsible for predict the emotion of the user.""" | ||
|
||
_model: SVC # or whatever model we use | ||
_detector: Detector # use built-in pyfeat classifier | ||
|
||
def __init__(self): | ||
# load model | ||
logger.info("Loading emotion detection model...") | ||
# Load the model from the file | ||
with open(SVM_MODEL_PATH, "rb") as file: | ||
self.loaded_model = pickle.load(file) | ||
def __init__(self, detector: Detector): | ||
self._detector = detector | ||
|
||
with open(SCALER_MODEL_PATH, "rb") as file: | ||
self.loaded_scaler = pickle.load(file) | ||
|
||
with open(LABEL_ENCODER_MODEL_PATH, "rb") as file: | ||
self.loaded_label_encoder = pickle.load(file) | ||
|
||
def detect_emotion(self, features: DataFrame) -> str: | ||
def detect_emotion( | ||
self, | ||
frame: np.ndarray, | ||
faces: list[tuple[float, float, float, float, float]], | ||
features: list, | ||
) -> str: | ||
"""Predicts the emotion in the given features and returns it as a string.""" | ||
|
||
if len(features) == 0: | ||
if len(faces) == 0 or len(features) == 0: | ||
return "neutral" | ||
|
||
scaled_aus = self.loaded_scaler.transform(features[0]) | ||
predictions = self.loaded_model.predict(scaled_aus) | ||
predicted_emotions = self.loaded_label_encoder.inverse_transform(predictions) | ||
detected_emotions = self._detector.detect_emotions(frame, [faces], features)[0] | ||
|
||
detected_emotion = FEAT_EMOTION_COLUMNS[np.argmax(detected_emotions[0])] | ||
if detected_emotion not in PYFEAT_EMOTIONS_TO_EMOTIONS.keys(): | ||
detected_emotion = "neutral" | ||
predicted_emotion = PYFEAT_EMOTIONS_TO_EMOTIONS[detected_emotion] | ||
|
||
return predicted_emotions[0] | ||
return predicted_emotion |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters