prova1
Browse files
app.py
CHANGED
|
@@ -1,7 +1,38 @@
|
|
| 1 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
-
|
| 4 |
-
|
| 5 |
|
| 6 |
-
|
| 7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import torch
|
| 3 |
+
from fastai.vision.all import *
|
| 4 |
+
from PIL import Image
|
| 5 |
|
| 6 |
+
# Cargar el modelo desde el archivo .pkl
|
| 7 |
+
modelo = load_learner('model.pkl')
|
| 8 |
|
| 9 |
+
# Funci贸n de clasificaci贸n
|
| 10 |
+
def clasificar_imagen(imagen):
|
| 11 |
+
imagen = Image.fromarray(imagen.astype('uint8'), 'RGB')
|
| 12 |
+
# Preprocesar la imagen si es necesario (ajustar tama帽o, normalizar, etc.)
|
| 13 |
+
imagen_preprocesada = pil2tensor(imagen, dtype=np.float32)
|
| 14 |
+
# Realizar la predicci贸n con el modelo
|
| 15 |
+
predicciones, _, _ = modelo.predict(imagen_preprocesada)
|
| 16 |
+
etiqueta = str(predicciones[0])
|
| 17 |
+
return etiqueta
|
| 18 |
+
|
| 19 |
+
# Funci贸n de interpretaci贸n
|
| 20 |
+
def interpretar_imagen(imagen):
|
| 21 |
+
imagen = Image.fromarray(imagen.astype('uint8'), 'RGB')
|
| 22 |
+
# Preprocesar la imagen si es necesario (ajustar tama帽o, normalizar, etc.)
|
| 23 |
+
imagen_preprocesada = pil2tensor(imagen, dtype=np.float32)
|
| 24 |
+
# Obtener las zonas de enfoque del modelo
|
| 25 |
+
interp = ClassificationInterpretation.from_learner(modelo, imagen_preprocesada)
|
| 26 |
+
zonas_enfoque, _ = interp.top_losses()
|
| 27 |
+
# Aplicar el resaltado de las zonas de enfoque a la imagen original
|
| 28 |
+
imagen_interpretada = interp.plot_top_losses(k=1, img_tensor=imagen_preprocesada)[0]
|
| 29 |
+
return imagen_interpretada
|
| 30 |
+
|
| 31 |
+
# Definir la interfaz de usuario con Gradio
|
| 32 |
+
entrada = gr.inputs.Image()
|
| 33 |
+
salida = gr.outputs.Textbox()
|
| 34 |
+
|
| 35 |
+
interfaz = gr.Interface(fn=clasificar_imagen, inputs=entrada, outputs=salida, interpretation=interpretar_imagen)
|
| 36 |
+
|
| 37 |
+
# Ejecutar la interfaz de usuario
|
| 38 |
+
interfaz.launch()
|