Growth / api.py
Tahiro20's picture
Update api.py
0804686 verified
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('946_.keras', compile=False)
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((64,64))
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"}