JeanCGuerrero's picture
Upload app.py
a1b1f3e verified
raw
history blame
1.01 kB
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()