-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
57 lines (40 loc) · 1.57 KB
/
main.py
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
from fastapi import FastAPI, UploadFile, HTTPException
from fastapi.middleware.cors import CORSMiddleware
import numpy as np
#from PIL import Image
from io import BytesIO
from tensorflow.keras.models import load_model
model = load_model('modelo_SinapsIA_SIGMOID_92.h5')
app = FastAPI()
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
@app.get("/")
async def a ():
return ("hello fastapi")
@app.post("/upload/")
async def create_upload_file(inputDoc: UploadFile):
try:
# Obtener los bytes directamente desde el objeto UploadFile
photo_bytes = await inputDoc.read()
# Cargar el archivo Numpy directamente
photo_data = np.load(BytesIO(photo_bytes))
# Asegurarse de que las dimensiones del array sean las esperadas por el modelo
#if photo_data.shape != (224, 224, 3):
# raise HTTPException(status_code=400, detail=photo_data.shape)
# Agregar una dimensión para coincidir con las expectativas del modelo
#photo_data = np.expand_dims(photo_data, axis=0)
# Realizar la predicción con el modelo
model_output = model.predict(photo_data)
# Promediar todos los números del array de salida del modelo
processed_output = np.mean(model_output)
porcentaje= np.mean(processed_output) * 100
return {"processed_output": float(porcentaje)}
except HTTPException as e:
raise e
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))