File size: 1,009 Bytes
a1b1f3e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
import numpy as np
from tensorflow.keras.models import load_model
from PIL import Image

# Cargar modelo
model = load_model("autoencoder.h5")

# Funci贸n de predicci贸n
def detectar_anomalia(imagen):
    imagen = imagen.convert("L").resize((64, 64))
    arr = np.array(imagen) / 255.0
    arr = arr.reshape((1, 64, 64, 1))
    reconstruido = model.predict(arr)
    error = np.mean((arr - reconstruido) ** 2)

    reconstruido = (reconstruido[0].squeeze() * 255).astype(np.uint8)
    imagen_reconstruida = Image.fromarray(reconstruido)

    return imagen, imagen_reconstruida, f"Error MSE: {error:.6f}"

# Interfaz
demo = gr.Interface(
    fn=detectar_anomalia,
    inputs=gr.Image(type="pil", label="Sube una imagen"),
    outputs=[
        gr.Image(label="Imagen original"),
        gr.Image(label="Reconstruida"),
        gr.Textbox(label="Error de reconstrucci贸n")
    ],
    title="Autoencoder para Detecci贸n de Anomal铆as (Keras)"
)

demo.launch()