Spaces:
Sleeping
Sleeping
Upload app.py
Browse files
app.py
ADDED
@@ -0,0 +1,34 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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()
|