FER / app.py
KikoDM's picture
Update app.py
a80d79c
raw
history blame
2.35 kB
import gradio as gr
import pandas as pd
import numpy as np
import os
#import cv2
from tqdm import tqdm
import tensorflow as tf
from tensorflow import keras
from keras.utils import np_utils
#from tensorflow.python.keras.preprocessing import image
#from tensorflow.python.keras.preprocessing.image import ImageDataGenerator
from keras.preprocessing import image
from keras.preprocessing.image import ImageDataGenerator
#from skimage import io
import matplotlib.pyplot as plt
#from tensorflow.python.keras.utils import np_utils
import pickle
#with Path("modelo_entrenado.pkl").open("br")as f:
# new_model=pickle.load(f)
#new_model = pickle.load(open("modelo_entrenado.pkl", 'rb'))
new_model = tf.keras.models.load_model('modelo_entrenado.h5')
objects = ('angry', 'disgust', 'fear', 'happy', 'sad', 'surprise', 'neutral')
y_pos = np.arange(len(objects))
print(y_pos)
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')
plt.show()
def predict_image(pic):
#img = image.load_img(pic, grayscale=True, target_size=(48, 48))
#show_img=image.load_img(pic, grayscale=False, target_size=(200, 200))
#pic = pic.reshape(-1,48, 48,1])
x = image.img_to_array(pic)
#x = np.expand_dims(x, axis = 0)
#x /= 255
custom = new_model.predict(x.reshape(0,48,48,1))
emotion_analysis(custom[0])
#x = np.array(x, 'float32')
#x = x.reshape([48, 48]);
#plt.gray()
#plt.imshow(show_img)
#plt.show()
m=0.000000000000000000001
a=custom[0]
for i in range(0,len(a)):
if a[i]>m:
m=a[i]
ind=i
return ('Expression Prediction:',objects[ind])
iface = gr.Interface(
predict_image,
[
#gr.inputs.Image(shape=None, image_mode="RGB", invert_colors=False, source="upload", tool="editor", type="numpy", label=None, optional=False)
gr.inputs.Image(source="upload",shape=(48,48))
],
"text",
interpretation="default",
title = 'FER',
description = 'El ',
theme = 'grass'
)
iface.launch()