test-test / api.py
O S I H
remove useless lines
0b0076c
raw
history blame contribute delete
736 Bytes
from fastapi import FastAPI, File, UploadFile
import numpy as np
from PIL import Image
import io
import tensorflow as tf
model = tf.keras.models.load_model('_9217')
app = FastAPI()
@app.post("/classify")
async def classify(image: UploadFile = File(...)):
if image is not None:
img = Image.open(io.BytesIO(await image.read()))
img = img.resize((128, 128))
img_array = np.array(img) / 255.0
img_array = np.expand_dims(img_array, axis=0)
predictions = model.predict(img_array)
predicted_class_idx = np.argmax(predictions)
predicted_class_idx = int(predicted_class_idx)
return {"prediction": predicted_class_idx}
else:
return {"error": "No image provided"}