48 lines
1.4 KiB
Python
48 lines
1.4 KiB
Python
from statistics import mode
|
|
|
|
import cv2
|
|
from keras.models import load_model
|
|
import numpy as np
|
|
|
|
from utils.datasets import get_labels
|
|
from utils.inference import load_detection_model
|
|
from utils.preprocessor import preprocess_input
|
|
|
|
# parameters for loading data and images
|
|
detection_model_path = './data/data_opencv/res10_300x300_ssd_iter_140000.caffemodel'
|
|
emotion_model_path = 'data/trained_models/emotion_models/fer2013_mini_XCEPTION.102-0.66.hdf5'
|
|
emotion_labels = get_labels('fer2013')
|
|
|
|
# hyper-parameters for bounding boxes shape
|
|
frame_window = 10
|
|
emotion_offsets = (20, 40)
|
|
|
|
# loading models
|
|
face_detection = load_detection_model(detection_model_path)
|
|
emotion_classifier = load_model(emotion_model_path, compile=False)
|
|
|
|
# getting input model shapes for inference
|
|
emotion_target_size = emotion_classifier.input_shape[1:3]
|
|
|
|
# starting lists for calculating modes
|
|
emotion_window = []
|
|
|
|
|
|
def smile_detect(bgr_image):
|
|
gray_image = cv2.cvtColor(bgr_image, cv2.COLOR_BGR2GRAY)
|
|
x1 = y1 = 0
|
|
(x2, y2, channel) = bgr_image.shape
|
|
|
|
gray_face = gray_image[y1:y2, x1:x2]
|
|
|
|
gray_face = cv2.resize(gray_face, (emotion_target_size))
|
|
|
|
gray_face = preprocess_input(gray_face, True)
|
|
gray_face = np.expand_dims(gray_face, 0)
|
|
gray_face = np.expand_dims(gray_face, -1)
|
|
emotion_prediction = emotion_classifier.predict(gray_face)
|
|
emotion_label_arg = np.argmax(emotion_prediction)
|
|
emotion_text = emotion_labels[emotion_label_arg]
|
|
|
|
return emotion_text
|