import gradio as gr import torch from tensorflow.keras.utils import img_to_array import imutils import cv2 from keras.models import load_model import numpy as np from gradio.components import Image from gradio.components import Label # parameters for loading data and images detection_model_path = 'haarcascade_frontalface_default.xml' emotion_model_path = '_mini_XCEPTION.102-0.66.hdf5' #emotion_model_path = 'emotion_detection_model_state.pth' # hyper-parameters for bounding boxes shape # loading models face_detection = cv2.CascadeClassifier(detection_model_path) #emotion_classifier = torch.load(emotion_model_path,map_location=torch.device('cpu')) emotion_classifier = load_model(emotion_model_path, compile=False) EMOTIONS = ["angry", "disgusted", "scared", "happy", "sad", "surprised", "neutral"] def predict(frame): frame = imutils.resize(frame, width=300) gray = cv2.cvtColor(frame, cv2.COLOR_RGB2GRAY) faces = face_detection.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=5, minSize=(30, 30), flags=cv2.CASCADE_SCALE_IMAGE) frameClone = frame.copy() 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 # Extract the ROI of the face from the grayscale image, resize it to a fixed 28x28 pixels, and then prepare # the ROI for classification via the CNN roi = gray[fY:fY + fH, fX:fX + fW] roi = cv2.resize(roi, (64, 64)) roi = roi.astype("float") / 255.0 roi = img_to_array(roi) roi = np.expand_dims(roi, axis=0) preds = emotion_classifier.predict(roi)[0] label = EMOTIONS[preds.argmax()] else: return frameClone, "No se ha detectado ninguna cara" probs = {} cv2.putText(frameClone, label, (fX, fY - 10), cv2.FONT_HERSHEY_DUPLEX, 1, (238, 164, 64), 1) cv2.rectangle(frameClone, (fX, fY), (fX + fW, fY + fH), (238, 164, 64), 2) for (i, (emotion, prob)) in enumerate(zip(EMOTIONS, preds)): probs[emotion] = float(prob) return frameClone, probs inp = gr.inputs.Image(source="webcam") out = [ gr.outputs.Image(label="Emoción detectada"), gr.outputs.Label(num_top_classes=2, label="Posibles emociones detectadas") ] title = "Reconocimiento de emociones" article = "Es posible que una mala iluminación impida la detección del rostro" description = "Este espacio muestra una demo del proyecto para Saturdays AI (https://saturdays.ai/2021/11/22/reconocimiento-de-emociones/)." gr.Interface(predict, inp, out, capture_session=True, title=title, article=article , theme="dark-peach", description=description).launch(inbrowser=True)