imanolcb commited on
Commit
4ca6dc6
verified
1 Parent(s): e946097

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +37 -18
app.py CHANGED
@@ -3,31 +3,50 @@ import tensorflow as tf
3
  import numpy as np
4
  from tensorflow.keras.preprocessing import image
5
 
6
- # Cargar el modelo (.keras)
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 (ajusta si es necesario)
13
- class_names = ['Fresa', 'Limon', 'Manzana', 'Pera','Platano', 'Uva']
14
 
15
- # Funci贸n de predicci贸n
16
  def predict_image(image_input):
17
  img = image.load_img(image_input, target_size=(150, 150))
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
25
- iface = gr.Interface(
26
- fn=predict_image,
27
- inputs=gr.Image(type="filepath", label="Cargar imagen de fruta"),
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()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ # Etiquetas
13
+ class_names = ['Fresa', 'Limon', 'Manzana', 'Pera', 'Platano', 'Uva']
14
 
15
+ # Funci贸n de predicci贸n con confianza y todas las probabilidades
16
  def predict_image(image_input):
17
  img = image.load_img(image_input, target_size=(150, 150))
18
  img_array = image.img_to_array(img) / 255.0
19
  img_array = np.expand_dims(img_array, axis=0)
20
+
21
+ pred = model.predict(img_array)[0]
22
+ predicted_index = np.argmax(pred)
23
+ predicted_label = class_names[predicted_index]
24
+ confidence = float(pred[predicted_index])
25
+
26
+ # Generar tabla de resultados
27
+ result_table = {class_names[i]: f"{pred[i]*100:.2f}%" for i in range(len(class_names))}
28
+
29
+ return f"馃崜 {predicted_label} ({confidence*100:.2f}% confianza)", confidence, result_table
30
+
31
+ # Interfaz avanzada
32
+ with gr.Blocks(theme=gr.themes.Base(), title="Clasificador de Frutas 馃崕馃崒馃崌") as demo:
33
+ gr.Markdown(
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", tool="editor", 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()