| | from keras.layers import TFSMLayer
|
| | from PIL import Image
|
| | import numpy as np
|
| | import io
|
| |
|
| | CLASS_NAMES = ['glioma', 'meningioma', 'no tumor', 'pituitary']
|
| |
|
| |
|
| |
|
| |
|
| | classification_model = TFSMLayer(
|
| | "BrainTumorClassificationModel/model",
|
| | call_endpoint="serving_default"
|
| | )
|
| |
|
| | def preprocess_image_bytes(image_bytes, target_size=(224, 224)):
|
| | image = Image.open(io.BytesIO(image_bytes)).convert("RGB")
|
| | image = image.resize(target_size)
|
| | image = np.array(image).astype("float32") / 255.0
|
| | image = np.expand_dims(image, axis=0)
|
| | return image
|
| |
|
| | def classify_tumor(image_bytes):
|
| |
|
| | image = preprocess_image_bytes(image_bytes)
|
| |
|
| |
|
| |
|
| | class_pred_tensor = classification_model(image)["output_0"]
|
| | class_pred = class_pred_tensor.numpy()
|
| |
|
| |
|
| | idx = int(np.argmax(class_pred))
|
| | confidence = float(np.max(class_pred) * 100)
|
| | tumor_type = CLASS_NAMES[idx]
|
| |
|
| |
|
| |
|
| | is_tumor_detected = tumor_type.lower() != "no tumor"
|
| |
|
| | return {
|
| | "has_tumor": is_tumor_detected,
|
| | "tumor_type": tumor_type,
|
| | "confidence": confidence
|
| | }
|
| |
|