<reponame>bsatrom/emotion-mesh
import tensorflow as tf
from tensorflow.keras.preprocessing.image import img_to_array
from tensorflow.keras.models import load_model
import imutils
import cv2
import numpy as np
import sys
import time

start = time.time()
# parameters for loading data and images
detection_model_path = 'haarcascade_files/haarcascade_frontalface_default.xml'
emotion_model_path = 'models/_mini_XCEPTION.134-0.66.hdf5'
img_path = 'sample/horns.png'

# hyper-parameters for bounding boxes shape
# loading models
face_detection = cv2.CascadeClassifier(detection_model_path)
emotion_classifier = load_model(emotion_model_path, compile=False)
EMOTIONS = ["angry", "disgust", "scared",
            "happy", "sad", "surprised", "neutral"]

# reading the frame
orig_frame = cv2.imread(img_path)
frame = cv2.imread(img_path, 0)
faces = face_detection.detectMultiScale(
    frame, scaleFactor=1.1, minNeighbors=5, minSize=(30, 30), flags=cv2.CASCADE_SCALE_IMAGE)

if len(faces) > 0:
    faces = sorted(faces, reverse=True, key=lambda x: (
        x[2] - x[0]) * (x[3] - x[1]))[0]
    (fX, fY, fW, fH) = faces
    roi = frame[fY:fY + fH, fX:fX + fW]
    roi = cv2.resize(roi, (48, 48))
    roi = roi.astype("float") / 255.0
    roi = img_to_array(roi)
    roi = np.expand_dims(roi, axis=0)
    preds = emotion_classifier.predict(roi)[0]

    print(EMOTIONS)
    print(preds)

    emotion_probability = np.max(preds)
    label = EMOTIONS[preds.argmax()]
    cv2.putText(orig_frame, label, (fX, fY - 20),
                cv2.FONT_HERSHEY_TRIPLEX, 2, (255, 255, 255), 2)
    cv2.rectangle(orig_frame, (fX, fY), (fX + fW, fY + fH), (0, 0, 255), 2)

# Show the image on the screen
# cv2.imshow('test_face', orig_frame)

cv2.imwrite(img_path.split('.')[0]+'_prediction.png', orig_frame)

# if (cv2.waitKey(5000) == ord('q')):
#    sys.exit("Thanks")
# cv2.destroyAllWindows()

end = time.time()

print("Execution Time: " + str(end-start))

import tensorflow as tf
from tensorflow.keras.preprocessing.image import img_to_array
from tensorflow.keras.models import load_model
import imutils
import cv2
import numpy as np
import sys
import time

start = time.time()
# parameters for loading data and images
detection_model_path = 'haarcascade_files/haarcascade_frontalface_default.xml'
emotion_model_path = 'models/_mini_XCEPTION.134-0.66.hdf5'
img_path = 'sample/horns.png'

# hyper-parameters for bounding boxes shape
# loading models
face_detection = cv2.CascadeClassifier(detection_model_path)
emotion_classifier = load_model(emotion_model_path, compile=False)
EMOTIONS = ["angry", "disgust", "scared",
            "happy", "sad", "surprised", "neutral"]

# reading the frame
orig_frame = cv2.imread(img_path)
frame = cv2.imread(img_path, 0)
faces = face_detection.detectMultiScale(
    frame, scaleFactor=1.1, minNeighbors=5, minSize=(30, 30), flags=cv2.CASCADE_SCALE_IMAGE)

if len(faces) > 0:
    faces = sorted(faces, reverse=True, key=lambda x: (
        x[2] - x[0]) * (x[3] - x[1]))[0]
    (fX, fY, fW, fH) = faces
    roi = frame[fY:fY + fH, fX:fX + fW]
    roi = cv2.resize(roi, (48, 48))
    roi = roi.astype("float") / 255.0
    roi = img_to_array(roi)
    roi = np.expand_dims(roi, axis=0)
    preds = emotion_classifier.predict(roi)[0]

    print(EMOTIONS)
    print(preds)

    emotion_probability = np.max(preds)
    label = EMOTIONS[preds.argmax()]
    cv2.putText(orig_frame, label, (fX, fY - 20),
                cv2.FONT_HERSHEY_TRIPLEX, 2, (255, 255, 255), 2)
    cv2.rectangle(orig_frame, (fX, fY), (fX + fW, fY + fH), (0, 0, 255), 2)

# Show the image on the screen
# cv2.imshow('test_face', orig_frame)

cv2.imwrite(img_path.split('.')[0]+'_prediction.png', orig_frame)

# if (cv2.waitKey(5000) == ord('q')):
#    sys.exit("Thanks")
# cv2.destroyAllWindows()

end = time.time()

print("Execution Time: " + str(end-start))
# parameters for loading data and images # hyper-parameters for bounding boxes shape # loading models # reading the frame # Show the image on the screen # cv2.imshow('test_face', orig_frame) # if (cv2.waitKey(5000) == ord('q')): #    sys.exit("Thanks") # cv2.destroyAllWindows()