|
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 = tf.keras.models.load_model("CLAHE_ODIR-ORJ-512_inception_v3.h5") |
|
|
|
|
|
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"} |
|
|
|
|
|
image = tf.image.resize(image, (229, 229)) |
|
|
|
image = tf.cast(image, tf.float32) / 255.0 |
|
|
|
img_batch = np.expand_dims(image, 0) |
|
|
|
|
|
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) |
|
} |
|
|