odirapi / api.py
Bitirme's picture
update file
7b52e04
raw
history blame
1.4 kB
from fastapi import FastAPI, File, UploadFile
import numpy as np
from io import BytesIO
from PIL import Image
import tensorflow as tf
app = FastAPI()
# Model yükleniyor, yüklenen modelin yolunu kontrol ediniz.
MODEL = tf.keras.models.load_model("CLAHE_ODIR-ORJ-512_inception_v3.h5")
# Sınıf isimleri
class_names = [
'Age related Macular Degeneration', 'Cataract', 'Diabetes', 'Glaucoma',
'Hypertension', 'Normal', 'Others', 'Pathological Myopia'
]
@app.get("/ping")
async def ping():
return "Hello, I am alive"
def read_file_as_image(data) -> np.ndarray:
image = np.array(Image.open(BytesIO(data)))
return image
@app.post("/predict")
async def predict(file: UploadFile = File(...)):
image_data = await file.read()
try:
image = read_file_as_image(image_data)
except IOError:
return {"error": "Invalid image format"}
# Görüntüyü modelin beklediği boyuta getirme
image = tf.image.resize(image, (229, 229))
# Görüntüyü normalize etme
image = tf.cast(image, tf.float32) / 255.0
# Batch haline getirme
img_batch = np.expand_dims(image, 0)
# Model üzerinde tahmin yapma
predictions = MODEL.predict(img_batch)
predicted_class = class_names[np.argmax(predictions[0])]
confidence = np.max(predictions[0])
return {
'class': predicted_class,
'confidence': float(confidence)
}