File size: 3,534 Bytes
9f94bb9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
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 de mapeo de etiquetas en inglés a etiquetas en español
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_path):
    img = PILImage.create(img_path)
    
    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(shape=(48, 48), image_mode='L'),
    outputs=[gr.Label(label='Emoción'), gr.Label(label='Sentimiento')],
    title=title,
    examples=examples,
    description=description,
    article=article,
    allow_flagging='never'
)

iface.launch(enable_queue=True)






#################

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

# Función de predicción
def predict(img_path):
    img = PILImage.create(img_path)
    
    pred_emotion, pred_emotion_idx, probs_emotion = learn_emotion.predict(img)
    pred_sentiment, pred_sentiment_idx, probs_sentiment = learn_sentiment.predict(img)
    
    emotions = {label: float(prob) for label, prob in zip(learn_emotion_labels, probs_emotion)}
    sentiments = {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(shape=(48, 48), image_mode='L'),
    outputs=[gr.Label(label='Emotion'), gr.Label(label='Sentiment')],
    title=title,
    examples=examples,
    description=description,
    article=article,
    allow_flagging='never'
)

iface.launch(enable_queue=True)