archivos 1
Browse files- app.py +64 -0
- normal1.jpeg +0 -0
- normal2.jpeg +0 -0
- pneumonia1.jpeg +0 -0
- pneumonia2.jpeg +0 -0
- requirements.txt +3 -0
app.py
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import tensorflow as tf
|
| 2 |
+
from tensorflow import keras
|
| 3 |
+
from PIL import Image
|
| 4 |
+
import numpy as np
|
| 5 |
+
import gradio as gr
|
| 6 |
+
|
| 7 |
+
model_path = './cnn_neumonia.keras'
|
| 8 |
+
IMG_HEIGHT = 64
|
| 9 |
+
IMG_WIDTH = 64
|
| 10 |
+
# 1. Cargar el modelo
|
| 11 |
+
try:
|
| 12 |
+
model = keras.models.load_model(model_path)
|
| 13 |
+
print("Modelo cargado exitosamente.")
|
| 14 |
+
except Exception as e:
|
| 15 |
+
print(f"Error al cargar el modelo: {e}")
|
| 16 |
+
# Detener la ejecuci贸n si el modelo no se carga
|
| 17 |
+
exit()
|
| 18 |
+
|
| 19 |
+
def preprocess_image_for_prediction(image: Image.Image, target_size=(IMG_HEIGHT, IMG_WIDTH)):
|
| 20 |
+
img = image.convert('RGB')
|
| 21 |
+
img = img.resize(target_size)
|
| 22 |
+
img_array = np.array(img)
|
| 23 |
+
img_array = np.expand_dims(img_array, axis=0) # A帽adir dimensi贸n de lote
|
| 24 |
+
img_array = img_array / 255.0 # Normalizar a [0, 1]
|
| 25 |
+
return img_array
|
| 26 |
+
|
| 27 |
+
def predict_pneumonia(image_path_or_object):
|
| 28 |
+
if isinstance(image_path_or_object, str):
|
| 29 |
+
# Si es una ruta, cargar la imagen
|
| 30 |
+
image = Image.open(image_path_or_object)
|
| 31 |
+
else:
|
| 32 |
+
# Si ya es un objeto PIL Image (como en Gradio)
|
| 33 |
+
image = image_path_or_object
|
| 34 |
+
|
| 35 |
+
# Preprocesar la imagen
|
| 36 |
+
processed_image = preprocess_image_for_prediction(image, target_size=(IMG_HEIGHT, IMG_WIDTH))
|
| 37 |
+
|
| 38 |
+
# Realizar la predicci贸n
|
| 39 |
+
prediction = model.predict(processed_image, verbose=0) # verbose=0 para no imprimir el progreso
|
| 40 |
+
|
| 41 |
+
# Interpretar la predicci贸n
|
| 42 |
+
probability = prediction[0][0]
|
| 43 |
+
if probability > 0.5:
|
| 44 |
+
message = f"隆Tiene Neumon铆a! (Probabilidad: {probability:.2f})"
|
| 45 |
+
else:
|
| 46 |
+
message = f"No tiene Neumon铆a (Probabilidad: {probability:.2f})"
|
| 47 |
+
|
| 48 |
+
return message
|
| 49 |
+
|
| 50 |
+
iface = gr.Interface(
|
| 51 |
+
fn=predict_pneumonia,
|
| 52 |
+
title="Predicci贸n de Neumon铆a por Radiograf铆a",
|
| 53 |
+
description="Carga una imagen de radiograf铆a y predice si contiene neumon铆a.",
|
| 54 |
+
inputs=gr.Image(type="pil",label="Sube una imagen de rayos X de t贸rax"),
|
| 55 |
+
outputs=gr.Textbox(label="Diagnostico"),
|
| 56 |
+
examples=[
|
| 57 |
+
'./normal1.jpeg',
|
| 58 |
+
'./normal2.jpeg',
|
| 59 |
+
'./pneumonia1.jpeg',
|
| 60 |
+
'./pneumonia2.jpeg',
|
| 61 |
+
]
|
| 62 |
+
)
|
| 63 |
+
|
| 64 |
+
iface.launch(debug=True,share=True)
|
normal1.jpeg
ADDED
|
normal2.jpeg
ADDED
|
pneumonia1.jpeg
ADDED
|
pneumonia2.jpeg
ADDED
|
requirements.txt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
tensorflow==2.19.0
|
| 2 |
+
pillow==11.3.0
|
| 3 |
+
numpy
|