Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -4,47 +4,33 @@ from tensorflow.keras.models import load_model
|
|
4 |
from tensorflow.keras.losses import MeanSquaredError
|
5 |
from PIL import Image
|
6 |
|
7 |
-
# Cargar modelo
|
8 |
model = load_model("autoencoder.h5", custom_objects={"mse": MeanSquaredError()})
|
9 |
|
|
|
|
|
|
|
10 |
# Funci贸n de predicci贸n
|
11 |
def detectar_anomalia(imagen):
|
12 |
-
# Convertir y preprocesar la imagen
|
13 |
imagen = imagen.convert("L").resize((64, 64))
|
14 |
arr = np.array(imagen) / 255.0
|
15 |
arr = arr.reshape((1, 64, 64, 1))
|
16 |
-
|
17 |
-
|
18 |
-
reconstruido = model.predict(arr, verbose=0) # verbose=0 para evitar logs innecesarios
|
19 |
-
|
20 |
-
# Calcular el error de reconstrucci贸n (MSE)
|
21 |
error = np.mean((arr - reconstruido) ** 2)
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
# Convertir a objetos PIL para Gradio
|
28 |
-
imagen_original = Image.fromarray(img_original)
|
29 |
-
imagen_reconstruida = Image.fromarray(img_reconstruida)
|
30 |
-
|
31 |
-
# Determinar si es an贸mala (usando un umbral)
|
32 |
-
is_anomaly = error > 0.01 # Ajusta este umbral seg煤n tus necesidades
|
33 |
-
return (imagen_original,
|
34 |
-
imagen_reconstruida,
|
35 |
-
f"Error de reconstrucci贸n (MSE): {error:.6f}\n驴Es an贸mala?: {'S铆' if is_anomaly else 'No'}")
|
36 |
-
|
37 |
-
# Interfaz de Gradio
|
38 |
demo = gr.Interface(
|
39 |
fn=detectar_anomalia,
|
40 |
inputs=gr.Image(type="pil", label="Sube una imagen para analizar"),
|
41 |
-
outputs=
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
],
|
46 |
-
title="Detecci贸n de Anomal铆as en Textiles con Autoencoder (Keras)",
|
47 |
-
description="Este Space utiliza un autoencoder entrenado con Keras para detectar anomal铆as en im谩genes de textiles industriales."
|
48 |
)
|
49 |
|
50 |
demo.launch()
|
|
|
|
4 |
from tensorflow.keras.losses import MeanSquaredError
|
5 |
from PIL import Image
|
6 |
|
7 |
+
# Cargar el modelo entrenado
|
8 |
model = load_model("autoencoder.h5", custom_objects={"mse": MeanSquaredError()})
|
9 |
|
10 |
+
# Umbral para detectar anomal铆as (ajustable)
|
11 |
+
THRESHOLD = 0.01
|
12 |
+
|
13 |
# Funci贸n de predicci贸n
|
14 |
def detectar_anomalia(imagen):
|
|
|
15 |
imagen = imagen.convert("L").resize((64, 64))
|
16 |
arr = np.array(imagen) / 255.0
|
17 |
arr = arr.reshape((1, 64, 64, 1))
|
18 |
+
|
19 |
+
reconstruido = model.predict(arr, verbose=0)
|
|
|
|
|
|
|
20 |
error = np.mean((arr - reconstruido) ** 2)
|
21 |
+
|
22 |
+
resultado = f"An贸mala" if error > THRESHOLD else "Normal"
|
23 |
+
return resultado
|
24 |
+
|
25 |
+
# Crear la interfaz
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
26 |
demo = gr.Interface(
|
27 |
fn=detectar_anomalia,
|
28 |
inputs=gr.Image(type="pil", label="Sube una imagen para analizar"),
|
29 |
+
outputs=gr.Label(label="Resultado"),
|
30 |
+
examples=["anomalous.png", "normal.png"],
|
31 |
+
title="馃搶 Detecci贸n de Anomal铆as con Autoencoder (Keras)",
|
32 |
+
description="Este Space utiliza un autoencoder entrenado con Keras para detectar anomal铆as en im谩genes de textiles industriales.",
|
|
|
|
|
|
|
33 |
)
|
34 |
|
35 |
demo.launch()
|
36 |
+
|