JeanCGuerrero commited on
Commit
f2bcbc5
verified
1 Parent(s): c11ad7c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +50 -34
app.py CHANGED
@@ -1,34 +1,50 @@
1
- import gradio as gr
2
- import numpy as np
3
- from tensorflow.keras.models import load_model
4
- from PIL import Image
5
-
6
- # Cargar modelo
7
- model = load_model("autoencoder.h5")
8
-
9
- # Funci贸n de predicci贸n
10
- def detectar_anomalia(imagen):
11
- imagen = imagen.convert("L").resize((64, 64))
12
- arr = np.array(imagen) / 255.0
13
- arr = arr.reshape((1, 64, 64, 1))
14
- reconstruido = model.predict(arr)
15
- error = np.mean((arr - reconstruido) ** 2)
16
-
17
- reconstruido = (reconstruido[0].squeeze() * 255).astype(np.uint8)
18
- imagen_reconstruida = Image.fromarray(reconstruido)
19
-
20
- return imagen, imagen_reconstruida, f"Error MSE: {error:.6f}"
21
-
22
- # Interfaz
23
- demo = gr.Interface(
24
- fn=detectar_anomalia,
25
- inputs=gr.Image(type="pil", label="Sube una imagen"),
26
- outputs=[
27
- gr.Image(label="Imagen original"),
28
- gr.Image(label="Reconstruida"),
29
- gr.Textbox(label="Error de reconstrucci贸n")
30
- ],
31
- title="Autoencoder para Detecci贸n de Anomal铆as (Keras)"
32
- )
33
-
34
- demo.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import numpy as np
3
+ from tensorflow.keras.models import load_model
4
+ from tensorflow.keras.losses import MeanSquaredError
5
+ from PIL import Image
6
+
7
+ # Cargar modelo con custom_objects para mapear 'mse'
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
+ # Obtener la reconstrucci贸n
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
+ # Convertir las im谩genes para visualizaci贸n
24
+ img_original = (arr.squeeze() * 255).astype(np.uint8)
25
+ img_reconstruida = (reconstruido.squeeze() * 255).astype(np.uint8)
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
+ gr.Image(label="Imagen Original"),
43
+ gr.Image(label="Imagen Reconstruida"),
44
+ gr.Textbox(label="Resultado de la Detecci贸n")
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. Sube una imagen para comparar la original con su reconstrucci贸n y determinar si contiene defectos (como manchas, texto o patrones irregulares) bas谩ndote en el error de reconstrucci贸n (MSE). Un MSE alto indica una posible anomal铆a."
48
+ )
49
+
50
+ demo.launch()