|
import tensorflow as tf
|
|
import numpy as np
|
|
from PIL import Image
|
|
import io
|
|
import base64
|
|
import json
|
|
|
|
class MRIClassifier:
|
|
def __init__(self):
|
|
|
|
self.model = tf.keras.models.load_model("./model")
|
|
self.img_size = (224, 224)
|
|
print(f"Model loaded with input size: {self.img_size}")
|
|
|
|
def preprocess(self, image):
|
|
|
|
image = image.resize(self.img_size)
|
|
|
|
|
|
if image.mode != "RGB":
|
|
image = image.convert("RGB")
|
|
|
|
|
|
image_array = np.array(image) / 255.0
|
|
|
|
|
|
image_array = np.expand_dims(image_array, axis=0)
|
|
|
|
return image_array
|
|
|
|
def predict(self, image_bytes):
|
|
|
|
image = Image.open(io.BytesIO(image_bytes))
|
|
|
|
|
|
image_array = self.preprocess(image)
|
|
|
|
|
|
prediction = self.model.predict(image_array)
|
|
pred_value = float(prediction[0][0])
|
|
|
|
|
|
predicted_class = "VAD-Demented" if pred_value > 0.5 else "Non-Demented"
|
|
|
|
return [
|
|
{
|
|
"label": predicted_class,
|
|
"score": pred_value
|
|
}
|
|
]
|
|
|
|
|
|
classifier = MRIClassifier()
|
|
|
|
def inference(model_inputs):
|
|
|
|
if isinstance(model_inputs, dict) and "image" in model_inputs:
|
|
|
|
image_bytes = base64.b64decode(model_inputs["image"])
|
|
else:
|
|
|
|
image_bytes = model_inputs
|
|
|
|
results = classifier.predict(image_bytes)
|
|
return results
|
|
|