File size: 980 Bytes
5e4d333 4aecfc4 5e4d333 4aecfc4 5e4d333 4aecfc4 5e4d333 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | from fastapi import FastAPI, File, UploadFile
from fastapi.responses import JSONResponse
import numpy as np
from PIL import Image
import io
from tensorflow.keras.models import load_model
app = FastAPI()
# Cargar el modelo al iniciar (solo 1 vez)
model = load_model("modelo_cirrosis.h5")
# Definir las clases en el mismo orden del entrenamiento
class_names = ["sano", "cirrosis"]
@app.post("/predict")
async def predict(file: UploadFile = File(...)):
# Leer la imagen enviada por POST
contents = await file.read()
image = Image.open(io.BytesIO(contents)).convert("RGB")
image = image.resize((150, 150)) # O el tamaño de tu modelo
img_array = np.array(image) / 255.0
img_array = np.expand_dims(img_array, 0)
# Predicción
prediction = model.predict(img_array)
pred_index = np.argmax(prediction)
result = {
"class": class_names[pred_index],
"confidence": float(np.max(prediction))
}
return JSONResponse(result)
|