-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathproyecto.py
More file actions
316 lines (249 loc) · 12.1 KB
/
Copy pathproyecto.py
File metadata and controls
316 lines (249 loc) · 12.1 KB
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
import cv2
import numpy as np
import tensorflow as tf
from tensorflow.keras.applications import MobileNetV2
from tensorflow.keras.models import Model
from tensorflow.keras.layers import Dense, GlobalAveragePooling2D
from tensorflow.keras.preprocessing.image import img_to_array, load_img
from tensorflow.keras.optimizers import Adam
from tensorflow.keras.callbacks import EarlyStopping
from sklearn.decomposition import PCA
from sklearn.discriminant_analysis import LinearDiscriminantAnalysis as LDA
from sklearn.pipeline import Pipeline
from sklearn.svm import SVC
from sklearn.metrics import accuracy_score, f1_score, confusion_matrix, classification_report
import os
import matplotlib.pyplot as plt
import seaborn as sns
import tkinter as tk
from tkinter import filedialog
# --- CONFIGURACION ---
BASE_DIR = 'Synthetic_DB_CAS' # Nombre corregido de la DB
CLASSES = ['confused', 'distracted', 'fatigued', 'joyful', 'neutral']
NUM_CLASSES = len(CLASSES)
# Rutas a los splits
TRAIN_DIR = os.path.join(BASE_DIR, 'train')
VAL_DIR = os.path.join(BASE_DIR, 'val')
TEST_DIR = os.path.join(BASE_DIR, 'test')
# --- FUNCIONES DE PREPROCESAMIENTO ---
def preprocess_fisherfaces(img_array):
"""Escala de grises, 100x100, estandarizacion."""
# Estandarizar (media 0, varianza 1)
standardized_img = (img_array - np.mean(img_array)) / (np.std(img_array) + 1e-6)
return standardized_img.flatten()
def preprocess_mobilenet(img_array):
"""Normalizacion para MobileNet [0, 1]."""
return img_array / 255.0
# --- CARGA DE DATOS ---
def load_images_from_directory(directory, target_size, is_rgb, model_type):
"""Carga imagenes recorriendo las subcarpetas de clases."""
X = []
y = []
label_map = {cls: i for i, cls in enumerate(CLASSES)}
print(f"Cargando datos desde: {directory} ({model_type})")
if not os.path.exists(directory):
print(f"ERROR: La carpeta {directory} no existe.")
return np.array([]), np.array([])
for class_name in CLASSES:
class_path = os.path.join(directory, class_name)
if not os.path.isdir(class_path):
continue
label_index = label_map[class_name]
files_list = os.listdir(class_path)
for filename in files_list:
img_path = os.path.join(class_path, filename)
try:
color_mode = 'rgb' if is_rgb else 'grayscale'
img = load_img(img_path, color_mode=color_mode, target_size=target_size)
img_array = img_to_array(img)
if model_type == 'mobilenet':
processed = preprocess_mobilenet(img_array)
else: # fisherfaces
img_np = np.squeeze(img_array)
processed = preprocess_fisherfaces(img_np)
X.append(processed)
y.append(label_index)
except Exception as e:
pass
return np.array(X), np.array(y)
# --- VISUALIZACION ---
def plot_history(history, model_name):
"""Grafica curvas de Accuracy y Loss."""
acc = history.history['accuracy']
val_acc = history.history['val_accuracy']
loss = history.history['loss']
val_loss = history.history['val_loss']
epochs_range = range(len(acc))
plt.figure(figsize=(12, 5))
plt.subplot(1, 2, 1)
plt.plot(epochs_range, acc, label='Training Accuracy')
plt.plot(epochs_range, val_acc, label='Validation Accuracy')
plt.legend(loc='lower right')
plt.title(f'{model_name} - Accuracy')
plt.grid(True)
plt.subplot(1, 2, 2)
plt.plot(epochs_range, loss, label='Training Loss')
plt.plot(epochs_range, val_loss, label='Validation Loss')
plt.legend(loc='upper right')
plt.title(f'{model_name} - Loss')
plt.grid(True)
plt.show()
def plot_confusion_matrix_custom(y_true, y_pred, title):
"""Genera la Matriz de Confusion visual."""
cm = confusion_matrix(y_true, y_pred)
plt.figure(figsize=(8, 6))
sns.heatmap(cm, annot=True, fmt='d', cmap='Blues',
xticklabels=CLASSES, yticklabels=CLASSES)
plt.xlabel('Prediccion')
plt.ylabel('Verdad (Ground Truth)')
plt.title(f'Matriz de Confusion: {title}')
plt.show()
# =============================================================================
# BLOQUE 1: ENTRENAMIENTO MOBILENET (Deep Learning)
# =============================================================================
print("\n=== INICIANDO MODELO MOBILENET (RGB 224x224) ===")
X_train_mb, y_train_mb = load_images_from_directory(TRAIN_DIR, (224, 224), True, 'mobilenet')
X_val_mb, y_val_mb = load_images_from_directory(VAL_DIR, (224, 224), True, 'mobilenet')
X_test_mb, y_test_mb = load_images_from_directory(TEST_DIR, (224, 224), True, 'mobilenet')
model_mb = None
if len(X_train_mb) > 0:
y_train_cat = tf.keras.utils.to_categorical(y_train_mb, NUM_CLASSES)
y_val_cat = tf.keras.utils.to_categorical(y_val_mb, NUM_CLASSES)
base_model = MobileNetV2(weights='imagenet', include_top=False, input_shape=(224, 224, 3))
base_model.trainable = False
x = base_model.output
x = GlobalAveragePooling2D()(x)
x = Dense(128, activation='relu')(x)
predictions = Dense(NUM_CLASSES, activation='softmax')(x)
model_mb = Model(inputs=base_model.input, outputs=predictions)
print("Entrenando MobileNet (Capas superiores)...")
model_mb.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])
history_mb = model_mb.fit(
X_train_mb, y_train_cat,
epochs=10,
validation_data=(X_val_mb, y_val_cat),
verbose=1
)
plot_history(history_mb, 'MobileNetV2')
print("\n--- Evaluacion MobileNet en Test Set ---")
y_pred_probs = model_mb.predict(X_test_mb)
y_pred_mb = np.argmax(y_pred_probs, axis=1)
print("Accuracy:", accuracy_score(y_test_mb, y_pred_mb))
print(classification_report(y_test_mb, y_pred_mb, target_names=CLASSES))
plot_confusion_matrix_custom(y_test_mb, y_pred_mb, 'MobileNetV2')
else:
print("No se encontraron datos de entrenamiento para MobileNet.")
# =============================================================================
# BLOQUE 2: ENTRENAMIENTO FISHERFACES (PCA + LDA)
# =============================================================================
print("\n=== INICIANDO MODELO FISHERFACES (Grayscale 100x100) ===")
X_train_ff, y_train_ff = load_images_from_directory(TRAIN_DIR, (100, 100), False, 'fisherfaces')
X_test_ff, y_test_ff = load_images_from_directory(TEST_DIR, (100, 100), False, 'fisherfaces')
if len(X_train_ff) > 0:
print(f"Dimensiones Fisherfaces Train: {X_train_ff.shape}")
n_components_pca = min(150, X_train_ff.shape[0] - 1)
fisher_pipeline = Pipeline([
('pca', PCA(n_components=n_components_pca, whiten=True)),
('lda', LDA(n_components=NUM_CLASSES - 1)),
('clf', SVC(kernel='linear', class_weight='balanced'))
])
print("Entrenando Fisherfaces...")
fisher_pipeline.fit(X_train_ff, y_train_ff)
print("\n--- Evaluacion Fisherfaces en Test Set ---")
y_pred_ff = fisher_pipeline.predict(X_test_ff)
print("Accuracy:", accuracy_score(y_test_ff, y_pred_ff))
print(classification_report(y_test_ff, y_pred_ff, target_names=CLASSES))
plot_confusion_matrix_custom(y_test_ff, y_pred_ff, 'Fisherfaces (PCA+LDA)')
else:
print("No se encontraron datos para Fisherfaces.")
# =============================================================================
# BLOQUE 3: VALIDACION CON VIDEOS REALES (SELECCION LOCAL)
# =============================================================================
print("\n" + "="*60)
print("=== FASE 2: VALIDACION CON VIDEOS REALES ===")
print("="*60)
FPS_TARGET = 1 # 1 frame por segundo
if model_mb is not None:
print("\nINSTRUCCIONES:")
print("1. Se abrira una ventana de explorador de archivos.")
print("2. Selecciona TODOS los videos que quieras probar.")
print("3. Luego, el programa te pedira la etiqueta real para cada uno.")
print("-" * 60)
# Abrir ventana de seleccion de archivos (Tkinter)
root = tk.Tk()
root.withdraw() # Ocultar la ventana principal de Tkinter
root.attributes('-topmost', True) # Asegurar que la ventana salga al frente
print("Esperando seleccion de archivos...")
file_paths = filedialog.askopenfilenames(
title='Selecciona tus videos de prueba',
filetypes=[("Archivos de video", "*.mp4 *.avi *.mov *.mkv *.webm")]
)
if not file_paths:
print("No se seleccionaron archivos. Finalizando.")
else:
print(f"Se seleccionaron {len(file_paths)} videos.")
# Iterar sobre los videos seleccionados
for video_path in file_paths:
video_filename = os.path.basename(video_path)
print(f"\nProcesando archivo: {video_filename}")
# --- PREGUNTA INTERACTIVA ---
print(f"¿Cual es la emocion REAL (Ground Truth) del video '{video_filename}'?")
print(f"Opciones validas: {CLASSES}")
etiqueta_input = input("Escribe la etiqueta aqui: ").strip().lower()
# Validar entrada
if etiqueta_input not in CLASSES:
print(f"Error: '{etiqueta_input}' no es una clase valida.")
etiqueta_real_valida = False
else:
etiqueta_real_valida = True
real_idx = CLASSES.index(etiqueta_input)
# Extraccion y Prediccion
cap = cv2.VideoCapture(video_path)
fps = cap.get(cv2.CAP_PROP_FPS)
frame_interval = int(fps / FPS_TARGET) if fps > FPS_TARGET else 1
predictions_frame = []
frame_count = 0
extracted_count = 0
while cap.isOpened():
ret, frame = cap.read()
if not ret: break
if frame_count % frame_interval == 0:
frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
face = cv2.resize(frame_rgb, (224, 224))
img_array = img_to_array(face)
processed = preprocess_mobilenet(img_array)
processed = np.expand_dims(processed, axis=0) # Batch de 1
preds = model_mb.predict(processed, verbose=0)
predictions_frame.append(preds[0])
extracted_count += 1
frame_count += 1
cap.release()
if extracted_count > 0:
predictions_frame = np.array(predictions_frame)
# Promedio de probabilidades
avg_preds = np.mean(predictions_frame, axis=0)
final_pred_idx = np.argmax(avg_preds)
final_pred_label = CLASSES[final_pred_idx]
print("\n" + "-"*40)
print(f"RESULTADOS: {video_filename}")
print("-"*40)
if etiqueta_real_valida:
print(f"Etiqueta Real (Tu input): {etiqueta_input.upper()}")
else:
print(f"Etiqueta Real (Tu input): NO VALIDA")
print(f"Prediccion del Modelo: {final_pred_label.upper()}")
print("\nProbabilidades promedio (Confianza):")
for i, cls in enumerate(CLASSES):
prob = avg_preds[i] * 100
marcador = "<--" if i == final_pred_idx else ""
print(f"{cls:10s}: {prob:6.2f}% {marcador}")
if etiqueta_real_valida:
if final_pred_idx == real_idx:
print("\n---> [CONCLUSION: PREDICCION CORRECTA]")
else:
print("\n---> [CONCLUSION: PREDICCION INCORRECTA]")
else:
print("Error: No se pudieron extraer frames validos del video.")
else:
print("Error: El modelo MobileNet no se entreno correctamente. Revisa los datos de entrenamiento.")
print("\n=== PROCESO TERMINADO ===")