File size: 2,053 Bytes
6c67cce 55316a9 4ced22e 55316a9 2a1d5c1 699ec29 55316a9 2fe605f 55316a9 699ec29 55316a9 699ec29 55316a9 4ced22e f8aaecd 4ced22e 55316a9 2fe605f 699ec29 55316a9 01e6a5a |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
import os
from fastai.vision.all import *
import gradio as gr
# Cargar los modelos
learn_emotion = load_learner('emotions_jey.pkl')
learn_emotion_labels = learn_emotion.dls.vocab
learn_sentiment = load_learner('sentiment_jey.pkl')
learn_sentiment_labels = learn_sentiment.dls.vocab
# Diccionario
label_mapping = {
'angry': 'enojado',
'disgust': 'asco',
'fear': 'miedo',
'happy': 'feliz',
'sad': 'triste',
'surprise': 'sorpresa',
'neutral': 'neutral',
'negative': 'negativo',
'positive': 'positivo'
}
# Funci贸n de predicci贸n
def predict(img):
img = img.convert('L') # Convertir la imagen a escala de grises
img = img.resize((48, 48)) # Redimensionar la imagen a (48, 48)
img = PILImage.create(img) # Convertir a PILImage
pred_emotion, pred_emotion_idx, probs_emotion = learn_emotion.predict(img)
pred_sentiment, pred_sentiment_idx, probs_sentiment = learn_sentiment.predict(img)
emotions = {label_mapping[label]: float(prob) for label, prob in zip(learn_emotion_labels, probs_emotion)}
sentiments = {label_mapping[label]: float(prob) for label, prob in zip(learn_sentiment_labels, probs_sentiment)}
return emotions, sentiments
# Interfaz de Gradio
title = "Detector de emociones y sentimientos faciales"
description = (
"Esta interfaz utiliza redes neuronales para detectar emociones y sentimientos a partir de im谩genes faciales."
)
article = "Esta herramienta proporciona una forma r谩pida de analizar emociones y sentimientos en im谩genes."
examples = [
'PrivateTest_10131363.jpg',
'angry1.png',
'angry2.jpg',
'happy1.jpg',
'happy2.jpg',
'neutral1.jpg',
'neutral2.jpg'
]
iface = gr.Interface(
fn=predict,
inputs=gr.Image(type='pil'), # Eliminar el argumento 'shape'
outputs=[gr.Label(label='Emoci贸n'), gr.Label(label='Sentimiento')],
title=title,
examples=examples,
description=description,
article=article,
allow_flagging='never'
)
iface.launch() # Eliminar el argumento 'enable_queue'
|