File size: 852 Bytes
4975e29
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# Utility functions can go here
import tensorflow as tf
import numpy as np
from PIL import Image

CLASS_NAMES = ['hemorrhagic_stroke', 'ischemic_stroke', 'no_stroke']

def preprocess_image(image_file, target_size=(224, 224)):
    """
    Preprocess uploaded image for prediction
    """
    img = Image.open(image_file).convert("RGB")
    img = img.resize(target_size)

    img_array = tf.keras.utils.img_to_array(img)
    img_array = tf.expand_dims(img_array, 0)

    return img_array


def predict_image(model, image_file):
    """
    Predict stroke type
    """
    processed = preprocess_image(image_file)

    predictions = model.predict(processed)
    index = np.argmax(predictions[0])

    confidence = float(np.max(predictions[0]) * 100)

    return {
        "prediction": CLASS_NAMES[index],
        "confidence": round(confidence, 2)
    }