import cv2 import numpy as np import gradio as gr import tensorflow as tf IMG_SIZE = 48 CLASSES = ['angry', 'disgust', 'fear', 'happy', 'neutral', 'sad', 'surprise'] model = tf.keras.models.load_model('CNN4G7.h5') def extract_face(image): faceCascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml') faces = faceCascade.detectMultiScale(image, minNeighbors=3, minSize=(30, 30)) if len(faces) == 0: return cv2.resize(image, (IMG_SIZE, IMG_SIZE), interpolation = cv2.INTER_CUBIC) x, y, w, h = faces[0] face = image[y:y+h,x:x+w] return cv2.resize(face, (IMG_SIZE, IMG_SIZE), interpolation = cv2.INTER_CUBIC) def preprocess_image(image): return image.reshape(-1, IMG_SIZE, IMG_SIZE, 1) def predict_face_expression(image): if image is None: return "Please select an image..." face = extract_face(image) image = preprocess_image(face) prediction = model.predict(image) return { CLASSES[i]: float(prediction[0][i]) for i in range(len(CLASSES)) } with gr.Blocks() as app: gr.Markdown('## Sistema de identificación de emociones basado en procesamiento de imágenes usando técnicas de Machine Learning') with gr.Tab('Image'): with gr.Row(): image_input = gr.Image(image_mode='L') image_button = gr.Button('Submit') image_button.click(predict_face_expression, inputs=image_input, outputs=gr.outputs.Label(num_top_classes=3)) app.launch()