-
Notifications
You must be signed in to change notification settings - Fork 0
/
helper.py
71 lines (57 loc) · 1.78 KB
/
helper.py
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import tensorflow as tf
import numpy as np
import streamlit as st
from PIL import Image
import time
import os
# Set class names used in the model
class_names = ["Glioma", "Meningioma", "No Tumor", "Pituitary"]
@st.cache_resource
def load_model(path):
"""
Parameters:
path - takes path of the model to be loaded
Returns-
A loaded model.
"""
model = tf.keras.models.load_model(path)
return model
@st.cache_data
def predict(path):
"""
Parameters:
path - takes path of an image which is input to the model
Returns-
A score for a class predicted by the model.
"""
model = load_model("model/brainT_detect.h5")
img = tf.keras.utils.load_img(path, target_size=(180, 180))
input_arr = tf.keras.utils.img_to_array(img)
input_arr = np.array([input_arr])
predictions = model.predict(input_arr)
scores = tf.nn.softmax(predictions[:])
return scores
@st.cache_data
def make_path(option):
return os.path.join("img/test_imgs/", option)
def prediction_runner(img_path):
img = Image.open(img_path)
resized_img = img.resize((256, 256))
st.title("Here is the image you've selected:")
st.divider()
st.image(resized_img)
st.divider()
scores = predict(img_path)
st.title("Result of MRI scan:")
with st.spinner(text="In progress..."):
time.sleep(3)
if (class_names[np.argmax(scores)]) == "No Tumor":
st.success(
f"This image most likely belongs to '{class_names[np.argmax(scores)]}' with a {100 * np.max(scores):.2f} percent confidence.",
icon="✅",
)
else:
st.error(
f"This image most likely belongs to '{class_names[np.argmax(scores)]}' with a {100 * np.max(scores):.2f} percent confidence.",
icon="❗",
)