Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,50 +3,31 @@ import tensorflow as tf
|
|
| 3 |
import numpy as np
|
| 4 |
from tensorflow.keras.preprocessing import image
|
| 5 |
|
| 6 |
-
# Cargar modelo
|
| 7 |
def load_model():
|
| 8 |
return tf.keras.models.load_model("modelo_frutas_transfer.keras")
|
| 9 |
|
| 10 |
model = load_model()
|
| 11 |
|
| 12 |
-
#
|
| 13 |
class_names = ['Fresa', 'Limon', 'Manzana', 'Pera', 'Platano', 'Uva']
|
| 14 |
|
| 15 |
-
# Funci贸n de predicci贸n
|
| 16 |
-
def predict_image(
|
| 17 |
-
img =
|
| 18 |
img_array = image.img_to_array(img) / 255.0
|
| 19 |
img_array = np.expand_dims(img_array, axis=0)
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
"""
|
| 35 |
-
# 馃 Clasificador de Frutas con IA
|
| 36 |
-
Sube una imagen de una fruta y el modelo predecir谩 cu谩l es.
|
| 37 |
-
Utiliza aprendizaje profundo y visi贸n por computadora para darte una respuesta precisa.
|
| 38 |
-
"""
|
| 39 |
-
)
|
| 40 |
-
|
| 41 |
-
with gr.Row():
|
| 42 |
-
with gr.Column(scale=1):
|
| 43 |
-
image_input = gr.Image(type="filepath", label="馃摲 Sube una imagen", height=300)
|
| 44 |
-
submit_button = gr.Button("馃攳 Clasificar fruta")
|
| 45 |
-
with gr.Column(scale=1):
|
| 46 |
-
prediction_output = gr.Textbox(label="馃崓 Predicci贸n", lines=1)
|
| 47 |
-
confidence_bar = gr.Slider(0, 1, label="馃攷 Confianza del modelo", interactive=False)
|
| 48 |
-
probs_output = gr.Dataframe(headers=["Fruta", "Probabilidad (%)"], label="馃搳 Tabla de clases", row_count=6, col_count=2)
|
| 49 |
-
|
| 50 |
-
submit_button.click(fn=predict_image, inputs=image_input, outputs=[prediction_output, confidence_bar, probs_output])
|
| 51 |
-
|
| 52 |
-
demo.launch()
|
|
|
|
| 3 |
import numpy as np
|
| 4 |
from tensorflow.keras.preprocessing import image
|
| 5 |
|
| 6 |
+
# Cargar el modelo
|
| 7 |
def load_model():
|
| 8 |
return tf.keras.models.load_model("modelo_frutas_transfer.keras")
|
| 9 |
|
| 10 |
model = load_model()
|
| 11 |
|
| 12 |
+
# Clases del modelo
|
| 13 |
class_names = ['Fresa', 'Limon', 'Manzana', 'Pera', 'Platano', 'Uva']
|
| 14 |
|
| 15 |
+
# Funci贸n de predicci贸n usando imagen PIL
|
| 16 |
+
def predict_image(img):
|
| 17 |
+
img = img.resize((150, 150)) # Asegurar tama帽o
|
| 18 |
img_array = image.img_to_array(img) / 255.0
|
| 19 |
img_array = np.expand_dims(img_array, axis=0)
|
| 20 |
+
pred = model.predict(img_array)
|
| 21 |
+
predicted_class = np.argmax(pred, axis=1)[0]
|
| 22 |
+
return class_names[predicted_class]
|
| 23 |
+
|
| 24 |
+
# Interfaz Gradio (sin "tool", con PIL)
|
| 25 |
+
iface = gr.Interface(
|
| 26 |
+
fn=predict_image,
|
| 27 |
+
inputs=gr.Image(type="pil", label="馃摲 Sube una imagen de fruta", height=300),
|
| 28 |
+
outputs=gr.Textbox(label="馃崕 Predicci贸n de la clase"),
|
| 29 |
+
title="Clasificador de Frutas",
|
| 30 |
+
description="Sube una imagen de una fruta y el modelo predecir谩 qu茅 fruta es."
|
| 31 |
+
)
|
| 32 |
+
|
| 33 |
+
iface.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|