File size: 1,419 Bytes
7d3ce75
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
from keras.layers import TFSMLayer
from PIL import Image
import numpy as np
import io

CLASS_NAMES = ['glioma', 'meningioma', 'no tumor', 'pituitary']

# Load models ONCE


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):
    # 1. Preprocess the image
    image = preprocess_image_bytes(image_bytes)

    # 2. Run Classification directly
    # Removed the detection_model block entirely
    class_pred_tensor = classification_model(image)["output_0"]
    class_pred = class_pred_tensor.numpy()
    
    # 3. Get the index of the highest confidence score
    idx = int(np.argmax(class_pred))
    confidence = float(np.max(class_pred) * 100)
    tumor_type = CLASS_NAMES[idx]

    # 4. Determine has_tumor status based on the classification result
    # We assume "no tumor" is a string in your CLASS_NAMES list
    is_tumor_detected = tumor_type.lower() != "no tumor"

    return {
        "has_tumor": is_tumor_detected,
        "tumor_type": tumor_type,
        "confidence": confidence
    }