# from skimage import io
import cv2
import imutils
import numpy as np
import tensorflow as tf
from tensorflow import keras
from keras.preprocessing import image
from keras.models import Sequential, load_model
from keras.preprocessing.image import load_img
from keras.preprocessing.image import img_to_array
import matplotlib.pyplot as plt

Savedmodel = tf.keras.models.load_model('./new model8.h5')
Savedmodel.summary()

objects = ('Angry', 'Disgust', 'Fear', 'Happy', 'Sad', 'Surprise', 'Neutral')
vid = cv2.VideoCapture(0)

def run():
    while True:

        _, frame = vid.read()
        frame = imutils.resize(frame, width=500)

        # result = api(frame)

        cv2.imshow("frame",frame)
        # getPrediction(frame)

        # cv.waitKey(0)
        if cv2.waitKey(20) & 0XFF == ord('q'):
            break

    vid.release()
    cv2.destroyAllWindows()

def getPrediction(img):

    x = image.img_to_array(img)
    x = np.expand_dims(x, axis=0)

    x /= 255

    custom = Savedmodel.predict(x)
    # print(custom[0])
    emotion_analysis(custom[0])

    x = np.array(x, 'float32')
    x = x.reshape([48, 48]);

    # plt.gray()
    # plt.show()

    m = 0.000000000000000000001
    a = custom[0]
    for i in range(0, len(a)):
        if a[i] > m:
            m = a[i]
            ind = i

    print('Expression Prediction:', objects[ind])


def emotion_analysis(emotions):
    objects = ['Angry', 'Disgust', 'Fear', 'Happy', 'Sad', 'Surprise', 'Neutral']
    y_pos = np.arange(len(objects))
    plt.bar(y_pos, emotions, align='center', alpha=0.9)
    plt.tick_params(axis='x', which='both', pad=10, width=4, length=10)
    plt.xticks(y_pos, objects)
    plt.ylabel('percentage')
    plt.title('emotion')

run()